Java

Discussion in 'Programmer's Corner' started by AMJC77, Oct 10, 2015.

Java / Double println Question

  1. Hints on how to remove the 0 following the .

    1 vote(s)
    100.0%
  2. Help on how to remove the 0 following the .

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. AMJC77

    Thread Starter New Member

    Apr 2, 2015
    13
    0
    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: Oct 10, 2015
  2. vpoko

    Member

    Jan 5, 2012
    258
    47
    Why doubles? Use ints.
     
  3. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,729
    Wow.
     
    GopherT likes this.
  4. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,026
    3,789
    @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.
     
    AMJC77 likes this.
  5. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    How about using unsigned char, or signed char? Each part will not be greater that 255.
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,737
    4,789
    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.
     
    AMJC77 likes this.
  7. AMJC77

    Thread Starter New Member

    Apr 2, 2015
    13
    0
    I've never done the module before so I am learning as I go! Thanks very much for your reply, it helped a lot!
     
  8. AMJC77

    Thread Starter New Member

    Apr 2, 2015
    13
    0
    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!
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,001
    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.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,001
    And you post a survey for a question??? Yikes!
     
  11. darrough

    Member

    Jan 18, 2015
    86
    19
    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.
     
  12. jim829

    New Member

    Aug 2, 2012
    3
    1
    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).
     
  13. WBahn

    Moderator

    Mar 31, 2012
    17,737
    4,789
    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.
     
  14. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,026
    3,789
    I had to give you a "like" for using the word "yikes" in scary October.
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,001
    Boo!
     
Loading...