简体   繁体   中英

Having trouble getting user input into array without getting errors

I am creating a roommate bill splitter program and do not know what I am doing wrong. It gets hung up after asking what the roommates names are. Output is below code.

package roomBillSplit;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Welcome to Roommate Bill Splitter!\n");

        //Get Residence Name from User
        System.out.print("Please Enter Name of Place or Address:  ");
        String place = input.nextLine();

        roommates(place);           
        bills(place);
        input.close();
    }

    public static void roommates(String place) {
        int numRoom, i;

        Scanner input = new Scanner(System.in);

        //Get # of Roommates at Residence
        System.out.print("How Many Total People Reside at " + place + ":  ");
        numRoom = input.nextInt();

        String[] roommates = new String[numRoom];
        //ArrayList<String> roommates = new ArrayList<String>(5);

        //Get Names of Roommates
        for(i = 0; i < roommates.length; i++) {
            System.out.print("What is Person Number " + (i + 1) + "'s Name:  ");
            if(input.hasNext()) {
                roommates[i] = input.nextLine();
                input.next();
            }
        }

        for(i = 0; i < roommates.length; i++) {
                System.out.println(roommates[i]);
            }
            input.close();
        }
    }

    public static void bills(String place) {
        int numBills, i;

        Scanner input = new Scanner(System.in);

        //Get # of Bills Split Between Roommates
        System.out.print("What is the Total Number of Bills to be Split at " + place + ":  ");
        numBills = input.nextInt();

        String[] bills = new String[numBills];

        //Get Names of Bills
        for(i = 0; i < bills.length; i++) {
            System.out.print("Please List Bill Number " + (i + 1) + ":  ");
            if(input.hasNext()) {
                bills[i] = input.nextLine();
                input.next();
            }
        }

        for(i = 0; i < bills.length; i++) {
            System.out.println(bills[i]);
        }

        int amount[] = new int[numBills];

        //Get Amount of Each Bill
        for(i = 0; i < bills.length; i++) {
            System.out.print("What is the Total Amount of the " + bills[i] + " Bill:  $");
            if(input.hasNextInt()) {
                amount[i] = input.nextInt(); 
                input.next();
            }
        }

        input.close();

        for(i = 0; i < amount.length; i++) {
            System.out.print(bills[i]);
            System.out.print("\t$");
            System.out.println(amount[i]);
        }  
    }
}

Output:

Welcome to Roommate Bill Splitter!

Please Enter Name of Place or Address: 1212 Main

How Many Total People Reside at 1212 Main: 2

What is Person Number 1's Name: a

What is Person Number 2's Name: b

Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at roomBillSplit.Main.bills(Main.java:62)

at roomBillSplit.Main.main(Main.java:19)

What is the Total Number of Bills to be Split at 1212 Main:

Your program waiting for user input. If you press any key it will resume. This is because

You have extra input.next() in remove that line it will work.

//Get Names of Roommates
    for(i = 0; i < roommates.length; i++){
        System.out.print("What is Person Number " + (i + 1) + "'s Name:  ");
            if(input.hasNext()){
               roommates[i] = input.nextLine();
               input.next();
            }
    }

UPDATE:

You don't need to use input.hasNext() and input.next() because input.nextLine() will block for the user input.

When you close the Scanner at the end of your roommates method, you also close System.in, so you can't scan from it any more when you need it in your bills method, and that's why your new Scanner there doesn't work.

Pass the Scanner you create in your main method as an argument to your roommates and bills method, instead of having those methods create a new Scanner, and remove all of the input.close() statements except for the one in main.

Edited to add: It will fix your other problem if you change how you are reading from the scanner inside of your loops to look like this:

...
roommates[i] = input.next();
input.nextLine();
...
bills[i] = input.next();
input.nextLine();
...
amount[i] = input.nextInt(); 
input.nextLine();
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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