扫描仪system.in,不读取第1,第3,第5等输入

时间:2013-11-18 13:00:46

标签: java java.util.scanner

我有以下代码,我想通过system.in给出输入,但是,程序滑动frist输入,但是它读取了第一个,它跳过第三个输入,但它读取第四个,所以上。我无法弄清楚这个问题。 这是我的代码:

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());
        }

    }

}

所以这是我的输入和输出:

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}

有人可以向我解释出现了什么问题吗?

我解决了问题,问题是根据我在循环中使用sc.nextLine()两次的注释,这就是为什么它会错过第一个输入并读取第二个输入。

新的正确代码就是这样,它运行得很好,谢谢你们。

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());
        }

    }

4 个答案:

答案 0 :(得分:1)

那是因为你在循环中呼叫sc.nextLine() 两次

你应该每行只调用一次:

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

...

if("".equals(nextLine)) {
    break;
}

答案 1 :(得分:1)

while(sc.hasNextLine() ){
        String s = removeNumbers(sc.nextLine());
        // ...
        if(sc.nextLine().equals(""))
            break;
}

您在sc.nextLine()循环内调用while两次。将nextLine()分配给变量一次,然后用变量替换循环内的所有调用。像这样:

while(sc.hasNextLine() ){
        String line = sc.nextLine();
        String s = removeNumbers(line);
        // ...
        if(line.equals(""))
            break;
}

答案 2 :(得分:1)

确实阅读了它们。问题更像是在错误的地方读取一些输入!

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

您应该将读取行分配到String变量中,并使用它来记住您最后读取的行。实际上,您已经使用s执行此操作。你试过if(s.equals(""))吗?

答案 3 :(得分:1)

代码sc.nextLine()在while循环中执行两次,这是导致问题的根本原因。照顾好这一点,并提供一些必要的机会并继续前进。

  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());
    }
相关问题