gpt4 book ai didi

java - 分别将字符串数组存储在指定变量中

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:00 24 4
gpt4 key购买 nike

我是 JAVA 的初学者,我正在编写这段代码,该代码应该接收一个字符串数组并将每个字符串(数组元素)分别存储在指定的变量中。但它只存储第一个元素。下面是代码:

package ontoretrive;

import org.apache.commons.lang3.ArrayUtils;

public class j {
static String sss= "male,O+,45,saudi,brain_diseases";
static int size =sss.length();

static String male="";
static String blood="";
static String age="";
static String nat="";
static String dis="";
static char temp;
static void func (){
char[] charArray = sss.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);
int count=0;
int x=0;
while (x< size){
temp =charObjectArray[x];
while(temp!=','&&count==0){
male=male+temp;
x++;
temp =charObjectArray[x];}
x++;
temp =charObjectArray[x];

count++;

while(temp!=','&&count==1){
blood=blood+temp;
x++;
temp =charObjectArray[x];}
x++;
temp =charObjectArray[x];
count++;

while(temp!=','&&count==2){
age=age+temp;
x++;
temp =charObjectArray[x];
}x++;
temp =charObjectArray[x];
count++;

while(temp!=','&&count==3){
nat=nat+temp;
x++;
temp =charObjectArray[x];
}x++;
temp =charObjectArray[x];
count++;


while(temp!=','&&count==4){
dis=dis+temp;
x++;
//temp =charObjectArray[x];
}
x++;
// temp =charObjectArray[x];
count++; }

System.out.println(male);//end while1
System.out.println(blood);//end while1
System.out.println(age);//end while1
System.out.println(nat);//end while1
System.out.println(dis);//end while1

}//end func
public static void main(String[] args) {
System.out.println("dis1");
func();
System.out.println(male);//end while1

//System.out.println("dis3");


}
} //end class

最佳答案

public class j {

static String sss = "male,O+,45,saudi,brain_diseases";
static int size = sss.length();

//I suggest you not to give them a start value or set them as null
static String male = "";
static String blood = "";
static String age = "";
static String nat = "";
static String dis = "";

static void func() {

//it uses the "," character to breaks the string sss into pieces
//so became into "male","O+","45","saudi","brain_diseases"
String[] pieces = sss.split(",");

//pieces[0] is the first piece so = "male"
//pieces[1] is the second "O+" and so on
male = pieces[0];
blood = pieces[1];
age = pieces[2];
nat = pieces[3];
dis = pieces[4];

System.out.println(male);
System.out.println(blood);
System.out.println(age);
System.out.println(nat);
System.out.println(dis);

}

public static void main(String[] args) {

func();
}
}

关于java - 分别将字符串数组存储在指定变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36747075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com