Results 1 to 17 of 17
Thread: need help converting
- 04-02-2010, 03:07 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
-
- 04-02-2010, 04:00 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
System.out.println("Enter month: "); //enters month number here
month = scan.nextInt();
if (month==1)
;
system.out.println("month name"); //month name displayed
i dont know something like that
-
One way is to use an array, but I'm not sure if you've learned about these yet. Your line of thinking could work as well, but you'll have to work it through to completion in order to find out. To succeed in learning to program you should experiment with your code as much as possible. Don't be afraid since it's hard to break your computer. :)
So why not keep working on it and see what happens. One other bit of advice, add only a little bit of code to your program at a time and compile often. If you have compile error, then don't add any more code until you've fixed it. Best of luck and happy coding!
- 04-02-2010, 04:17 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
ive been playing with it for a while tried useing toString and other things just cant quite figure it out maybe a hint on how to get the int to convert to a string cause im stuck
-
How stuck? Your little bit of code could work if implemented properly. You could have a bunch of
Or you could have an equivalent effective code using a switch/case statement.Java Code:if (month == 1) { //... } else if (month == 2) { //... }
Let's see your full attempt with this first, and then we'll better know how you're stuck. But don't forget to use code tags when posting code (see the link in my signature). Also, are you supposed to create a method and return a String representing the month?
Best of luck!
- 04-02-2010, 09:46 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
Hi, glad to show my advice to you:
Like your question, I think a 'switch - case' statement will be well needed. The code may like this:
I think u can understand my meaning, good luck!Java Code:switch (month) { case 1: System.out.println("Jan"); break; case 2: System.out.println("Feb"); break; case 3: System.out.println("Mar"); break; ... default: System.out.println("!!! INVALID number !!!"); break;
- 04-02-2010, 10:01 AM #8
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
Here, I show u the total source code.
Java Code:package Test; import java.util.Scanner; public class ShowMonth { public static void main(String[] args) { int month; Scanner scan = new Scanner(System.in); System.out.println("Enter month: "); //enters month number here month = scan.nextInt(); // Get a number from user input do { switch (month) { case 1: System.out.println("Jan"); break; case 2: System.out.println("Feb"); break; case 3: System.out.println("Mar"); break; case 4: System.out.println("Apr"); break; case 5: System.out.println("May"); break; case 6: System.out.println("Jun"); break; case 7: System.out.println("Jul"); break; case 8: System.out.println("Aug"); break; case 9: System.out.println("Sep"); break; case 10: System.out.println("Oct"); break; case 11: System.out.println("Nov"); break; case 12: System.out.println("Dec"); break; default: System.out.println("###INVALID number###"); break; } month = scan.nextInt(); // Make sure it can loop } while (month != 0); // Press '0' to quit } }
- 04-02-2010, 10:57 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
Nah, switches and if-else-if ... elses are so Fortranesque. Use anything that can be indexed and contains the month names.
kind regards,
Jos
- 04-02-2010, 11:36 AM #10
wht's demerit in the following. just an idea...to analyse..suggest !
Java Code:import java.util.Scanner; public class Month { public static void main(String []args) { String[] list = {"Invalid Entry","jan","feb","march","april","may","june","july","august","sept","oct","nov","dec"}; System.out.println("Enter month number"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); try{ System.out.println(list[num]); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("INvalid Entry"); } } }
- 04-02-2010, 11:40 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
- 04-02-2010, 11:46 AM #12
wht abt these.. can u write most effiecient code in ur prospect..Exceptions are slow, far slower than a simple bounds test.
Java Code:import java.util.Scanner; public class Month { public static void main(String []args) { String[] list = {"Invalid Entry","jan","feb","march","april","may","june","july","august","sept","oct","nov","dec"}; System.out.println("Enter month number"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); if(num<=12) System.out.println(list[num]); else System.out.println("INvalid Entry"); } }
- 04-02-2010, 11:57 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
- 04-02-2010, 12:09 PM #14
please detail it . as example ?If you do bounds checking do it well.
Regards
- 04-02-2010, 12:32 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
- 04-02-2010, 12:35 PM #16
thts wat i was expecting.Imagine what would happen if the user types -42.
- 04-02-2010, 01:07 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Is it OK to do this... (converting int to string)
By Psyclone in forum New To JavaReplies: 1Last Post: 02-16-2010, 05:51 PM -
converting a decimal to an int
By shuks in forum New To JavaReplies: 9Last Post: 10-12-2009, 09:41 AM -
Converting an App to Applet
By josephdcoleman in forum New To JavaReplies: 1Last Post: 02-21-2009, 07:07 AM -
Converting URL to URI
By Java Tip in forum Java TipReplies: 0Last Post: 12-26-2007, 10:15 AM -
help with converting to JApplet
By Simmy in forum AWT / SwingReplies: 2Last Post: 08-09-2007, 08:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks