0
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!!

mariatsamp
  • 49
  • 7

2 Answers2

2

Check this code. I have made it shorter

To read next line Java has nextLine() and to check if there is a next line Java has hasNextLine()

import java.util.*; 
import java.io.*; 
public class Dooms
{
    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 lines in the file
    while(scan.hasNextLine())
    {
        String line = new String(scan.nextLine());
        System.out.println(line);
        counter++;
    }

    System.out.println("There are "+counter+" lines");
    scan.close();

   }

}

Now you can easily store the file content in the 2d array with little modification.

shubham johar
  • 268
  • 1
  • 9
  • Thank you for your code. I come from a C background and Im extremely new in Java. Is there any function to replace hasNextInt to read chars? Since I want to read the file char by char – mariatsamp May 19 '18 at 15:57
0

As pointed by @shubham johar, Java's Scanner class does have nextLine() method that could really simplify the code. Conversion here is to a char array as requested, but those arrays are line by line added into an ArrayList, because we cannot in advance know how many lines is going to be there. That is, sort of, the equivalent of a requested 2D array.
If it doesn't matter to be specifically char array, I would recommend usage of String class.

For total line count, you can always take the size of an ArrayList.
Here's the code:

import java.util.*; 
import java.io.*; 

public class Doomday {
    public static void main (String args[]) throws FileNotFoundException {
        Scanner lineScanner = new Scanner(new File(args[0])); 
        ArrayList<char []> lines = new ArrayList<char []>();

        while(lineScanner.hasNext()) {
            lines.add(lineScanner.nextLine().toCharArray());
        }

        for(char line[] : lines) {
            System.out.println(line);
        }

        System.out.println("Line count: " + lines.size());

        lineScanner.close();
    }


}

EDIT:

Version of code, reading file char by char:

public class Doomday {
    public static void main (String args[]) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File(args[0])); 
        scanner.useDelimiter("");
        int numberOfLines = 0;

        while(scanner.hasNext()) {
            char c = scanner.next().charAt(0);

            if(c == System.getProperty("line.separator").charAt(0)) {
                numberOfLines++;
                continue;
            }

            // do something with char
        }

        System.out.println("Number of lines: " + numberOfLines);

        scanner.close();
    }

}
  • Thank you for your code. I come from a C background and Im extremely new in Java. Is there any function to replace hasNextInt to read chars? – mariatsamp May 19 '18 at 15:57
  • Yes, you can use .next() function, but before that, you have to tell your scanner to use empty string as a delimiter. See this answer for more details: https://stackoverflow.com/a/28008444/6945057 – creepy_cubicle May 19 '18 at 15:59
  • Thank you very much for your solution! It really helped! – mariatsamp May 19 '18 at 16:38