import java.util.*;
import java.io.*;
public class Doomday
{
public static void main (String args[]) throws FileNotFoundException
{
Scanner scan = new Scanner(new File(args[0])); //provide file name from outside
int counter =0; //keep track of how many integers in the file
while(scan.hasNextInt())
{
counter++;
scan.nextInt();
}
Scanner scan2 = new Scanner(new File(args[0]));
int a[] = new int[counter];
for(int i=0;i<counter;i++)
{
a[i]=scan2.nextInt(); //fill the array with the integers
}
for(int i=0;i<counter;i++)
{
System.out.print(a[i]);
}
}
}
The above sample of code reads input from a file, integers, via the command line and stores them into an array. How can I modify the code so as it reads chars from the file? I also want the program to count the lines of the file.
for example, as this for an input
+.........-
-+--------.
I want to store this into a 2d array . This would be easy if I knew how to count the number of chars and the new lines (since given the number of lines and chars -and each line in my program has the same number of chars- I can find how many rows I have)
So how Can I change the above code to read chars from the file and count the lines of the program?
Can you provide your help with some code?
Thanks!!