我必须输入一个带有数字ex:1,2,3,4,5的字符串。 这是输入的一个示例,然后我必须将它放在一个INT数组中,这样我就可以对它进行排序,但是它的工作方式不行。
package array;
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
String input;
int length, count, size;
Scanner keyboard = new Scanner(System.in);
input = keyboard.next();
length = input.length();
size = length / 2;
int intarray[] = new int[size];
String strarray[] = new String[size];
strarray = input.split(",");
for (count = 0; count < intarray.length ; count++) {
intarray[count] = Integer.parseInt(strarray[count]);
}
for (int s : intarray) {
System.out.println(s);
}
}
}
答案 0 :(得分:51)
对于输入1,2,3,4,5
,整数数学中的输入长度为9. 9/2 = 4
,因此您只存储前四个变量,而不是全部5个。
即使你修正了它,如果你传入10,11,12,13
如果您使用1,2,3,4,50
作为输入,它会起作用(偶然):奇怪的是: - )
你做这样的事情会好得多
String[] strArray = input.split(",");
int[] intArray = new int[strArray.length];
for(int i = 0; i < strArray.length; i++) {
intArray[i] = Integer.parseInt(strArray[i]);
}
为了将来参考,当您收到错误时,我强烈建议您将其与代码一起发布。您可能没有人可以随时使用jdk编译代码来调试它! :)
答案 1 :(得分:18)
Java 8提供了基于流的手动迭代替代方法:
int[] intArray = Arrays.stream(input.split(","))
.mapToInt(Integer::parseInt)
.toArray();
如果输入可能包含无法转换为整数的字符序列,请准备好捕获NumberFormatException
。
答案 2 :(得分:2)
让我们考虑您输入“1,2,3,4”。
这意味着输入的长度为7.所以现在写入size = 7/2 = 3.5。但是,由于大小为int
,它将四舍五入为3.简而言之,您将失去1个值。
如果您重写下面的代码,它应该有效:
String input;
int length, count, size;
Scanner keyboard = new Scanner(System.in);
input = keyboard.next();
length = input.length();
String strarray[] = input.split(",");
int intarray[] = new int[strarray.length];
for (count = 0; count < intarray.length ; count++) {
intarray[count] = Integer.parseInt(strarray[count]);
}
for (int s : intarray) {
System.out.println(s);
}
答案 3 :(得分:0)
您正在进行整数除法,因此如果用户碰巧输入了奇数个输入,您将失去正确的长度 - 这是我注意到的一个问题。因此,当我使用输入'1,2,3,4,5,6,7'运行代码时,我的最后一个值被忽略...
答案 4 :(得分:0)
String input = "2,1,3,4,5,10,100";
String[] strings = input.split(",");
int[] numbers = new int[strings.length];
for (int i = 0; i < numbers.length; i++)
{
numbers[i] = Integer.parseInt(strings[i]);
}
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
答案 5 :(得分:0)
稍微改变你做事的顺序。你似乎没有特别的理由除以2。
虽然您的应用程序不保证半冒号分隔变量的输入字符串,但您可以轻松地这样做:
package com;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// Good practice to initialize before use
Scanner keyboard = new Scanner(System.in);
String input = "";
// it's also a good idea to prompt the user as to what is going on
keyboardScanner : for (;;) {
input = keyboard.next();
if (input.indexOf(",") >= 0) { // Realistically we would want to use a regex to ensure [0-9],...etc groupings
break keyboardScanner; // break out of the loop
} else {
keyboard = new Scanner(System.in);
continue keyboardScanner; // recreate the scanner in the event we have to start over, just some cleanup
}
}
String strarray[] = input.split(","); // move this up here
int intarray[] = new int[strarray.length];
int count = 0; // Declare variables when they are needed not at the top of methods as there is no reason to allocate memory before it is ready to be used
for (count = 0; count < intarray.length; count++) {
intarray[count] = Integer.parseInt(strarray[count]);
}
for (int s : intarray) {
System.out.println(s);
}
}
}
答案 6 :(得分:0)
List<String> stringList = new ArrayList<String>(Arrays.asList(arr.split(",")));
List<Integer> intList = new ArrayList<Integer>();
for (String s : stringList)
intList.add(Integer.valueOf(s));
答案 7 :(得分:-1)
这样的事情:
public static void main(String[] args) {
String N = "ABCD";
char[] array = N.toCharArray();
// and as you can see:
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
}