Results 1 to 17 of 17
  1. #1
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default need help converting

    i need help converting 1 to jan 2 to feb ect.
    the user will enter a number then i need to have the name of the month come out after words
    sorry for the poor wording and thanks for the help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sr20guy View Post
    i need help converting 1 to jan 2 to feb ect.
    the user will enter a number then i need to have the name of the month come out after words
    So what do you have so far? What is currently and specifically tripping you up?

  3. #3
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sr20guy View Post
    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!

  5. #5
    sr20guy is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sr20guy View Post
    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
    Java Code:
    if (month == 1) {
      //...
    } 
    else if (month == 2) {
      //...
    }
    Or you could have an equivalent effective code using a switch/case statement.

    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!

  7. #7
    wangwei is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    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:
    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;
    I think u can understand my meaning, good luck!

  8. #8
    wangwei is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    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
    	}
    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Nah, switches and if-else-if ... elses are so Fortranesque. Use anything that can be indexed and contains the month names.

    kind regards,

    Jos

  10. #10
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    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");
    		}
    	}
    
    }

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by programmer_007 View Post
    wht's demerit in the following. just an idea...to analyse..suggest !
    Exceptions are slow, far slower than a simple bounds test.

    kind regards,

    Jos

  12. #12
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    Exceptions are slow, far slower than a simple bounds test.
    wht abt these.. can u write most effiecient code in ur prospect..
    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");
    		
    	}
    
    }

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by programmer_007 View Post
    wht abt these.. can u write most effiecient code in ur prospect..
    Java Code:
    		if(num<=12)
    If you do bounds checking do it well.

    kind regards,

    Jos

  14. #14
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    If you do bounds checking do it well.
    please detail it . as example ?
    Regards

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by programmer_007 View Post
    please detail it . as example ?
    Regards
    Imagine what would happen if the user types -42.

    kind regards,

    Jos

  16. #16
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    Imagine what would happen if the user types -42.
    thts wat i was expecting.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by programmer_007 View Post
    thts wat i was expecting.
    But your code wasn't ...

    kind regards,

    Jos

Similar Threads

  1. Is it OK to do this... (converting int to string)
    By Psyclone in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 05:51 PM
  2. converting a decimal to an int
    By shuks in forum New To Java
    Replies: 9
    Last Post: 10-12-2009, 09:41 AM
  3. Converting an App to Applet
    By josephdcoleman in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 07:07 AM
  4. Converting URL to URI
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:15 AM
  5. help with converting to JApplet
    By Simmy in forum AWT / Swing
    Replies: 2
    Last Post: 08-09-2007, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •