如何在数组中打印选定的名称?

velaa5lx  于 2021-07-11  发布在  Java
关注(0)|答案(4)|浏览(294)

如何在数组中打印选定的名称?我想打印在数组中输入的名称并单独打印,但当我尝试运行代码时,它会显示:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Main.main(Main.java:17)

这是我的密码:

Scanner in = new Scanner(System.in);

  int numOfLoop = in.nextInt(); //number of loops I want.
  String[] name = new String[numOfLoop]; //size of the array is depend on how many loop I want.

  //Getting the names using for loop.
  for(int i = 0; i < numOfLoop; i++) {

   name[i] = in.nextLine();

}

  int num = in.nextInt(); //The name I want to print depend on what number I enter here.

  //Reading the array one by one to print the name I want.
  for(int i = 0; i <numOfLoop; i++) {

   if(name[i] == name[num]) {

   System.out.println(name[i]);

  }

}

输入:

6 //How many loop and size of array I want.
john
mark
kevin
tesia
arthur
cody
5 //what ever is in array[5] will be printed.

预期产量: cody

pjngdqdw

pjngdqdw1#

我以前也遇到过这个问题,好像当你从 nextInt() 扫描仪示例未读取 \n 字符前进到 nextLine() .
只是添加 in.nextLine(); 在for循环修复问题之前。
您的错误来自这样一个事实:数组中的第一个条目被设置为空字符串,而您输入的姓氏被读取到您通常会输入第二个数字的位置,因此 nextInt() 因为它得到一个 String 而不是一个 int .

6mw9ycah

6mw9ycah2#

要解决的代码片段中有几个典型的缺陷: InputMismatchException -因为在调用
nextInt name[i] == name[num] --字符串比较无效,应为 name[i].equals(name[num]) 遗失支票 num < numOfLoop --否则,BoundsException的排列是可能的
固定代码如下所示:

Scanner in = new Scanner(System.in);

System.out.println("Input the number of names: ");
int numOfLoop = in.nextInt(); //number of loops I want.
in.nextLine();  // skip remaining line
String[] name = new String[numOfLoop]; //size of the array is depend on how many loop I want.

System.out.println("Input the names, one per line: ");
//Getting the names using for loop.
for (int i = 0; i < numOfLoop; i++) {
    name[i] = in.nextLine();
}

System.out.println("Input the index of the name to print: ");
int num = in.nextInt(); //The name I want to print depend on what number I enter here.

//Reading the array one by one to print the name I want.
if (num >= 0 && num < numOfLoop) {
    System.out.println("Looking for name: " + name[num]);
    for (int i = 0; i <numOfLoop; i++) {
        if(name[i].equals(name[num])) {
            System.out.println(name[i] + " at index=" + i);
        }
    }
} else {
    System.out.println("Invalid index, cannot be greater or equal to " + numOfLoop);
}

样本输出:

Input the number of names: 
5
Input the names, one per line: 
john
jeff
joan
john
jake
Input the index of the name to print: 
0
Looking for name: john
john at index=0
john at index=3
nr7wwzry

nr7wwzry3#

你不需要第二个循环。

你只需要检查一下 if (num >= 0 && num < numOfLoop) 并显示 name[num] 或错误消息。

import java.util.Scanner;

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

        int numOfLoop = Integer.parseInt(in.nextLine()); // number of loops I want.
        String[] name = new String[numOfLoop]; // size of the array is depend on how many loop I want.

        // Getting the names using for loop.
        for (int i = 0; i < numOfLoop; i++) {
            name[i] = in.nextLine();
        }

        int num = Integer.parseInt(in.nextLine()); // The name I want to print depend on what number I enter here.
        if (num >= 0 && num < numOfLoop) {
            System.out.println(name[num]);
        } else {
            System.out.println("Invalid index.");
        }
    }
}

另外,使用 Integer.parseInt(in.nextLine()) 而不是 in.nextInt() 因为scanner中提到的原因,在使用next()或nextfoo()之后跳过nextline()?
示例运行:

5
Johny
Arvind
Kumar
Avinash
Stackoverflow
3
Avinash
vlurs2pr

vlurs2pr4#

Scanner in = new Scanner(System.in);
    int numOfLoop = in.nextInt(); //number of loops I want.
    String[] name = new String[numOfLoop]; //size of the array is depend on how many loop I want.

    for (int i=0; i<name.length; i++){
        String names = in.next();
        name[i] = names;
    }
    System.out.println("The names array: " + Arrays.toString(name));

    for(int index=0;index<name.length;index++) {
        System.out.print("Enter an index you want to print: ");
        index = in.nextInt();
        System.out.println("index " + index + " is:  " + name[index-1]);
    }

相关问题