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 ....

roge
  • 31
  • 4
  • 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(...);` – Erwin Bolwidt Mar 18 '16 at 08:39
  • That is a while-loop block (or while block). There isn't any while loop buffer. – jhamon Mar 18 '16 at 08:44

2 Answers2

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.

Tom
  • 14,120
  • 16
  • 41
  • 47
Vikrant Kashyap
  • 5,028
  • 1
  • 23
  • 46
  • if i use it like that , it will be 0.0 outside the loop . – roge Mar 18 '16 at 08:45
  • then something probably goes wrong in your parseDouble method. – Lars Celie Mar 18 '16 at 08:46
  • 1
    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 – Vikrant Kashyap Mar 18 '16 at 08:47
  • then debug your application look intio the array `tempData` is this variable contains actual expected data that you want to load ?? or not. – Vikrant Kashyap Mar 18 '16 at 09:51
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]
}
Community
  • 1
  • 1
Lars Celie
  • 592
  • 4
  • 15