0

This is my code:

 public void setAreaAccessPoints(){
    String mac = "",essid = "",status = "";
    int strength = 0,kanali = 0;
    List<String> AccessPoints = new ArrayList<String>(); //i lista me ta access points
    String temp;
    try{
        String[] command = {"/bin/sh", "-c", "sudo iwlist " + wirelessName + " scanning | grep -A5 \"Cell\" "};
        Process child = Runtime.getRuntime().exec(command);
        BufferedReader r = new BufferedReader(new InputStreamReader(child.getInputStream()));
        while((temp = r.readLine()) != null){
            if(temp.contains("Cell")){
                String[] info = temp.split(" ");
                mac = info[3];
                System.out.println(mac);
                do{
                    temp = r.readLine();
                    if(temp.contains("ESSID:")){
                        essid = temp.replace("ESSID:","");
                    }
                    if(temp.contains("Frequency:")){
                        String[] info1 = temp.split(" ");
                        info1[3] = info1[3].replace(")","");
                        kanali = Integer.parseInt(info1[3]);
                    }
                    if(temp.contains("Mode:")){
                        status = temp.replace("Mode:","");
                    }
                    if(temp.contains("Quality=")){
                        String[] info2 = temp.split(" ");
                        info2[3] = info2[3].replace("level=","");
                        strength = Integer.parseInt(info2[3]);
                    }
                    if(temp.contains("Protocol:")){
                        temp = r.readLine();
                    }
                }while(!(temp.contains("Cell")));
                AccessPoint newAP = new AccessPoint(mac,essid,kanali,status,strength); 
                AccessPoints.add(newAP.toString());  //vazoume ta access points sti lista san strings
            }
        }
        r.close();
        for(String s : AccessPoints)
            System.out.println(s);
    }catch(IOException e){e.printStackTrace();}
}

The output which i am parsing looks like this:

      Cell 04 - Address: 00:05:59:30:C1:7C
                Protocol:802.11b/g
                ESSID:"NA home"
                Mode:Managed
                Frequency:2.437 GHz (Channel 6)
                Quality=2/100  Signal level=-89 dBm  Noise level=-92 dBm
  --
      Cell 05 - Address: 00:05:59:43:AE:C9
                Protocol:802.11b/g
                ESSID:"NetFasteR IAD 2 (PSTN)"
                Mode:Managed
                Frequency:2.437 GHz (Channel 6)
                Quality=0/100  Signal level=-91 dBm  Noise level=-94 dBm
  --
      Cell 06 - Address: 00:05:59:3B:C1:FA
                Protocol:802.11b/g
                ESSID:"Kpanagiotou"
                Mode:Managed
                Frequency:2.437 GHz (Channel 6)
                Quality=0/100  Signal level=-91 dBm  Noise level=-94 dBm
  --

The error is in the 2 lines "strength = Integer.parseInt(info2[2]);" and "kanali = Integer.parseInt(info1[3]);" ... I can't seem to figure out where is the problem. When I am splitting the string, the info I want is in the second and third field according to the output. So why does it try to pass a null string for integer parsing?

StackTrace:

 Exception in thread "Thread-1" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at askisi1.Wireless.setAreaAccessPoints(Wireless.java:213)
    at askisi1.Wireless.run(Wireless.java:43)
    at java.lang.Thread.run(Thread.java:722)
5
  • 1
    Well, from error message it is evident that value at info1[3] is not a valid integer. I would do System.out and make sure it is valid Integer. Commented Nov 9, 2012 at 15:59
  • 1
    It tells you the value that it can't convert - right there in the stack trace. Commented Nov 9, 2012 at 16:02
  • please give full input text . Commented Nov 9, 2012 at 16:04
  • Yep i know that it cant convert the value "".. the problem is..why does it have that value when its supposed to have the value "6)" Commented Nov 9, 2012 at 16:05
  • @Stelsawa you have to trim() your temp string. Check my answer Commented Nov 9, 2012 at 16:21

4 Answers 4

1

The line

Quality=2/100  Signal level=-89 dBm  Noise level=-92 dBm

contains double spaces, so in the split result, you have empty Strings and the non-empty Strings are not at the indices you think they are.

Printing out the result of "Quality=2/100 Signal level=-89 dBm Noise level=-92 dBm".split(" "); with indices yields

0: Quality=2/100
1: 
2: Signal
3: level=-89
4: dBm
5: 
6: Noise
7: level=-92
8: dBm

You seem to have leading spaces in your file, so the indices with nonempty Strings would be later then.

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

4 Comments

So when i am splitting the string..how does it handle the double spaces? does it put a "" value in the info array?
Yup, exactly that. Between the two splitting tokens, there is an empty String, and that is put into the split array.
Ok i fixed that line but what about this one? Frequency:2.437 GHz (Channel 6)? This one does not contain double spaces and if i am not mistaken the value i want should be in info1[3] ("6)") correct?
The leading spaces I mentioned at the end of my answer (after the edit, admittedly).
1

you are checking for something and replacing something else...

 if(temp.contains("Quality=")){
                    String[] info2 = temp.split(" ");
                    info2[1] = info2[1].replace("level=","");
                    strength = Integer.parseInt(info2[2]);
                }

2 Comments

temp contains the whole line of the output ( Frequency:2.437 GHz (Channel 6) thats why i am splitting it and replacing only the part i want
@Stelsavva You like making your code unreadable dont you :). Check all the spaces you are splitting and if you are working on the right index.
0

You remove "level=" from info2[1] and then don't do anything with it later. You should remove it from info2[2]. Also, note that they are separated by double spaces as noted in another answer here.

Comments

0

Since you have spaces in-front of the lines you are getting wrong part for to format without any spaces in both sides your logic is working

you could do trim() and check your result

 temp = r.readLine().trim();

For example this is working

String str ="Frequency:2.437 GHz (Channel 6)";
String [] str1 = str.split(" ");
System.out.println(Integer.parseInt(str1[3].replace(")", "")));

2 Comments

in which line now your getting the exception
same lines..i printed the data of the info arrays and i indeed have a lot of trailing whitespaces..i used temp.trim() in all cases but still no result

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.