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
Printable View
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
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!
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.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!
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!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;
Here, I show u the total source code.
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
}
}
Nah, switches and if-else-if ... elses are so Fortranesque. Use anything that can be indexed and contains the month names.
kind regards,
Jos
wht's demerit in the following. just an idea...to analyse..suggest !
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");
}
}
}
wht abt these.. can u write most effiecient code in ur prospect..Quote:
Exceptions are slow, far slower than a simple bounds test.
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");
}
}
please detail it . as example ?Quote:
If you do bounds checking do it well.
Regards
thts wat i was expecting.Quote:
Imagine what would happen if the user types -42.