0

I have the following code, I am suppose to give the input though system.in, however, the program skit the frist input, but it reads the secod one, it skips the 3rd input but it reads the forth one and so on. I can not figure out the problem. here is my code:

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

public class problem1a {
    private static HashMap<String,Integer> ales;

    private static int counter = 0;
    private static ArrayList<String> names;

    private static ArrayList<String> city;
    private static ArrayList<Integer> count;

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

        Scanner sc = new Scanner(System.in);
        // initialize name,line variables
        names = new ArrayList<String>();
        ales = new HashMap<String,Integer>();
        //store the names of the city and the corresponding student
        while(sc.hasNextLine() ){
            String s = removeNumbers(sc.nextLine());

            Integer c = ales.get(s);
            if(c == null){
                ales.put(s, 1);
            }
            else{
                ales.put(s, c.intValue() + 1);
            }

            if(sc.nextLine().equals(""))
                break;

            System.out.println(ales.toString());
        }

    }

}

so here is my input and the output:

input: 3456345 Delft Jan
input: 435243 Delft Tim
{Delft Jan=1}
input: 54322 Delft Tim
input: 3453455 Delft Tim
{Delft Tim=1, Delft Jan=1}
input: 3456345 Delft Jan
input: 3456345 Delft Jan
{Delft Tim=1, Delft Jan=2}

can some one please explain to me what is going wrong?

I fixed the problem, the issue was according to the comments that I was using sc.nextLine() twice inside the loop and thats why it would miss the 1st input and read the second one.

the new correct code is this and it works just fine, so thank you guys.

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

        Scanner sc = new Scanner(System.in);
        // initialize name,line variables
        names = new ArrayList<String>();
        ales = new HashMap<String,Integer>();
        //store the names of the city and the corresponding student

        while(sc.hasNextLine() ){
            String s = removeNumbers(sc.nextLine());

            Integer c = ales.get(s);
            if(c == null){
                ales.put(s, 1);
            }
            else{
                ales.put(s, c.intValue() + 1);
            }

            if(s.equals(""))
                break;

            System.out.println(ales.toString());
        }

    }
S. N
  • 3,456
  • 12
  • 42
  • 65

4 Answers4

1

That's because you call sc.nextLine() twice inside your loop.

You should call it only once per line:

String nextLine = sc.nextLine();
String s = removeNumbers(nextLine);

...

if("".equals(nextLine)) {
    break;
}
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1
while(sc.hasNextLine() ){
        String s = removeNumbers(sc.nextLine());
        // ...
        if(sc.nextLine().equals(""))
            break;
}

You're calling sc.nextLine() twice inside the while loop. Assign nextLine() to a variable once, and then replace all the calls inside the loop with the variable. Like so:

while(sc.hasNextLine() ){
        String line = sc.nextLine();
        String s = removeNumbers(line);
        // ...
        if(line.equals(""))
            break;
}
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • the problem is that this part if(sc.nextLine().equals("")) is the stop condition without it it would never come out of the loop. – S. N Nov 18 '13 at 13:06
  • @S.N Yes, but you're getting **another line** there. That means you're asking for two lines of input in every iteration of the loop. – tckmn Nov 18 '13 at 13:07
  • yes, you are right, it fixed my problem, and I dont get stuck on the loop thanks. – S. N Nov 18 '13 at 13:09
1

It does read them. The problem is more like it reads the some inputs in the wrong places!

String s = removeNumbers(sc.nextLine()); //this line reads the 1st, 3rd, 5th... line

 if(sc.nextLine().equals("")) //this line reads the 2nd, 4th, 6th... lines

You should assign the read line into a String variable instead and use it to remember which line you read last. In fact, you already do it with s. Have you tried if(s.equals(""))?

kviiri
  • 3,282
  • 1
  • 21
  • 30
  • no, I am gonna try it now, the if part is the stop condition. Without it I cant come out of the loop. – S. N Nov 18 '13 at 13:07
1

Code sc.nextLine() is executed twice in while - loop, that's the root cause for your issue. Take care of this, and make some neccessary chance and go ahead.

  while(sc.hasNextLine() ){
        String s = removeNumbers(sc.nextLine());

        Integer c = ales.get(s);
        if(c == null){
            ales.put(s, 1);
        }
        else{
            ales.put(s, c.intValue() + 1);
        }

        if(sc.nextLine().equals(""))
            break;

        System.out.println(ales.toString());
    }
Mengjun
  • 3,159
  • 1
  • 15
  • 21