0

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

        }
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599

5 Answers5

1

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:

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

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • Thank you @Sanjeev ! That's what I needed. I can't give rep because I don't have enough, otherwise I would give! Thanks a ton! – user3672822 Jun 02 '14 at 13:13
0

You need to create new instance of Edition inside the while loop.

    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++;
    }
Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38
0

There are some problems with the Scanner class if you don't know how to use it. Some people suggest using a buffered reader. I would recommend researching both objects before using either one.

Community
  • 1
  • 1
Kyte
  • 834
  • 2
  • 12
  • 27
0

You have made 2 errors.

  1. Make new Edition instances inside loop
  2. use Scanner.next() to get the input String. Scanner.nextLine() is invalid.

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++;
        }
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
Asheesh Gupta
  • 318
  • 3
  • 4
0

create new Object for Scanner.now you can put the value.

newCity = new Scanner(System.in).nextLine();
hari
  • 1,874
  • 1
  • 16
  • 10