Slight problem with programming Java

Discussion in 'OT Technology' started by unknown_soldier, Sep 4, 2004.

  1. Just started taking classes, and we were given some code and told to fix the errors. I've fixed much of it, but I'm not sure what to do with this one part (in Bold). This is the very first assignment in my Java programming class, so I'm guessing it's pretty basic, but how do I make the Int and the Double work together? or what do I change?
    Any help is appreciated. I'm using TextPad in WinXP.
    Given this error when compiling:
    public class VarTest
    {
    public static void main(String [] args )
    {
    int firstInt = 5;
    double firstDouble = 18, secondDouble = 5.5234;
    int secondInt = secondDouble;
    int thirdInt = firstInt + secondInt;
    char firstChar = 'c', secondChar = 'C';
    char thirdChar = firstChar += 4;
    double thirdDouble = 11.4;
    String firstName = "Duke";
    System.out.println("firstInt = " + firstInt);
    System.out.println("secondInt = " + secondInt);
    System.out.println("thirdInt = " + thirdInt);
    System.out.println("firstChar = " + firstChar);
    System.out.println("secondChar = " + secondChar);
    System.out.println("thirdChar = " + thirdChar);
    System.out.println("thirdDouble = " + thirdDouble);
    System.out.println("firstDouble = " + firstDouble);
    System.out.println("firstName = " + firstName);
    System.out.println("Livin' on the edge...");
    }
    }
     
  2. 66comet

    66comet New Member

    Joined:
    Jun 4, 2004
    Messages:
    54
    Likes Received:
    0
    Try saying double instead of int. In most languages double are handled like floats. I am pretty sure there is a function to convert between doubles and integers but I can't remember, it should be in your book.
     
  3. Up All Night

    Up All Night I might sleep all day

    Joined:
    Jan 23, 2004
    Messages:
    2,104
    Likes Received:
    0
    You can't just make an int = to a double, an Int cant hold the values after the decimal. Change your ints to doubles. Or you could round the double and then make secondint= to the rounded double
     
  4. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    Code:
    public class VarTest
    {
    	public static void main(String [] args )
    	{
    		int firstInt = 5;
    		double firstDouble = 18, secondDouble = 5.5234;
    		int secondInt = (int)secondDouble;
    		int thirdInt = firstInt + secondInt;
    		char firstChar = 'c', secondChar = 'C';
    		char thirdChar = firstChar += 4;
    		double thirdDouble = 11.4;
    		String firstName = "Duke";
    		System.out.println("firstInt = " + firstInt);
    		System.out.println("secondInt = " + secondInt);
    		System.out.println("thirdInt = " + thirdInt);
    		System.out.println("firstChar = " + firstChar);
    		System.out.println("secondChar = " + secondChar);
    		System.out.println("thirdChar = " + thirdChar);
    		System.out.println("thirdDouble = " + thirdDouble);
    		System.out.println("firstDouble = " + firstDouble);
    		System.out.println("firstName = " + firstName);
    		System.out.println("Livin' on the edge...");
    	}
    }
    
    :)
     
  5. can you add Int + Double?

    This is my problem...
    int secondInt = secondDouble;
    int thirdInt = firstInt + secondInt;

    How do I create a value for the secondInt so that I can create a value for the thirdInt? Will I have to make thirdInt be a fourthDouble? (thirdDouble already exists)
    . . . . (5 + 5.5234 = 10.5234)
    Can: Int + Double = Double ?

    Would the correct outcome be:
    double secondDouble = secondDouble; (since secondDouble it already in coding above, I could delete this line)
    double fourthDouble = firstInt + secondDouble;
     
  6. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    secondInt is not a double, it is the truncated version of the double which is 5.
    so thirdInt = firstInt + secondInt would equal 10.
     
  7. oh, I see...

    so putting (int) secondDouble will change the Double into an int value, just by adding the (int)... right?
     
  8. RaginBajin

    RaginBajin Have you punched a donkey today?

    Joined:
    Dec 24, 2001
    Messages:
    8,740
    Likes Received:
    0
    Location:
    NoVA
    Yes it's called casting.
     
  9. researched it... found it... thanks guys

    correct format as posted by emlla vbmenu_register("postmenu_23326391", true); is: (int) second Double
     

Share This Page