0

I have a problem with last line of input. I cannot force Scanner to read a number from last line since it is unfinished. When I copy input with one more line after the previous last line, there is no problem, but i cannot have the extra line there.

Link to problem

Example of input:

2  
1 1  
9 9  
4   
4 5  
6 5  
2 5  
3 4  

-1  

I've tried sc.nextInt(), sc.next(), sc.nextLine() and nothing can reach the -1. Even sc.hasNext, sc.hasNextInt etc. are waiting.

while(true)
    {
        line = sc.nextLine();
        try {
            a = Integer.parseInt(line);
        } 
        catch (NumberFormatException e) {
        }

        if(a == -1)
        {
            break;
        }

        rr = new int[a][2];

        for(int i = 0; i < a; i++)
        {
            line = sc.nextLine();
            numbers = line.split(" ");

            try {
                rr[i][0] = Integer.parseInt(numbers[0]);
                rr[i][1] = Integer.parseInt(numbers[1]);
            } 
            catch (NumberFormatException e) {
            }

        }

        b = sc.nextInt();

        sc.nextLine();

        tt = new int[b][2];

        for(int i = 0; i < b; i++)
        {
            line = sc.nextLine();
            numbers = line.split(" ");

            try {
                tt[i][0] = Integer.parseInt(numbers[0]);
                tt[i][1] = Integer.parseInt(numbers[1]);
            } 
            catch (NumberFormatException e) {
            }

        }
        sc.nextLine();

    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Daniel Švarc
  • 59
  • 1
  • 9

2 Answers2

1
while(true) {
    // read the line.. 
    line = sc.nextLine().trim();
    // check if it's a blank line...
    // blank line means new test case.. Take the value of n
    if (line.length() == 0) line = sc.nextLine().trim();

    // now parse the value of n.. If it's -1 then end of input
    a = Integer.parseInt(line);
    if (a==-1) break;

    // rest of your input
}

Take the value of b like this:

b = Integer.parseInt(sc.nextLine().trim());

And remove the 'extra' sc.nextLine()from below of b and from the bottom of while loop. That case is handled at the top.

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
  • Doesnt work neither. I tried to debug it and it stucks on the if(line.length() == 0) line = sc.nextLine(); – Daniel Švarc Nov 27 '16 at 21:55
  • Use these sites: http://uhunt.felix-halim.net/id/377362 https://www.udebug.com/UVa/12389 I have edited my answer. You need to probably trim the strings before you parse it. – denvercoder9 Nov 28 '16 at 06:15
0

Before trying to get the -1 you could have a separate scanner use the delimiter /n (line break) and have it delete the extra break between -1 and the last line before it. Then you could go through and just scan with .nextLine() or .nextInt().

  • Isnt it the same what does .nextLine() on the same place? – Daniel Švarc Nov 27 '16 at 21:21
  • .nextLine() will return whatever is on the next line. What I am proposing is to set up a scanner so that it searches for blank lines, and then edits the file so that it contains no blank lines. After that, it goes through and gets the integers you want, and should have no problem getting -1 since it will no longer have an empty line separating it from the other numbers. – Dane Williams Nov 27 '16 at 21:27