Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-21-2012, 07:11 PM   PM User | #1
Jemdt
New Coder

 
Join Date: May 2012
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Jemdt is an unknown quantity at this point
Number randomly rounding

Hi, from a book I'm reading, I made a volume calculator using a foreign subroutine. The subroutine (TextIO) can be found by CLICKING HERE

The code for what I've made is as followed:

Code:

public class Volume {
	public static void main(String[] argv) {
		float squareLength;
	    float area;
		float otherDimension;
		float volume;
		
		TextIO.put("Please enter the length of your square cuboid face: ");
		squareLength = TextIO.getlnInt();
		
		area = (float)Math.pow(squareLength,2);
		System.out.print("Your area is: ");
		System.out.println(area);
		
		TextIO.put("Please enter the length of your other dimension: ");
		otherDimension = TextIO.getlnInt();
		
		volume = area * otherDimension;
		System.out.print("The volume of this shape is: ");
		System.out.println(volume);
		
	}

}


Basically, by using TextIO, this system works out the volume by squaring squareLength and then multiplying the area by the other dimension. The system works fine, apart from one problem: whenever you square a decimal number or multiply a decimal number by the area in this system, it randomly rounds the number either down despite the fact I'm using primitive type float.

I don't want it to change the number at all, I want the system to print the actual number for the area and the volume.

Thanks
Jemdt is offline   Reply With Quote
Old 06-21-2012, 08:57 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
If you look @ the getlnInt() it returns an int

Code:
public static int getlnInt() { 
      int x=getInt(); 
      emptyBuffer(); 
      return x; 
   }
and then...
Code:
getInt()     { 
      return (int)readInteger(Integer.MIN_VALUE, Integer.MAX_VALUE);
   }
so you are getting as your most primitive type as an Int which makes the float, not a float.

*I think
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 06-22-2012, 07:43 PM   PM User | #3
Jemdt
New Coder

 
Join Date: May 2012
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Jemdt is an unknown quantity at this point
Quote:
Originally Posted by alykins View Post
If you look @ the getlnInt() it returns an int

Code:
public static int getlnInt() { 
      int x=getInt(); 
      emptyBuffer(); 
      return x; 
   }
and then...
Code:
getInt()     { 
      return (int)readInteger(Integer.MIN_VALUE, Integer.MAX_VALUE);
   }
so you are getting as your most primitive type as an Int which makes the float, not a float.

*I think
Thanks for pointing that out, all it involved was just changing the int in the getln subroutine to a float.
Jemdt is offline   Reply With Quote
Reply

Bookmarks

Tags
round help java print

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:12 AM.


Advertisement
Log in to turn off these ads.