[英]Splitting a string into multiple int form using indexOf()
This is my first post so go easy on me.这是我的第一篇文章,所以放轻松。 This IS a homework question, but I have spent about 7 hours working through various means to complete this goal and have had no success.
这是一个家庭作业问题,但我花了大约 7 个小时通过各种方式完成这个目标,但没有成功。 I am building various methods for an assignment, and I need to figure out how to split a
String
into several int
variables.我正在为赋值构建各种方法,我需要弄清楚如何将
String
拆分为多个int
变量。
Ex: given the String
"100 200 300" I need to change it to three int
of 100
, 200
, 300
.例如:给定
String
"100 200 300" 我需要将其更改为三个int
100
, 200
, 300
。 I have to use indexOf()
, and cannot use split()
or arrays.我必须使用
indexOf()
,不能使用split()
或数组。
String scores="100 200 300";
int n=scores.indexOf(" ");
String sub=scores.substring(0,n);
Integer.parseInt(sub);
This lets me get the first string "100" and parse it.这让我得到第一个字符串“100”并解析它。 However, I do not know how to continue the code so it will get the next ones.
但是,我不知道如何继续代码,所以它会得到下一个。 For my method, I will need the new
int
variables for later arguments.对于我的方法,我将需要新的
int
变量作为后面的参数。
EDIT: I think I need to use a for
loop: something like:编辑:我想我需要使用
for
循环:类似于:
for(int i=0; i<=scores.length; i++)
{//I do not know what to put here}
Joe, indexOf() is overloaded, check out this version: Joe,indexOf() 超载了,看看这个版本:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int,%20int) http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int,%20int)
public static void main(String[] args) {
String scores = "100 200 300";
ArrayList<Integer> numbers = new ArrayList<Integer>();
int n = 0;
while (n != -1) {
String sub = "";
n = scores.indexOf(" ");
if (n != -1) {
sub = scores.substring(0, n);
scores = scores.substring((n + 1));
} else {
sub = scores;
}
numbers.add(Integer.parseInt(sub));
}
for (int i : numbers) {
System.out.println("" + i);
}
}
Try something like this to loop through and add numbers to arraylist.尝试这样的事情来循环并将数字添加到数组列表。 The arraylist numbers will contain all your numbers.
arraylist 数字将包含您的所有数字。
try this:尝试这个:
String scores="100 200 300";
int offset = 0;
int space;
int score;
scores = scores.trim(); //clean the string
do
{
space= scores.indexOf(" ", offset);
if(space > -1)
{
score = Integer.parseInt(scores.substring(offset , space));
}
else
{
score = Integer.parseInt(scores.substring(offset));
}
System.out.println(score);
offset = space + 1;
}while(space > -1);
Your 'n' variable is the important part.您的 'n' 变量是重要的部分。 You get your first String by slicing from 0 to 'n', so your next string starts not at 0, but at n + " ".size()
你通过从 0 到 'n' 切片得到你的第一个字符串,所以你的下一个字符串不是从 0 开始,而是从 n + " ".size()
Ok, so here is what I have come up with: Since I needed to compare the newly parsed ints
with a different variable, as well as ensure that the amount of ints
was equal to a different variable, I created this while
loop:好的,这就是我想出的:由于我需要将新解析的
ints
与不同的变量进行比较,并确保ints
的数量等于不同的变量,因此我创建了这个while
循环:
public boolean isValid()
{
int index=0;
int initialindex=0;
int ntotal=0;
int ncount=0;
boolean flag=false;
while (index!=-1)
{
index=scores.indexOf(" ");
String temp=scores.substring(initialindex,index);
int num=Integer.parseInt(temp);
ntotal+=num;
ncount++;
initialindex=index;
}
if (ntotal==total && ncount==count)
{
flag=true;
}
return flag;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.