gpt4 book ai didi

java 如何加载字符串值

转载 作者:行者123 更新时间:2023-12-02 04:30:06 24 4
gpt4 key购买 nike

您好,我有一个加载值的应用程序。这些值为 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();
}
}

我有

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

我知道如何加载xy,但我不知道如何加载name。请帮助我。

编辑----

加载的文件格式如下:

x y name

示例:

10 10 jack
100 500 conor

每颗星星由 1 条线表示。

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);
}
}
}

最佳答案

Array args[] 已经是字符串,所以你只需要更改

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

String name = args[2];

就是这样。

关于java 如何加载字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31203925/

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