chankel Posted October 16, 2004 Share Posted October 16, 2004 need some help guys int unitPrice[] = {1599,599,2999,599,4999,899,999,5999}; for(int z=0;z<unitPrice.length;z++) { final NumberFormat test = new DecimalFormat("$##.##"); System.out.println(test.format(unitPrice[z])); } y my output is nt 15.99 5.99 29.99 5.99 49.99 8.99 9.99 59.99 i have no idea wat is wrong...any someone help me Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 16, 2004 Share Posted October 16, 2004 (edited) need some help guys int unitPrice[] = {1599,599,2999,599,4999,899,999,5999}; for(int z=0;z<unitPrice.length;z++) { final NumberFormat test = new DecimalFormat("$##.##"); System.out.println(test.format(unitPrice[z])); } y my output is nt 15.99 5.99 29.99 5.99 49.99 8.99 9.99 59.99 i have no idea wat is wrong...any someone help me 584743986[/snapback] Is "y my output is nt" supposed to read "why is my output not"? If so, it's because you're using an int and not a float, and not to mention the numbers don't match the format. The purpose of decimalformat is to a) round off decimals b) add $, in other words, formatting a decimal. It doesn't not work the way you're trying though. Edited October 16, 2004 by kjordan2001 Link to comment Share on other sites More sharing options...
0 chankel Posted October 16, 2004 Author Share Posted October 16, 2004 cos my course work require us to declare the monetary variables as int so as to avoid roundoff when performing arithmetic on these values so any method to print out the values like i wan?any hints?any help is appreciated... PS. does java has a special method to print a decimal point in the right place?tat is wat is said in my coursework Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 16, 2004 Share Posted October 16, 2004 (edited) cos my course work require us to declare the monetary variables as int so as to avoid roundoff when performing arithmetic on these valuesso any method to print out the values like i wan?any hints?any help is appreciated... PS. does java has a special method to print a decimal point in the right place?tat is wat is said in my coursework 584744168[/snapback] unitPrice[z]/100.0 Odd that you're using ints though, I'd worry more that I'd go over the max size of an int if I was working with floats as ints. Edited October 16, 2004 by kjordan2001 Link to comment Share on other sites More sharing options...
0 chankel Posted October 16, 2004 Author Share Posted October 16, 2004 import java.io.*; import java.text.*; public class Lab01 { public static void main (String[] args)throws IOException { int divide; int odd; int x; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Type a number:"); String s = in.readLine(); x = Integer.parseInt(s); /*Check whether the number is a positive number * */ if ( x > 0 ) { System.out.println("The number is an positive number"); } /*Check whether the number is divisble by 17 * */ divide = x % 17; if (divide == 0) { System.out.println("The number is divisible by 17"); } else { System.out.println("The number is not divisible by 17"); } /*Check whether the number is a odd number * */ odd = x%3; if (odd == 0) { System.out.println("It is an odd number"); } else { System.out.println("It is an even number"); } /*Check whether the number is between 10 and 20 * */ if ( x<20 && x > 10) { System.out.println(x + "is between 10 and 20"); } else { System.out.println(x + " is not between 10 and 20"); } /*Check whether the number is less than 50 and more than 100 * */ if ((x <50) || (x > 100)) { System.out.println("It is less than 50 or greater than 100"); } else { System.out.println("It is neither less than 50 or greater than 100"); } } } this is another program of mine..i wan it to read integers but not character...but i cant seem to do it..can anyone give me some hints Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 16, 2004 Share Posted October 16, 2004 You mean right here: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Type a number:"); String s = in.readLine(); x = Integer.parseInt(s); ? That's the only way you can get a number. Link to comment Share on other sites More sharing options...
0 chankel Posted October 16, 2004 Author Share Posted October 16, 2004 i only wan to accept numbers which are integers..nt characters...any idea how to do it? Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 16, 2004 Share Posted October 16, 2004 Integer.parseInt() will throw an exception if it can't convert it. Link to comment Share on other sites More sharing options...
0 chankel Posted October 16, 2004 Author Share Posted October 16, 2004 do { System.out.print("Type a number:"); String s = in.readLine(); try{ // this turns a string into the integer it represents x = Integer.parseInt(s); ok = true; } catch(NumberFormatException e) { // if you try to turn any string that doesn't represent an int, it will chuck this exception System.out.println("Enter a valid integer!"); ok = false; } }while(ok == false); like this?i this a correct way to do it?i find it quite stupid though Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 16, 2004 Share Posted October 16, 2004 Yeah, that should work, since ok = true won't execute during an exception, it goes right to the catch. It may be stupid, but about the only other way is to parse the string yourself looking for non-numeric characters. Although I prefer a regular while loop: while (!ok) { System.out.print("Type a number:"); String s = in.readLine(); try{ // this turns a string into the integer it represents x = Integer.parseInt(s); ok = true; } catch(NumberFormatException e) { // if you try to turn any string that doesn't represent an int, it will chuck this exception System.out.println("Enter a valid integer!"); ok = false; } } Link to comment Share on other sites More sharing options...
Question
chankel
need some help guys
int unitPrice[] = {1599,599,2999,599,4999,899,999,5999}; for(int z=0;z<unitPrice.length;z++) { final NumberFormat test = new DecimalFormat("$##.##"); System.out.println(test.format(unitPrice[z])); }y my output is nt
15.99
5.99
29.99
5.99
49.99
8.99
9.99
59.99
i have no idea wat is wrong...any someone help me
Link to comment
Share on other sites
9 answers to this question
Recommended Posts