[英]StringTokenizer array
Java. Java的。 So I want to input an array of integers and then print out the max value using StringTokenizer. 所以我想输入一个整数数组,然后使用StringTokenizer打印出最大值。 I know how to do this using integers, but when I try to do it with an array the String to int command (Integer.parseInt) fails me - cannot convert from int to int[]. 我知道如何使用整数来执行此操作,但是当我尝试使用数组来执行此操作时,将字符串转换为int命令(Integer.parseInt)失败了-无法从int转换为int []。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String strTmp;
StringTokenizer strTok;
while ((strTmp = in.readLine()) != null && strTmp.length() != 0) {
strTok = new StringTokenizer(strTmp);
int[] c;
c = Integer.parseInt(strTok.nextToken());
int max = 0;
for (int i = 0; i < c.length; i++) {
if (c[i] > max) {
max = c[i];
}
}
System.out.println(max);
}
}
}
How do I go about solving this or are there other commands I should be using? 我该如何解决该问题,或者应该使用其他命令?
You seem to be confused about whether you actually want an array or not. 您似乎对实际上是否需要数组感到困惑。 You're parsing a single value , but trying to assign that int
to c
which is an array variable. 您正在解析一个值 ,但尝试将该int
分配给c
,它是一个数组变量。 You don't really need one, as you only need to remember the current maximum, and the value you've just parsed: 您实际上并不需要一个,因为您只需要记住当前的最大值以及刚刚解析的值即可:
int max = Integer.MIN_VALUE;
while (strTok.hasMoreTokens()) {
int value = Integer.parseInt(strTok.nextToken());
if (value > max) {
max = value;
}
}
If you really want an array, then you'll need to know how many tokens there are before you parse them, which is tricky. 如果您真的想要一个数组,那么在解析它们之前,您需要知道有多少个令牌,这很棘手。
Another alternative would be to create a List<Integer>
: 另一种选择是创建一个List<Integer>
:
// First parse everything...
List<Integer> values = new ArrayList<>();
while (strTok.hasMoreTokens()) {
values.add(Integer.parse(strTok.nextToken());
}
// Then you can find the maximum value in the list
Personally I prefer the first approach, if you only need to find the maximum value. 就个人而言,如果您只需要找到最大值,则我倾向于第一种方法。
int[] c;
c = Integer.parseInt(strTok.nextToken());
You are trying to assign a single Integer object to an array of int
. 您试图将单个Integer对象分配给int
数组。
Rather, use int[index] = Integer.parseInt(strTok.nextToken());
而是使用int[index] = Integer.parseInt(strTok.nextToken());
Integer.parseInt(String)
returns an integer, not an Integer array
. Integer.parseInt(String)
返回一个整数,而不是Integer array
。 You have to store the return value Integer in an int variable like 您必须将返回值Integer存储在一个int变量中,例如
int i = Integer.parseInt(String);
You have to iterate the integer array and set it's index value to the returned integer value 您必须迭代整数数组并将其索引值设置为返回的整数值
for (int i = 0; i < c.length; i++) {
c[i] = Integer.parseInt(strTok.nextToken());
if (c[i] > max) {
max = c[i];
}
}
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.