# using indexOf and substring to calculate a number

• 08-28-2012, 11:30 AM
pindo
using indexOf and substring to calculate a number
hey,
i'm not sure how to go about this, my program is supposed to get the user to input an IP address. the program is then supposed to return a value in decimal notation. e.g 134.115.64.1 is input and 2255699969 is output.
i was wondering if i could store 134,115,64 and 1 in variable.
if so, how could i go about it?
a link to any sort of tutorial would be great as i haven't managed to find one my self.
also I'm not permited to use arrays.
• 08-28-2012, 12:03 PM
pbrockway2
Re: using indexOf and substring to calculate a number
Quote:

i was wondering if i could store 134,115,64 and 1 in variable
Sure. Why not?

You don't say how that big long (I guess decimal) number is related to the "dotted quad" form of the IP address. Perhaps you could explain that, because I'm thinking that would shed light on the process to follow to go from the address to a single integral quantity.

 Rereading the subject of the thread I realise that there is a preliminary question of how you go first from the IP address as a string to the separate strings "134", "115" etc (using indexOf() and substring()) and then to the numeric quantities 134, 115, etc. I would suggest you read up on what these string methods actually do, and then describe - in natural language - a process for dividing the long string into pieces. Post what you come up with if you get stuck. Then try to turn you idea into code.
• 08-28-2012, 01:47 PM
pindo
Re: using indexOf and substring to calculate a number
the "dotted quad" is calculated by (((a x 256) + b) x 256 + c) x 256 + d where a b c d are the separate strings a.b.c.d

so after some more searching i found that i could implement indexOf() to find the point of the '.'

Code:

```import java.util.Scanner; public static void main(String[] args){                       Scanner keyboard = new Scanner (System.in);             System.out.println("Please enter an IP address. enter in the following format a.b.c.d, e.g 123.123.123.132");             String ip = keyboard.next().trim();             int firstDot = ip.indexOf((int)('.'));             String a = ip.substring(0, firstDot);                         System.out.println(a); //just for test purposes to show first string                             }```
i've managed to split the first string before the first '.' , i can't manage to find out out to get indexOf() to work for the second '.'
• 08-28-2012, 01:53 PM
pbrockway2
Re: using indexOf and substring to calculate a number
OK, that's good.

You use substring() to find the first part. So just use substring() again (on the original string) to find the rest of the string past the first dot. There's another form of substring that does this.

Then repeat as required.
• 08-28-2012, 01:58 PM
pbrockway2
Re: using indexOf and substring to calculate a number
Manipulating Characters in a String (The Java™ Tutorials > Learning the Java Language > Numbers and Strings) might be useful. Not only for string methods but, nearby, getting the int values and, further afield, checking that ints can get big enough to hold the result of your calculation.
• 08-28-2012, 02:02 PM
pindo
Re: using indexOf and substring to calculate a number
i've figured it out
Code:

```Scanner keyboard = new Scanner (System.in);             System.out.println("Please enter an IP address. enter in the following format a.b.c.d, e.g 123.123.123.132");             String ip = keyboard.next().trim();             int first = ip.indexOf((int)('.'));             String a = ip.substring(0, first);             int second =  ip.indexOf('.',first+1);             String b = ip.substring(first+1,second);             int third= ip.indexOf('.',second+1);             String c = ip.substring(second+1, third);             String d = ip.substring(third+1);                                     System.out.println(a);             System.out.println(b);             System.out.println(c);             System.out.println(d);```
thanks =) if i need more help i'll post back
• 08-28-2012, 02:27 PM
DarrylBurke
Re: using indexOf and substring to calculate a number
Take a look at String's split(...) method. Since the parameter to split(...) is a regular expression, you'll need to learn a bit about those, particularly that the dot is a metacharacter in regex and will need to be escaped.

Pattern (Java Platform SE 6)
Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

db
• 09-03-2012, 08:16 AM
pindo
Re: using indexOf and substring to calculate a number
sorry for late reply but the regex helped me out a lot!
thanks to you all
• 09-03-2012, 08:37 AM
pbrockway2
Re: using indexOf and substring to calculate a number
You're wecome.

And thanks for the follow ups and feedback. It's really appreciated and some people don't bother.

It's understandable that there might be a delay ... those dots are real b@stards which is why I didn't suggest split(). But, Darryl's right, it's the way to go here.
• 09-03-2012, 09:15 AM
pindo
Re: using indexOf and substring to calculate a number
i would have used split() but i was was not allowed to use arrays in this question.
quite annoying as split() would have made it a lot easier