-1

So my program that I am creating is going to calculate the subnet of an IP address that is given. It is being written in java. The program asks for the IP address that will always be given in the form 255.255.255.255. The plan is to separate a part of the string, then wrap it into an integer.

I need to extract each of the 4 quadrants as separate integers. The trick I can't figure out is how to do this when the IP looks more like 192.168.1.20. How would I be able to do this and get reliable results.

User input : 192.168.205.16

Created Variables :

Quadrant1 : 192

Quadrant2 : 168

Quadrant3 : 205

Quadrant4 : 16

  • 1
    Use the **String.split()** method. `String[] quadrants = input.split("\\.");` – DevilsHnd - 退職した Apr 06 '18 at 01:56
  • My guess is that it's coming in as a string. In which case just split it on the periods and convert it to whatever. – Stucco Apr 06 '18 at 01:58
  • 1
    The proper terminology is `octets`, not `quadrants`. They are called `octets` because each represents eight bits of the 32-bit address. Convert the four octets to a 32-bit integer, mask it with the network mask, then convert back to the dotted-decimal notation. That will give you the network address in the text format. – Ron Maupin Apr 06 '18 at 02:01
  • Ya...what he said...but in reality you can call it **dirtyUnderwear** if you want. ;) – DevilsHnd - 退職した Apr 06 '18 at 02:08
  • @DevilsHnd, my comment was for the OP, not your comment. I hoped for a little education with the comment. – Ron Maupin Apr 06 '18 at 02:24
  • @RonMaupin haha thanks guys, I actually did know they were octets but i started the project on day 1 when I had no idea, simple refactor coulda solved it I suppose :P late reply, thanks none the less – Owen Stafford May 02 '18 at 12:50

1 Answers1

0
String[] ipSplit = userInput.split("\\.");
Integer q1 = Integer.valueOf(ipSplit[0]);
...

etc.. edits: my bad, regex for the split string, and array of strings rather than list of strings

dbs.83
  • 159
  • 1
  • 7