Java How to load string value

Hello I have an application that loads into values. Values: x, y, and string value. I want to know how to load in a string because I only know how to do 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 into x

and y

, but I don't know how to load it into name

. Please help me.

Edit ----

The download file is formatted as follows:

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

      

+3


source to share


4 answers


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

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

      

to



String name = args[2];

      

What is it.

+7


source


Try it.



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

      

+1


source


you enhance your code with StringTokenizer. Try it.

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

      

0


source


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

      

Remember that if there are spaces in the string arguments, each word will be a new argument. If you have a call:

java -jar MyProgram 1 2 This is a sentence.

      

Your arg [] will be:

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

      

If you need a sentence to be one String, you must use apostrophes:

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

      

Your arg [] will be:

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

      

0


source







All Articles