-3

I want to extract the IP address from the following String:

106.220.155.36 - - [29/Sep/2015:09:51:52 -0400] "GET /tutorial/grammar/LL/images/llparsetable.png HTTP/1.1" 200 14284

To do this, I decided to read the string one character at a time. I tried the following code:

public static String uniqueIP(String line){
        String IP = "";

        while(line.next() != " "){
            IP = IP + line.next();
        }
        return IP;
    }

However, the next method doesn't work. Is there some other method I can use?

Shoaib Ahmed
  • 51
  • 1
  • 2
  • 4
  • 1
    what's `line.next()` ? – Nir Alfasi Aug 27 '17 at 23:00
  • A String is more or less an Array of Character – Vivick Aug 27 '17 at 23:04
  • 1
    Where did you get the `next` method, some blog I'm guessing? Help yourself by looking at the `String` Javadoc; if you're writing Java code (apparently), Javadoc is the first place to look for help, not Stackoverflow. – Abhijit Sarkar Aug 27 '17 at 23:06
  • "However, the next method doesn't work" this looks like another case for [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Aug 27 '17 at 23:21

1 Answers1

0

If you are sure that your string structure will be always the same you can split it by "- -"

String s = "106.220.155.36 - - [29/Sep/2015:09:51:52 -0400] "GET /tutorial/grammar/LL/images/llparsetable.png HTTP/1.1" 200 14284"

String ip = s.split("- -")[0].trim();
Sarkhan
  • 1,281
  • 1
  • 11
  • 33