简体   繁体   English

用Java扫描行

[英]Scanning lines in Java

What's wrong with the code? 代码有什么问题? It won't let me write the input for each one of them..I wanted to read how many editions, the year and city of each edition, and it jumps to the loop and won't let me put input. 它不会让我为每个版本写输入。.我想阅读每个版本有多少个版本,年份和所在的城市,但它会跳到循环中,而不会让我输入输入。

public class Competition {

    private static ArrayList<Edition> editions = new ArrayList<Edition>();
    private static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {

        String newCity = null;
        int newYear = 0;
        int itineration = 0;
        int numberEditions = 0;
        Edition newEdition = new Edition(newCity, newYear);

        System.out.println("How many editions would you like to create?");
        numberEditions = scan.nextInt();

        while (itineration < numberEditions) {
            System.out.println("\nEnter the city and year of the Edition you would"
                    + " like to create: ");

            System.out.println("Year: ");
            newYear = scan.nextInt();
            System.out.println("City: ");
            newCity = scan.nextLine();

            editions.add(newEdition);
            itineration++;
        }

        for (int index = 0; index < editions.size(); index++) {
            System.out.println("Music Festival'" + editions.get(index).getYear()
                    + ", "
                    + editions.get(index).getCity());

        }
    }
}

You need to consume extra line after reading year. 读完一年后,您需要占用多余的行。 and also you need to create new Edition for each input and add it to the list in while loop: 并且您还需要为每个输入创建新的Edition并将其添加到while循环中的列表中:

while (itineration < numberEditions) {
    System.out.println("\nEnter the city and year of the Edition you would"
            + " like to create: ");

    System.out.println("Year: ");
    newYear = scan.nextInt();

    scan.nextLine();

    System.out.println("City: ");
    newCity = scan.nextLine();

    Edition newEdition = new Edition(newCity, newYear);
    editions.add(newEdition);
    itineration++;
}

Hope it helps 希望能帮助到你

You need to create new instance of Edition inside the while loop. 您需要在while循环中创建Edition新实例。

    while (itineration < numberEditions) {
        System.out.println("\nEnter the city and year of the Edition you would"
                + " like to create: ");

        System.out.println("Year: ");
        newYear = scan.nextInt();
        System.out.println("City: ");
        newCity = scan.nextLine();

       Edition newEdition = new Edition(newCity, newYear);
        editions.add(newEdition);
        itineration++;
    }

There are some problems with the Scanner class if you don't know how to use it. 如果您不知道如何使用Scanner类,则会遇到一些问题。 Some people suggest using a buffered reade r. 有人建议使用缓冲的reader I would recommend researching both objects before using either one. 我建议在使用任何一个之前先研究两个对象。

You have made 2 errors. 您犯了2个错误。

  1. Make new Edition instances inside loop 在循环内创建新的Edition实例
  2. use Scanner.next() to get the input String . 使用Scanner.next()获取输入String Scanner.nextLine() is invalid. Scanner.nextLine()无效。

Working code: 工作代码:

 while (itineration < numberEditions)
        {
            System.out.println("\nEnter the city and year of the Edition you would" + " like to create: ");

            System.out.println("Year: ");
            newYear = scan.nextInt();
            System.out.println("City: ");
            newCity = scan.next();

            editions.add(new Edition(newCity, newYear));
            itineration++;
        }

为Scanner创建新对象。现在就可以输入值了。

newCity = new Scanner(System.in).nextLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM