0
public void ReadCVS() {
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    int nbA,nbB,nbC;
    nbA=nbB=nbC=0;
    String lval="";
    String test;
    double x = 25;
    double y = 100;
    double z = 200;
    double lp1 = 0;
    try {
        ArrayList<String> l1 = new ArrayList<String>();
        br = new BufferedReader(new FileReader(this.filePath));

        while ((line = br.readLine()) != null) {
            //System.out.println(line);
            // use comma as separator
            String[] tempData = line.split(cvsSplitBy);
            allDatakey.add(tempData[0]);
            allDataValue.add(tempData[1]);

            double j = parseDouble(tempData[1]);
}

//I want to use j here

      if((j>x)&&(j<y)){

        test ="A";
        if(!lval.equals("A")){
        l1.add(test+String.valueOf(nbA));
        nbA++;
        }
        lval="A";

in this code the double j is inside the whileloop , my question is : how can I declare it to use it outside the whillloop buffer ....

2
  • 1
    Unlike your title suggests, double j is not an array. You can just declare it outside the while loop with double j = -1; (or any other initial value that you would like it to have if there were no lines in the file) and assign it inside the while loop with j = parseDouble(...); Commented Mar 18, 2016 at 8:39
  • That is a while-loop block (or while block). There isn't any while loop buffer. Commented Mar 18, 2016 at 8:44

2 Answers 2

1

Declaring an array can be seen here: How do I declare and initialize an array in Java?

If you want to use tempData variable outside of your while loop, you can also copy the array to a new one.

if you only want to get the variable J outside then do this:

double j = 0.0;
while (....) {
   j = parseDouble(tempData[1]
}
Sign up to request clarification or add additional context in comments.

1 Comment

like that it take j =0.0 outside the whileloop
1

Try this:

try {
    ArrayList<String> l1 = new ArrayList<String>();
    br = new BufferedReader(new FileReader(this.filePath));
    double j = 0.0;          //shift your J to Outside of the Loop.
    while ((line = br.readLine()) != null) {
        //System.out.println(line);
        // use comma as separator
        String[] tempData = line.split(cvsSplitBy);
        allDatakey.add(tempData[0]);
        allDataValue.add(tempData[1]);
        //change your parseDouble to Double.parseDouble as I Do Here.

        j = Double.parseDouble(tempData[1]); //Initialize Your J here
    // .......
}

Note: j is declared outside of while loop and Initialize inside the loop and again you can use outside of while whatever it contains.

4 Comments

if i use it like that , it will be 0.0 outside the loop .
then something probably goes wrong in your parseDouble method.
no . j value will be changed whenever this statement j = parseDouble(tempData[1]); is getting executed.. right what you say ?? J will contain 0.0 If and only if either while condition is false or tempData[1] contailns 0. Thank you
then debug your application look intio the array tempData is this variable contains actual expected data that you want to load ?? or not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.