您好我有一个加载值的应用程序。值为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));
我知道如何加载x
和y
,但我不知道如何加载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);
}
}
}
答案 0 :(得分:5)
Array args []已经是String,所以你只需要改变
String name = Integer.parseInt(args[2]);
到
String name = args[2];
就是这样。
答案 1 :(得分:1)
试试这个。
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();
}
}
答案 2 :(得分:0)
使用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);
}
}
答案 3 :(得分:0)
Array args []已经是String,所以你只需要改变
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.
你的arg []将是:
{"1", "2", "This", "is", "a", "sentence."}
如果你需要句子是一个字符串,你应该使用撇号:
java -jar MyProgram 1 2 "This is a sentence."
你的arg []将是:
{"1", "2", "This is a sentence."}