【发布时间】: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