944,052 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 422
  • Java RSS
Nov 9th, 2009
0

Super Newb Troubleshooting

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
luxxnatura is offline Offline
3 posts
since Nov 2009
Nov 9th, 2009
0
Re: Super Newb Troubleshooting
instead of while, use "if"

Java Syntax (Toggle Plain Text)
  1. if(x == 12)
  2. {
  3. System.out.println("December " + x);//put a space after the month also.
  4. }
Featured Poster
Reputation Points: 304
Solved Threads: 277
Posting Virtuoso
thines01 is online now Online
1,694 posts
since Oct 2009
Nov 9th, 2009
0
Re: Super Newb Troubleshooting
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
luxxnatura is offline Offline
3 posts
since Nov 2009
Nov 9th, 2009
0
Re: Super Newb Troubleshooting
Without giving too much away...

Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 304
Solved Threads: 277
Posting Virtuoso
thines01 is online now Online
1,694 posts
since Oct 2009
Nov 9th, 2009
0
Re: Super Newb Troubleshooting
...notice there are no semi-colons after my if statements

Java Syntax (Toggle Plain Text)
  1. if(x==1)
  2. {
  3. System.out.println("January " + x );
  4. }
  5. else ...
Featured Poster
Reputation Points: 304
Solved Threads: 277
Posting Virtuoso
thines01 is online now Online
1,694 posts
since Oct 2009
Nov 9th, 2009
0
Re: Super Newb Troubleshooting
Ah, i just discovered the switch statement!
thanks for the help, I think I've got it now!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
luxxnatura is offline Offline
3 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java
Next Thread in Java Forum Timeline: Consolidate Accounts - Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC