Super Newb Troubleshooting

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 3
Reputation: luxxnatura is an unknown quantity at this point 
Solved Threads: 0
luxxnatura luxxnatura is offline Offline
Newbie Poster

Super Newb Troubleshooting

 
0
  #1
Nov 9th, 2009
Hi all,
I'm supposed to write a small program that asks the user for a number of a month( 1= Jan, 2=Feb, etc) and then outputs the month name and the month number. If the number entered is invalid (for example, > 12 or equal 0), then the user receives an error message. Perhaps there is a better way to write this, but based on my limited knowledge, this is what I have put together. However, when I run the program it prints ALL the months of the year, and then the single number input, like this:

Give me a number
10
January10
February 10
March10
April10
May10
June10
July10
August10
September10

Can anyone tell me what I've done incorrectly?
Here is the code:

  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Exercise3 {
  5. public static void main (String [] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8.  
  9. {
  10. System.out.println("Give me a number");
  11. }
  12.  
  13. int x = scanner.nextInt();
  14.  
  15. while(x == 1);
  16. {
  17. System.out.println("January" + x);
  18. }
  19.  
  20. while(x == 2);
  21. {
  22. System.out.println("February " + x);
  23. }
  24.  
  25. while(x == 3);
  26. {
  27. System.out.println("March" + x);
  28. }
  29.  
  30. while(x == 4);
  31. {
  32. System.out.println("April" + x);
  33. }
  34. while(x == 5);
  35. {
  36. System.out.println("May" + x);
  37. }
  38. while(x == 6);
  39. {
  40. System.out.println("June" + x);
  41. }
  42. while(x == 7);
  43. {
  44. System.out.println("July" + x);
  45. }
  46. while(x == 8);
  47. {
  48. System.out.println("August" + x);
  49. }
  50. while(x == 9);
  51. {
  52. System.out.println("September" + x);
  53. }
  54. while(x == 10);
  55. {
  56. System.out.println("October" + x);
  57. }
  58.  
  59. while(x == 11);
  60. {
  61. System.out.println("November" + x);
  62. }
  63. while(x == 12);
  64. {
  65. System.out.println("December" + x);
  66. }
  67.  
  68.  
  69. while ((x > 12) || (x == 0));
  70. {
  71. System.out.println("Error. Number must be 12 or less");
  72. }
  73.  
  74. }
  75. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 92
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #2
Nov 9th, 2009
instead of while, use "if"

  1. if(x == 12)
  2. {
  3. System.out.println("December " + x);//put a space after the month also.
  4. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: luxxnatura is an unknown quantity at this point 
Solved Threads: 0
luxxnatura luxxnatura is offline Offline
Newbie Poster
 
0
  #3
Nov 9th, 2009
I tried using 'if' originally, but it gave the same output as above.
Thanks for the note about leaving a space after the month name -- I didnt catch that.

However, the original problem still stands..
Any help is appreciated!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 92
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #4
Nov 9th, 2009
Without giving too much away...

  1. if(x==1)
  2. {
  3. //January
  4. }
  5. else if (x==2)
  6. {
  7. //February
  8. }

I would also test for invalid numbers first before testing for the correct months.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 92
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #5
Nov 9th, 2009
...notice there are no semi-colons after my if statements

  1. if(x==1)
  2. {
  3. System.out.println("January " + x );
  4. }
  5. else ...
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: luxxnatura is an unknown quantity at this point 
Solved Threads: 0
luxxnatura luxxnatura is offline Offline
Newbie Poster
 
0
  #6
Nov 9th, 2009
Ah, i just discovered the switch statement!
thanks for the help, I think I've got it now!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 209 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC