Java

Java / Double println Question

  • Hints on how to remove the 0 following the .

    Votes: 1 100.0%
  • Help on how to remove the 0 following the .

    Votes: 0 0.0%

  • Total voters
    1

Thread Starter

AMJC77

Joined Apr 2, 2015
13
Hi, I'm currently writing a code for my 4th year project at the moment in Java.
To start off I'm first trying to ping two PCs, but I'm at the very beginning of this process.

At the moment I've created a class called Network Address
It will store the Network Address of the PC.

I'm having a problem though, because I cannot print the network add. all at once (because there are 3 full stops in it) I've broken the address up into 4 pieces.

First section = 192.
Second Section = 168.
Third Section = 1.
Fourth = 254

class NetworkAddress {


public double FirstPart; // 1st part of Network Address
public double SecondPart; // 2nd part of Network Address
public double ThirdPart; // 3rd part of Network Address
public double FourthPart; // 4th part of Network Address

public NetworkAddress(double Part1, double Part2, double Part3, double Part4) {
this.FirstPart = Part1;
this.SecondPart = Part2;
this.ThirdPart = Part3;
this.FourthPart = Part4;
}

public String toString(){
return "Network Address = "+FirstPart+SecondPart+ThirdPart+FourthPart;
}
}

import pc.NetworkAddress;


public class Test_PC_Class {

public static void main(String[] args) {

NetworkAddress PC0_IP = new NetworkAddress(192.,168.,1.,254);
System.out.println(PC0_IP);
}

}

The result I get is:

Network Address = 192.0168.01.0254.0

Is there any way of still using the decimal for the double but removing the zero afterwards?
Thank you.
 
Last edited:

GopherT

Joined Nov 23, 2012
8,009
@AMJC77
Dude. Anything that doesn't look like a number is a string. You can build a string from numbers using number to string conversion commands.

Since each part is 0-255, there is no need for float or double, just use int or char types and then create a string by concatenation: as you did above but use, first part+ "." + secondpart + "." + thirdpart + "." + fourthpart

Good luck and try to be more creative with all of the tools available in the language.
 

WBahn

Joined Mar 31, 2012
30,082
Hi, I'm currently writing a code for my 4th year project at the moment in Java.
To start off I'm first trying to ping two PCs, but I'm at the very beginning of this process.

At the moment I've created a class called Network Address
It will store the Network Address of the PC.

I'm having a problem though, because I cannot print the network add. all at once (because there are 3 full stops in it) I've broken the address up into 4 pieces.

First section = 192.
Second Section = 168.
Third Section = 1.
Fourth = 254

class NetworkAddress {


public double FirstPart; // 1st part of Network Address
public double SecondPart; // 2nd part of Network Address
public double ThirdPart; // 3rd part of Network Address
public double FourthPart; // 4th part of Network Address

public NetworkAddress(double Part1, double Part2, double Part3, double Part4) {
this.FirstPart = Part1;
this.SecondPart = Part2;
this.ThirdPart = Part3;
this.FourthPart = Part4;
}

public String toString(){
return "Network Address = "+FirstPart+SecondPart+ThirdPart+FourthPart;
}
}

import pc.NetworkAddress;


public class Test_PC_Class {

public static void main(String[] args) {

NetworkAddress PC0_IP = new NetworkAddress(192.,168.,1.,254);
System.out.println(PC0_IP);
}

}

The result I get is:

Network Address = 192.0168.01.0254.0

Is there any way of still using the decimal for the double but removing the zero afterwards?
Thank you.
I'm unfamiliar with the term "4th year project" so I don't know what level you are at. Are you at the beginning of college/university, or nearing the end of a bachelors degree, or what? What's your major field of study? Just helps to establish the context.

A network address (IPv4) is a 32-bit unsigned integer. The "dotted decimal" notation is just that -- a notation that is useful for humans. It is obtained by breaking the 32-bit integer into four 8-bit chunks (bytes) and then displaying each byte individually in decimal notation (so it will be a value between 0 and 256, inclusive) separated by periods.

You do NOT want to represent the pieces as floating point values -- that is asking for trouble if you do anything that results in even minor roundoff error.
 

Thread Starter

AMJC77

Joined Apr 2, 2015
13
@AMJC77
Dude. Anything that doesn't look like a number is a string. You can build a string from numbers using number to string conversion commands.

Since each part is 0-255, there is no need for float or double, just use int or char types and then create a string by concatenation: as you did above but use, first part+ "." + secondpart + "." + thirdpart + "." + fourthpart

Good luck and try to be more creative with all of the tools available in the language.
I've never done the module before so I am learning as I go! Thanks very much for your reply, it helped a lot!
 

Thread Starter

AMJC77

Joined Apr 2, 2015
13
I'm unfamiliar with the term "4th year project" so I don't know what level you are at. Are you at the beginning of college/university, or nearing the end of a bachelors degree, or what? What's your major field of study? Just helps to establish the context.

A network address (IPv4) is a 32-bit unsigned integer. The "dotted decimal" notation is just that -- a notation that is useful for humans. It is obtained by breaking the 32-bit integer into four 8-bit chunks (bytes) and then displaying each byte individually in decimal notation (so it will be a value between 0 and 256, inclusive) separated by periods.

You do NOT want to represent the pieces as floating point values -- that is asking for trouble if you do anything that results in even minor roundoff error.
I'm nearing the end of a Bachelor in Electronics, that's my field of study. I've done four modules in Networking so the theory for that is fine to me. It's just using Java is the part I'm struggling with. Thanks for your reply!
 

spinnaker

Joined Oct 29, 2009
7,830
I'm nearing the end of a Bachelor in Electronics, that's my field of study. I've done four modules in Networking so the theory for that is fine to me. It's just using Java is the part I'm struggling with. Thanks for your reply!
You are in your fourth year of your BSEE and you don't know to not use a float to represent an IP address? What have you been doing for the past 4 years? Upon graduation, I hope you won't be getting a job where public safety is involved.
 

darrough

Joined Jan 18, 2015
86
Java has an AWESOME networking library. Use java.net.InetAdress to represent an IP Address.

There is a tendency when one does not know what is in the standard libraries to invent what one needs over check the reference materials to see whats available. Your IP Address would be a good example of that. Your thinking "it's pretty simple. I could make my own in the time it take to look it up". You proceed to write a bunch of code around your IPAddress class. At some point you need to do a network task that is not so easy, so you look in the reference to see whats available. Sure enough it is there, but its methods require and or return a java.net.InetAdress. You cannot go back on your IPAddress class because of all the code you have built around it, so you have to convert and between InetAddress and IPAddress. This is called "glue code" because it "glues" your code to the java.net code. Glue code is notoriously troublesome.

The library reference is online at: http://docs.oracle.com/javase/7/docs/api/index.html

I would advise you check the reference for any function that is not application specific, such as networking stuff.
 

jim829

Joined Aug 2, 2012
3
Keep in mind that the dotted quad notation for IP addresses is simply for human consumption. If you ever want to place the address in an IP header for example, you will need to convert it to a 32 bit value. So as was previously suggested, focus on the libraries rather than growing your own. And keep in mind that you may need to include a network mask (in your case it is probably a /24 but doesn't have to be).
 

WBahn

Joined Mar 31, 2012
30,082
I'll third (or whatever we're up to now) the recommendation to use the available libraries, but caveat that with the proviso that this is assuming that the point of the project isn't, at least in part, to learn the nitty-gritty of network protocols and how to implement them. It seems like such proviso shouldn't be necessary, but it is amazing the number of people that seem think that calling a library function to do some task is completely equivalent to writing a library function to do that same task.
 
Top