2

Hello I have an application that loads in values. The values are x,y,and a string value. I want to know how to load in a string because I only know how to o it with an integer. Take a look at this code:

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

I have

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

I know how to load in x and y but I don't know how to load in name. Please help me.

Edit----

In the file that is loaded in it is formatted like this:

x y name

example:

10 10 jack
100 500 conor

each star is represented by 1 line.

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

4 Answers 4

5

Array args[] is already String so you just need to change

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

to

String name = args[2];

And that's it.

Sign up to request clarification or add additional context in comments.

8 Comments

@user3929251 So play.s is List<Star>?
it is ArrayList(star)
the problem with this is that it only uses the name from the last line in the file so when I run it all the stars have the name of the star at the last line in the file for example
10 10 jack (nextline) 20 50 connor , all of the stars have the name connor
Show the constructor for Star object.
|
1

Try this.

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

Comments

0

you improve your code by using a StringTokenizer. Try This.

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

Comments

0

Array args[] is already String so you just need to change

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

Be aware that if you have spaces in string arguments each word will be a new argument. If you have a call like:

java -jar MyProgram 1 2 This is a sentence.

Your arg[] will be:

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

If you need the sentence to be one String you should use apostrophes:

java -jar MyProgram 1 2 "This is a sentence."

Your arg[] will be:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.