【问题标题】:Exception in thread - java.util.InputMismatchException线程中的异常 - java.util.InputMismatchException
【发布时间】:2012-12-07 04:41:14
【问题描述】:

我正在尝试读取字符串并对其进行排序,但遇到了错误。我按照第一个答案的说法修改了程序,我在运行中走得更远,但它不会完成。我是初学者,所以请清楚要更改的内容。

我收到此错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at hw05.Strings.main(Strings.java:32)
Java Result: 1

The line with error is starred.

package hw05;

/**
 *Demonstrates selectionSort on an array of strings.
 * 
 * @author Maggie Erwin
 */
import java.util.Scanner;

public class Strings {

    // --------------------------------------------
    // Reads in an array of integers, sorts them,
    // then prints them in sorted order.
    // --------------------------------------------
    public static void main(String[] args) {

        String[] stringList;
        Integer[] intList;
        int size;

        Scanner scan = new Scanner(System.in);

        System.out.print("\nHow many strings do you want to sort? ");
        size = scan.nextInt();
        int sizeInInt = Integer.valueOf(size);
        stringList = new String[sizeInInt];
        intList= new Integer[sizeInInt]; // Initialize intList

        System.out.println("\nEnter the strings...");
        for (int i = 0; i < size; i++) {
                intList[i] = scan.nextInt();
            }
        Sorting.selectionSort(stringList);

        System.out.println("\nYour strings in sorted order...");
        for (int i = 0; i < size; i++) {
                System.out.print(stringList[i] + " ");
            }
        System.out.println();

    **}**

【问题讨论】:

  • 您必须在使用它之前实际创建数组。
  • 您在哪里创建了整数数组对象?将数据存储在 Integer 数组中,而实际上是在对 String 数组进行排序,这似乎很有趣。

标签: java string sorting polymorphism


【解决方案1】:

您尚未初始化 intList 变量,例如您已初始化的 stringList

String[] stringList;
Integer[] intList;
....
stringList = new String[sizeInInt];  //you initialized it in your code
intList = new Integer[sizeInInt];    // missing in your code

【讨论】:

    【解决方案2】:

    错误解释了一切。您需要先初始化sizeInInt。所以它看起来像这样:

    public static void main(String[] args) {
    
            String[] stringList;
            Integer[] intList;
            int size;
    
            Scanner scan = new Scanner(System.in);
    
            System.out.print("\nHow many strings do you want to sort? ");
            size = scan.nextInt();
            int sizeInInt = Integer.valueOf(size);
            stringList = new String[sizeInInt];
            intList= new Integer[sizeInInt]; // Initialize intList
    
            System.out.println("\nEnter the strings...");
            for (int i = 0; i < size; i++) {
                    **intList**[i] = scan.nextInt();
                }
            Sorting.selectionSort(stringList);
    
            System.out.println("\nYour strings in sorted order...");
            for (int i = 0; i < size; i++) {
                    System.out.print(stringList[i] + " ");
                }
            System.out.println();
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多