Results 1 to 10 of 10
- 08-28-2012, 11:30 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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 #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: using indexOf and substring to calculate a number
Sure. Why not?i was wondering if i could store 134,115,64 and 1 in variable
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.
[Edit] 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.Last edited by pbrockway2; 08-28-2012 at 12:11 PM.
- 08-28-2012, 01:47 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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 '.'
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 '.'Java 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 }
- 08-28-2012, 01:53 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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 #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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 #6
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
Re: using indexOf and substring to calculate a number
i've figured it out
thanks =) if i need more help i'll post backJava 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);Last edited by pindo; 08-28-2012 at 02:10 PM.
- 08-28-2012, 02:27 PM #7
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-03-2012, 08:16 AM #8
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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 #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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 #10
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Help with indexof please...
By fatabass in forum New To JavaReplies: 7Last Post: 02-07-2012, 09:36 PM -
Help in substring and indexOf function
By janjong03 in forum New To JavaReplies: 6Last Post: 06-16-2010, 05:43 PM -
Java application to calculate and display the cost of a number of tickets according
By herberwz in forum New To JavaReplies: 1Last Post: 04-27-2010, 11:29 PM -
Am having trouble to calculate number of methods in class
By luckyy in forum New To JavaReplies: 12Last Post: 04-16-2010, 02:18 PM -
Need help with calculate the number of seconds
By ProUnbeatable in forum New To JavaReplies: 5Last Post: 10-11-2008, 03:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks