【问题标题】:Having trouble getting user input into array without getting errors无法将用户输入输入数组而不会出现错误
【发布时间】:2015-04-29 19:51:04
【问题描述】:

我正在创建一个室友账单拆分程序,但不知道我做错了什么。在询问室友的名字后,它被挂断了。输出在代码下方。

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

输出:

欢迎来到室友 Bill Splitter!

请输入地名或地址:1212 Main

1212 Main 共有多少人居住:2

第 1 个人的名字是什么:a

第 2 个人的名字是什么:b

线程“主”java.util.NoSuchElementException 中的异常

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

在 java.util.Scanner.next(未知来源)

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

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

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

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

在 1212 Main 要拆分的票据总数是多少:

【问题讨论】:

  • 不要关闭Scanner
  • 好的,我被告知我需要关闭扫描仪,但它现在似乎可以工作,直到我到达要求我输入账单金额的部分。然后它似乎经历了无限循环或其他东西。请输入地名或地址:1212 1212 的总人数:2 第 1 个人的姓名是什么:g 第 2 个人的姓名是什么:d 1212 要拆分的账单总数是多少:3 请列出帐单编号 1:租金 请列出帐单编号 2:ele 请列出帐单编号 3:电缆 帐单的总金额是多少:$200
  • 此外,您正在检查一个字符串标记,然后拉出整行和另一个标记。我希望当您尝试未选中的 nextList()next() 时根本没有数据。

标签: java


【解决方案1】:

您的程序正在等待用户输入。如果您按任意键,它将恢复。这是因为

你有额外的input.next() 删除那行它会起作用。

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

更新:

您不需要使用input.hasNext()input.next(),因为input.nextLine() 会阻止用户输入。

【讨论】:

  • 当我删除 input.next() 时,它会要求第一个用户输入,然后它只会在同一行打印“输入用户输入 #2”,而不是等待用户响应并移动到下一个“输入用户输入#3”。它确实等待最后一个用户输入,但跳过第二个。在第一次用户输入输入账单金额后,它仍然挂断。在询问金额时,它也不会显示用户输入的账单名称。
  • 非常感谢...我已经为此炖了一个星期了!如果我对相同的代码还有其他问题,我应该在这里发布还是创建新线程?
  • @tjgpp9 你创建一个新的。如果它解决了您的问题,请投票并接受我的回答。这让我感到高兴.. :) 谢谢
【解决方案2】:

当您在室友方法结束时关闭扫描仪时,您也关闭了 System.in,因此当您在账单方法中需要它时,您无法再从它进行扫描,这就是您的新扫描仪没有的原因不行。

将您在 main 方法中创建的 Scanner 作为参数传递给您的 roommates 和 bills 方法,而不是让这些方法创建一个新的 Scanner,并删除除 main 中的所有 input.close() 语句。

编辑添加: 如果您将循环内的扫描仪读取方式更改为如下所示,它将解决您的其他问题:

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

【讨论】:

  • 是否需要input.nextInt();最后一个?
  • 每一个中的最后一行需要输入。nextLine() 以清除用户按回车键的换行符。
猜你喜欢
  • 2021-12-08
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多