0

Hello i want to use strings out of a while loop for further operations.. but how do i "extract" them ot of the while loop?

I want to use sollWert1,sollWert2,sollWert3,sollWert4.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadText {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BufferedReader br = null;

        String file_at_path_to_desktop = System.getProperty("user.home") + "\\" + "Desktop" + "\\" + "SollAuftrag.txt";

        try {
            br = new BufferedReader(new FileReader(new File(file_at_path_to_desktop)));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(";");
                String sollWert1= parts[0];
                String sollWert2= parts[1];
                String sollWert3= parts[2];
                String sollWert4= parts[3];
                System.out.println("Wert1: " + sollWert1);
                System.out.println("Wert2: " + sollWert2);
                System.out.println("Wert3: " + sollWert3);
                System.out.println("Wert4: " + sollWert4);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
15
  • 1
    Do you want the strings from a particular iteration through the loop, some kind of collection, or some kind of concatenation? What exactly are you looking for? Commented Jun 30, 2017 at 15:12
  • Store them in a list of some custom object. Commented Jun 30, 2017 at 15:12
  • Move the declarations out of the loop. Commented Jun 30, 2017 at 15:12
  • 1
    If you want to compare the sollWert1 etc values from each line, even when there's only one line, then you could leave the comparison inside the loop over lines. If you know there's exactly one line, you don't need to loop over lines at all. Commented Jun 30, 2017 at 15:27
  • 1
    @ktb - Reread my comment. I did not say your suggestion was wrong. I observed a limitation to it. You made an assumption, and I noted it. You'll find this polite honesty common on stackoverflow. Commented Jun 30, 2017 at 15:34

2 Answers 2

2

Define a global list in your program

List<String> werts = new ArrayList<>();
....................
....................
String[] parts = line.split(";");
for(String part : parts){
    werts.add(part);
}

Then you can use the elements in werts, and after that clear it if you need to go through the file another time (optional)

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

Comments

1

First of all, don't do processing directly within your main() method. At least create an instance of the enclosing class (use new ReadText()) and put the code within some processing method of the class (i.e. public void process() { ... }).

Objects in Java are used, among other reasons, because they have state. Then, you can define private properties for your ReadText object and store the values read in there (for example, do have a protected String sollWert1 etc.)

And finally, if you are intending to keep an unknown number of values for later usage, use a List (if you need ordering and don't need to label them), Set (if you're not concerned about order) or Map (if you want them labeled). Generally, using variable names like something1, something2, something3 etc. implies that you want to use a data structure instead.

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.