java如何加载字符串值

java如何加载字符串值,java,Java,您好,我有一个加载值的应用程序。这些值是x、y和字符串值。我想知道如何装入字符串,因为我只知道如何用整数对其进行运算。请看下面的代码: public static void loadStars() { try { BufferedReader bf = new BufferedReader(new FileReader ("files/cards.txt")); for (int i = 0; i < 10; i++) {

您好,我有一个加载值的应用程序。这些值是x、y和字符串值。我想知道如何装入字符串,因为我只知道如何用整数对其进行运算。请看下面的代码:

    public static void loadStars() {
    try {
        BufferedReader bf = new BufferedReader(new FileReader   ("files/cards.txt"));
        for (int i = 0; i < 10; i++) {
            String line;
            while ((line = bf.readLine()) != null) {

                String[] args = line.split(" ");

                int x = Integer.parseInt(args[0]);
                int y = Integer.parseInt(args[1]);
                String name = Integer.parseInt(args[2]);

                play.s.add(new Star(x,y,name));


            }
        }

        bf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
我知道如何加载
x
y
,但我不知道如何加载
name
。 请帮帮我

编辑--

在加载的文件中,其格式如下:

x y name
例如:

10 10 jack
100 500 conor
每颗星由一条线表示

public class Star extends BasicStar {

    private Image img;

    public Star(int x, int y,String starname) {
        this.x = x;
        this.y = y;
        this.starname = starname;

        r = new Rectangle(x, y, 3, 3);
    }

    public void tick() {

        if (r.contains(Comp.mx, Comp.my) && Comp.ml) {
            remove = true;
        }
    }

    public void render(Graphics g) {
        if (!displaySolar) {

            ImageIcon i2 = new ImageIcon("res/planets/star.png");
            img = i2.getImage();
            g.drawImage(img, x, y, null);
        }
    }
}

数组args[]已经是字符串,因此您只需更改

String name = Integer.parseInt(args[2]);

就这样。

试试这个

public static void loadStars() {
        try {
            BufferedReader bf = new BufferedReader(new FileReader   ("files/cards.txt"));
            for (int i = 0; i < 10; i++) {
                String line;
                while ((line = bf.readLine()) != null) {

                    String[] args = line.split(" ");

                    int x = Integer.parseInt(args[0]);
                    int y = Integer.parseInt(args[1]);
                    String name = args[2]; // args array contain string, no need of conversion.

                    play.s.add(new Star(x,y,name));


                }
            }

        bf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
publicstaticvoidloadstars(){
试一试{
BufferedReader bf=新的BufferedReader(新文件读取器(“files/cards.txt”);
对于(int i=0;i<10;i++){
弦线;
而((line=bf.readLine())!=null){
字符串[]args=line.split(“”);
intx=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
String name=args[2];//args数组包含字符串,无需转换。
play.s.add(新星(x,y,name));
}
}
bf.close();
}捕获(例外e){
e、 printStackTrace();
}
}

您可以使用StringTokenizer改进代码。试试这个

    public static void loadStars()
        throws IOException {
    String line;
    BufferedReader bf = new BufferedReader(new FileReader   ("files/cards.txt"));
    try
    {
    while((line = bf.readLine()) != null)
    {
        if (line == null || line.trim().isEmpty())
            throw new IllegalArgumentException(
                    "Line null!");

        StringTokenizer tokenizer = new StringTokenizer(line, " ");
        if (tokenizer.countTokens() < 3)
            throw new IllegalArgumentException(
                    "Token number not valid (<= 3)");
        int x, y;
        String xx = tokenizer.nextToken(" ").trim();
        String yy = tokenizer.nextToken(" ").trim();
        String name = tokenizer.nextToken(" ").trim();
        try
        {
        x = Integer.parseInt(xx);
        }catch(ParseException e){throw new IllegalArgumentException(
                "Number format not valid!");}
        try
        {
        y = Integer.parseInt(yy);
        }catch(ParseException e){throw new IllegalArgumentException(
                "Number format not valid!");}
        play.s.add(new Star(x,y,name));
    }
    } catch (NoSuchElementException | NumberFormatException | ParseException e) {
        throw new IllegalArgumentException(e);
    }
}
publicstaticvoidloadstars()
抛出IOException{
弦线;
BufferedReader bf=新的BufferedReader(新文件读取器(“files/cards.txt”);
尝试
{
而((line=bf.readLine())!=null)
{
if(line==null | | line.trim().isEmpty())
抛出新的IllegalArgumentException(
“行空!”;
StringTokenizer tokenizer=新的StringTokenizer(行“”);
if(tokenizer.countTokens()<3)
抛出新的IllegalArgumentException(

“令牌号无效(Array args[]已经是字符串,因此您只需更改

String name = Integer.parseInt(args[2]);

// If you are using this values anywhere else you'd do this so de GC can collects the arg[] array
String name = new String(args[2]);
请注意,如果字符串参数中有空格,则每个单词都将是一个新参数。如果有如下调用:

java -jar MyProgram 1 2 This is a sentence.
java -jar MyProgram 1 2 "This is a sentence."
您的arg[]将是:

{"1", "2", "This", "is", "a", "sentence."}
{"1", "2", "This is a sentence."}
如果需要将句子变成一个字符串,则应使用撇号:

您的arg[]将是:

{"1", "2", "This", "is", "a", "sentence."}
{"1", "2", "This is a sentence."}

@user3929251那么play.s是List吗?它是ArrayList(star)的问题是它只使用文件最后一行的名称,所以当我运行它时,所有的星星在文件的最后一行都有星星的名称,例如10 10 jack(nextline)20 50 connor,所有的星星都有名称connorShow star对象的构造函数。