Question

My program requires users to input some data. My problem is, when it runs, it shows the first question and then immediately moves to the second question, without waiting for the user input. My friend suggested putting sc.nextLine(), it did work for the first time, but when it looped back it gave me an additional space and actually saved the space to my obj. To illustrate, when i run it, it shows:

Original: Enter position no 1: How many seats?(use can input)

With the additional sc.nextLine: Enter position no 1: (user can input) How many seats:

BUT second time it shows: Enter position no 2: (user can input) (and then space) and then the succeeding question: How many seats:

Below is my whole code in my main class. Thanks in advance.

package election;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.StringBuilder;

public class Election {

    public static void main(String[] args) {
        int maxpos;
        int count;
        int maxcan;
        int cancount;
        String name;
        int num;
        String position;
        int posnum;
        int seat;
        String party;
        int vote;

        Scanner sc = new Scanner(System.in);
        ArrayList<Candidate> list = new ArrayList<Candidate>();
        Candidate c;

        do{
            System.out.print("How many positions for this election: ");
            maxpos = sc.nextInt();
            for(count = 1; count<=maxpos; count++){
                System.out.print("Enter position no. " + count + ": ");

                position = sc.nextLine();
                sc.nextLine();
                posnum = count;
                System.out.print("How many seats: ");
                seat = sc.nextInt();
                System.out.print("Enter number of candidates: ");
                maxcan = sc.nextInt();

                for(cancount = 1; cancount<=maxcan; cancount++){
                    System.out.print("Enter candidate no. " + cancount + " name: " );
                    name = sc.nextLine();

                    num = cancount;
                    System.out.print("Enter candidate no. " + cancount + " party: " );
                    party = sc.nextLine();
                    c = new Candidate(name, num, position, posnum, seat, party);
                    list.add(c);
                }
            } 
        }while(!(count > maxpos));

        for(int i = 0; i<list.size(); i++){
            list.get(i).displayInfo();
        }
    }
}
Était-ce utile?

La solution

Whenever you use nextInt(), you need to put nextLine() after it

 System.out.print("How many seats: ");
                    seat = sc.nextInt();
                    sc.nextLine();
                    System.out.print("Enter number of candidates: ");
                    maxcan = sc.nextInt();
                    sc.nextLine();

This will consume the unconsumed \n character from stream

Autres conseils

Instead of using the Scanner, you can use readLine() of DataInputStream

 DataInputStream din=new DataInputStream(System.in);
 System.out.print("Enter number of candidates: ");
 int maxcan =Integer.parseInt(din.readLine());
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top