RCampbell
RCampbell

Reputation: 43

JAVA - How do I detect "\n" characters from a scanner reading from a file

First time poster,

I'm having trouble reading a return character from a scanner reading through a text file.

The text file being read from looks like this:

//test.txt start//

2

0 30 30 1

1 90 30 0

//test.txt end//

First Line: 2 (indicating two points)

Second Line: position index: 0 xpos: 30 ypos: 30 draw line to position 1

Third Line: position index: 1 xpos: 90 ypos: 30 draw line to position 0

I know the code change should go in this do/while.

do {        
    edge[pNum].add(input.nextInt());
} while(input.hasNextInt());

The rest of the code seems to function as intended, but I can't seem to detect the "\n" return characters from the text file in order to save the x,y values in the position array and the following values in the ArrayList and start the process over for the next line in the text file.

Here's the full code below:

public class PointReader extends JFrame {

    class GraphView extends JPanel{

    }

    public static void main(String[] args) throws Exception{

        String fileName;

        System.out.print("Input File Name: ");
        Scanner user = new Scanner( System.in ); 
        fileName = user.nextLine();

        java.io.File file = new java.io.File(fileName);

        Scanner input = new Scanner(file);

        int count = input.nextInt();

        int[][] position = new int[count][2];
        ArrayList[] edge = new ArrayList[count];

        for(int k = 0; k < edge.length; k++){
            edge[k] = new ArrayList<Integer>();
        }

        while(input.hasNextInt()){

            int pNum = input.nextInt();
            int xPos = input.nextInt();
            int yPos = input.nextInt();

            position[pNum][0] = xPos;
            position[pNum][1] = yPos;

            do{        
                edge[pNum].add(input.nextInt());
            }while(input.hasNextInt());
        }

        System.out.println(count);

        for(int i=0; i < count; i++){
            System.out.println(i + " " + position[i][0] + " " + position[i][1] + " ");

            for(int j = 0; j < edge[i].size(); j++){
                System.out.print(edge[i].get(j) + " ");
            }
        }
    }
}

I've tried while(!(input.next().equals("\n"))); and it's still not being detected. Any Ideas?

Upvotes: 4

Views: 6398

Answers (3)

RCampbell
RCampbell

Reputation: 43

I ended up using input.nextLine() to grab the rest of the values and then toss those through another scanner thanks to L33D's suggestion:

Scanner input = new Scanner(file);

            int count = input.nextInt();

            int[][] position = new int[count][2];
            ArrayList[] edge = new ArrayList[count];

        for(int k = 0; k < edge.length; k++){
            edge[k] = new ArrayList<Integer>();
        }

         while(input.hasNext()){

            int pNum = input.nextInt();
            int xPos = input.nextInt();
            int yPos = input.nextInt();

            position[pNum][0] = xPos;
            position[pNum][1] = yPos;

            String extra = input.nextLine();
            Scanner linescan = new Scanner(extra);

            while(linescan.hasNextInt()){
                edge[pNum].add(linescan.nextInt());
            }
        }

Then is code displays the file read:

// displays input read from file
        System.out.println("File input from file: ");
        System.out.println(count);

        for(int i=0; i < count; i++){
            System.out.print(i + " " + position[i][0] + " " + position[i][1] + " ");

            for(int j = 0; j < edge[i].size(); j++){
                System.out.print(edge[i].get(j) + " ");
            }

        System.out.println();
        }

and finally the graphing code is as follows:

// draws points, edges, and finally labels points
        int pointName = 0;

        for(int h = 0; h < count; h++){

            g.fillOval(position[h][0], position[h][1], 10, 10);

            for(int y = 0; y < edge[h].size(); y++){
                int ref = (int)edge[h].get(y);
                g.drawLine((position[h][0] + 5), (position[h][1] + 5), (position[ref][0] + 5), (position[ref][1]+ 5));
            }
            if(((h % 2) == 0)){
                g.drawString(" " + pointName+ " ", (position[h][0] - 15), (position[h][1] + 10));
            }
            else{
                g.drawString(" " + pointName + " ", (position[h][0] + 15), (position[h][1] + 10));
            }
            pointName++;
        }

Upvotes: 0

ggovan
ggovan

Reputation: 1927

The problem is that a new line character is treated as white space.

You can manually set the delimiter of the Scanner:

scanner = new Scanner(...).useDelimiter(" "); //To use only space as a delimiter.

This will make line feeds appear as tokens which will be returned by scanner.next();

The changes that you suggested to your code should now work.

Upvotes: 3

flotothemoon
flotothemoon

Reputation: 1892

If you want to seperate the file into lines, the easiest way is probably to process each line individually.

while (input.hasNextLine())
{
    String line = input.nextLine();

    // DO STUFF WITH LINE...
}

So you get each line into the String "line" and can do something with it then, and you dont have to take care of seperating the lines yourself. But other than that, I am not hundred percent sure what you are trying to do.

Upvotes: 3

Related Questions