Results 1 to 10 of 10
  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default centimetres = inches*2.75;

    Can someone tell me what is wrong with this coding, when i am trying to compile this i keep getting told that there is something wrong with the line in bold (centimetres = inches*2.75;) any advise??

    public class inc2cm{

    public static void main (String[]arg) {

    float inches;
    float centimetres;

    centimetres = inches*2.75;

    System.out.println("How many inches?");
    inches=UserInput.readInt();



    System.out.println("inches into centimetres is"+centimetres);


    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Your small problem is that your using a double (2.75) and trying to get a float, and the compiler is complaining. My response would be for you to never use floats unless necessary. A double is more accurate and doesn't cause a significant performance hit.
    2) A major problem is that you're trying to calculate the result before you've received the data. Since the inches variable hasn't received any input, it's equal to zero, and so the resulting centimetres will always be 0 no matter what inches number is entered. A solution would be to do the calculation after the user enteres the inches number. A better solution still would be to calculate this in a separate method.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default

    That code won't compile on two accounts.
    -- inches has not been initialized. You have to initialize a local variable before it can be accessed.
    -- and what Fubarable pointed out about mixing doubles and floats.

    > Since the inches variable hasn't received any input, it's equal to zero
    Um, not in this case, it's a local variable.

    db

    edit And apart from all that, cm = in * 2.54. Not 2.75.
    Last edited by DarrylBurke; 11-01-2008 at 11:07 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    there is something wrong with the line in bold
    How do you know?
    Did you get an error message? Please post it.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by soc86 View Post
    Can someone tell me what is wrong with this coding, when i am trying to compile this i keep getting told that there is something wrong with the line in bold (centimetres = inches*2.75;) any advise??

    public class inc2cm{

    public static void main (String[]arg) {

    float inches;
    float centimetres;

    centimetres = inches*2.75;

    System.out.println("How many inches?");
    inches=UserInput.readInt();



    System.out.println("inches into centimetres is"+centimetres);


    }
    }
    You get that error because your conversion is wrong.

    Please don't post the same question. I found that more like two questions from you in this same sub-forum, only thing is that the tittle and the way of implementation is differ. And also please ask your question much more clearly, state that what the requirement is.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Wink Who says Cat's can't dance?

    Java Code:
    //www.leepoint.net/notes-java/data/strings/conversion/string2num.html
    public class Converter{
        private static final Double conversionFactor = new Double(0.3937008);
        public static void main(String[] argumentation) {
            if(argumentation != null){
                if(argumentation.length > 0){
                    try
                    {
                        // Try to write some code that can do the conversion.
                        double doublePosters = Double.parseDouble(argumentation[0]);
                    }
                    catch(NumberFormatException nix)
                    {
                        System.out.println("Who says Cat's can't dance?");//
    With my apologies to the Masters, I needed something to make my day....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    P.S. OP, AFAIK it is 2.54 cm per inch, not 2.75 ;-)

  8. #8
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    public class inc2cm{
    
    public static void main (String[]arg) {
    
    
    float centimetres;
    System.out.println("How many inches?");
    Scanner UserInput= new Scanner (System.in);
    float inches = UserInput.nextInt();
    
    centimetres = (float)(inches * 2.54);/* it compiles and runs successfully 
    but i think its right without the casting bcoz inches is float so the centemeter????*/
    
    
    System.out.println("inches into centimetres is"+centimetres);
    
    
    }
    }
    :confused:

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    2.54 is a double, 2.54f is a float.

  10. #10
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •