Printing a number based on a user-provided number
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Is there a better way to ask for input and give answers, but without so many lines? Each input will give a specific output according to a key that I use, but this many lines seems excessive. I'm a beginner to java and could use some ideas on how to optimize this coding.
import java.util.Scanner;
class Test
public static void main (String args)
System.out.print ("Enter the day of the month - Answer: ");
Scanner inputFigure = new Scanner (System.in);
int i = inputFigure.nextInt ();
if ( i==1)
System.out.println ("12");
else if (i==2)
System.out.println ("13");
else if (i==3)
System.out.println ("14");
else if (i==4)
System.out.println ("11");
else if (i==5)
System.out.println ("12");
else if (i==6)
System.out.println ("13");
else if (i==7)
System.out.println ("14");
else if (i==8)
System.out.println ("11");
else if (i==9)
System.out.println ("12");
else if (i==10)
System.out.println ("21");
else if (i==11)
System.out.println ("22");
else if (i==12)
System.out.println ("23");
else if (i==13)
System.out.println ("24");
else if (i==14)
System.out.println ("21");
else if (i==15)
System.out.println ("22");
else if (i==16)
System.out.println ("23");
else if (i==17)
System.out.println ("24");
else if (i==18)
System.out.println ("21");
else if (i==19)
System.out.println ("22");
else if (i==20)
System.out.println ("31");
else if (i==21)
System.out.println ("32");
else if (i==22)
System.out.println ("33");
else if (i==23)
System.out.println ("34");
else if (i==24)
System.out.println ("31");
else if (i==25)
System.out.println ("32");
else if (i==26)
System.out.println ("33");
else if (i==27)
System.out.println ("34");
else if (i==28)
System.out.println ("31");
else if (i==29)
System.out.println ("32");
else if (i==30)
System.out.println ("41");
else if (i==31)
System.out.println ("42");
else
System.out.println ("Invalid entry for the day. Try again.");
java beginner
add a comment |Â
up vote
1
down vote
favorite
Is there a better way to ask for input and give answers, but without so many lines? Each input will give a specific output according to a key that I use, but this many lines seems excessive. I'm a beginner to java and could use some ideas on how to optimize this coding.
import java.util.Scanner;
class Test
public static void main (String args)
System.out.print ("Enter the day of the month - Answer: ");
Scanner inputFigure = new Scanner (System.in);
int i = inputFigure.nextInt ();
if ( i==1)
System.out.println ("12");
else if (i==2)
System.out.println ("13");
else if (i==3)
System.out.println ("14");
else if (i==4)
System.out.println ("11");
else if (i==5)
System.out.println ("12");
else if (i==6)
System.out.println ("13");
else if (i==7)
System.out.println ("14");
else if (i==8)
System.out.println ("11");
else if (i==9)
System.out.println ("12");
else if (i==10)
System.out.println ("21");
else if (i==11)
System.out.println ("22");
else if (i==12)
System.out.println ("23");
else if (i==13)
System.out.println ("24");
else if (i==14)
System.out.println ("21");
else if (i==15)
System.out.println ("22");
else if (i==16)
System.out.println ("23");
else if (i==17)
System.out.println ("24");
else if (i==18)
System.out.println ("21");
else if (i==19)
System.out.println ("22");
else if (i==20)
System.out.println ("31");
else if (i==21)
System.out.println ("32");
else if (i==22)
System.out.println ("33");
else if (i==23)
System.out.println ("34");
else if (i==24)
System.out.println ("31");
else if (i==25)
System.out.println ("32");
else if (i==26)
System.out.println ("33");
else if (i==27)
System.out.println ("34");
else if (i==28)
System.out.println ("31");
else if (i==29)
System.out.println ("32");
else if (i==30)
System.out.println ("41");
else if (i==31)
System.out.println ("42");
else
System.out.println ("Invalid entry for the day. Try again.");
java beginner
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Is there a better way to ask for input and give answers, but without so many lines? Each input will give a specific output according to a key that I use, but this many lines seems excessive. I'm a beginner to java and could use some ideas on how to optimize this coding.
import java.util.Scanner;
class Test
public static void main (String args)
System.out.print ("Enter the day of the month - Answer: ");
Scanner inputFigure = new Scanner (System.in);
int i = inputFigure.nextInt ();
if ( i==1)
System.out.println ("12");
else if (i==2)
System.out.println ("13");
else if (i==3)
System.out.println ("14");
else if (i==4)
System.out.println ("11");
else if (i==5)
System.out.println ("12");
else if (i==6)
System.out.println ("13");
else if (i==7)
System.out.println ("14");
else if (i==8)
System.out.println ("11");
else if (i==9)
System.out.println ("12");
else if (i==10)
System.out.println ("21");
else if (i==11)
System.out.println ("22");
else if (i==12)
System.out.println ("23");
else if (i==13)
System.out.println ("24");
else if (i==14)
System.out.println ("21");
else if (i==15)
System.out.println ("22");
else if (i==16)
System.out.println ("23");
else if (i==17)
System.out.println ("24");
else if (i==18)
System.out.println ("21");
else if (i==19)
System.out.println ("22");
else if (i==20)
System.out.println ("31");
else if (i==21)
System.out.println ("32");
else if (i==22)
System.out.println ("33");
else if (i==23)
System.out.println ("34");
else if (i==24)
System.out.println ("31");
else if (i==25)
System.out.println ("32");
else if (i==26)
System.out.println ("33");
else if (i==27)
System.out.println ("34");
else if (i==28)
System.out.println ("31");
else if (i==29)
System.out.println ("32");
else if (i==30)
System.out.println ("41");
else if (i==31)
System.out.println ("42");
else
System.out.println ("Invalid entry for the day. Try again.");
java beginner
Is there a better way to ask for input and give answers, but without so many lines? Each input will give a specific output according to a key that I use, but this many lines seems excessive. I'm a beginner to java and could use some ideas on how to optimize this coding.
import java.util.Scanner;
class Test
public static void main (String args)
System.out.print ("Enter the day of the month - Answer: ");
Scanner inputFigure = new Scanner (System.in);
int i = inputFigure.nextInt ();
if ( i==1)
System.out.println ("12");
else if (i==2)
System.out.println ("13");
else if (i==3)
System.out.println ("14");
else if (i==4)
System.out.println ("11");
else if (i==5)
System.out.println ("12");
else if (i==6)
System.out.println ("13");
else if (i==7)
System.out.println ("14");
else if (i==8)
System.out.println ("11");
else if (i==9)
System.out.println ("12");
else if (i==10)
System.out.println ("21");
else if (i==11)
System.out.println ("22");
else if (i==12)
System.out.println ("23");
else if (i==13)
System.out.println ("24");
else if (i==14)
System.out.println ("21");
else if (i==15)
System.out.println ("22");
else if (i==16)
System.out.println ("23");
else if (i==17)
System.out.println ("24");
else if (i==18)
System.out.println ("21");
else if (i==19)
System.out.println ("22");
else if (i==20)
System.out.println ("31");
else if (i==21)
System.out.println ("32");
else if (i==22)
System.out.println ("33");
else if (i==23)
System.out.println ("34");
else if (i==24)
System.out.println ("31");
else if (i==25)
System.out.println ("32");
else if (i==26)
System.out.println ("33");
else if (i==27)
System.out.println ("34");
else if (i==28)
System.out.println ("31");
else if (i==29)
System.out.println ("32");
else if (i==30)
System.out.println ("41");
else if (i==31)
System.out.println ("42");
else
System.out.println ("Invalid entry for the day. Try again.");
java beginner
edited Feb 26 at 20:28
200_success
123k14142399
123k14142399
asked Feb 26 at 19:56
Barnabas Showl
114
114
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:
public static String outputForDay(int day)
if (!(1 <= day && day <= 31))
throw new IllegalArgumentException("Invalid entry for the day.");
int onesDigit = day % 10,
tensDigit = day / 10;
return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);
Then, you would call the function like this:
public static void main(String args)
try (Scanner scanner = new Scanner(System.in))
System.out.print("Enter the day of the month: ");
System.out.println(outputForDay(scanner.nextInt()));
catch (IllegalArgumentException e)
System.out.println(e.getMessage());
add a comment |Â
up vote
0
down vote
You can use what is called a Map.
So basically you will create your map with
Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();
Your case would be something like this:
map<Integer, String> map = new HashMap<Integer, String>();
Then put the values as so:
map.put("World/Value", "What is would equal");
in your case, it would be
map.put(1, "12").put(2,"13").build();
To do what you are trying to do you can create a for loop and
System.out.println(map.get(VALUE));
For example,
System.out.println(map.get(1));
and this would return "12"
Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
â Imus
Feb 28 at 8:16
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:
public static String outputForDay(int day)
if (!(1 <= day && day <= 31))
throw new IllegalArgumentException("Invalid entry for the day.");
int onesDigit = day % 10,
tensDigit = day / 10;
return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);
Then, you would call the function like this:
public static void main(String args)
try (Scanner scanner = new Scanner(System.in))
System.out.print("Enter the day of the month: ");
System.out.println(outputForDay(scanner.nextInt()));
catch (IllegalArgumentException e)
System.out.println(e.getMessage());
add a comment |Â
up vote
3
down vote
The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:
public static String outputForDay(int day)
if (!(1 <= day && day <= 31))
throw new IllegalArgumentException("Invalid entry for the day.");
int onesDigit = day % 10,
tensDigit = day / 10;
return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);
Then, you would call the function like this:
public static void main(String args)
try (Scanner scanner = new Scanner(System.in))
System.out.print("Enter the day of the month: ");
System.out.println(outputForDay(scanner.nextInt()));
catch (IllegalArgumentException e)
System.out.println(e.getMessage());
add a comment |Â
up vote
3
down vote
up vote
3
down vote
The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:
public static String outputForDay(int day)
if (!(1 <= day && day <= 31))
throw new IllegalArgumentException("Invalid entry for the day.");
int onesDigit = day % 10,
tensDigit = day / 10;
return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);
Then, you would call the function like this:
public static void main(String args)
try (Scanner scanner = new Scanner(System.in))
System.out.print("Enter the day of the month: ");
System.out.println(outputForDay(scanner.nextInt()));
catch (IllegalArgumentException e)
System.out.println(e.getMessage());
The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:
public static String outputForDay(int day)
if (!(1 <= day && day <= 31))
throw new IllegalArgumentException("Invalid entry for the day.");
int onesDigit = day % 10,
tensDigit = day / 10;
return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);
Then, you would call the function like this:
public static void main(String args)
try (Scanner scanner = new Scanner(System.in))
System.out.print("Enter the day of the month: ");
System.out.println(outputForDay(scanner.nextInt()));
catch (IllegalArgumentException e)
System.out.println(e.getMessage());
answered Feb 26 at 20:44
200_success
123k14142399
123k14142399
add a comment |Â
add a comment |Â
up vote
0
down vote
You can use what is called a Map.
So basically you will create your map with
Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();
Your case would be something like this:
map<Integer, String> map = new HashMap<Integer, String>();
Then put the values as so:
map.put("World/Value", "What is would equal");
in your case, it would be
map.put(1, "12").put(2,"13").build();
To do what you are trying to do you can create a for loop and
System.out.println(map.get(VALUE));
For example,
System.out.println(map.get(1));
and this would return "12"
Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
â Imus
Feb 28 at 8:16
add a comment |Â
up vote
0
down vote
You can use what is called a Map.
So basically you will create your map with
Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();
Your case would be something like this:
map<Integer, String> map = new HashMap<Integer, String>();
Then put the values as so:
map.put("World/Value", "What is would equal");
in your case, it would be
map.put(1, "12").put(2,"13").build();
To do what you are trying to do you can create a for loop and
System.out.println(map.get(VALUE));
For example,
System.out.println(map.get(1));
and this would return "12"
Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
â Imus
Feb 28 at 8:16
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can use what is called a Map.
So basically you will create your map with
Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();
Your case would be something like this:
map<Integer, String> map = new HashMap<Integer, String>();
Then put the values as so:
map.put("World/Value", "What is would equal");
in your case, it would be
map.put(1, "12").put(2,"13").build();
To do what you are trying to do you can create a for loop and
System.out.println(map.get(VALUE));
For example,
System.out.println(map.get(1));
and this would return "12"
You can use what is called a Map.
So basically you will create your map with
Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();
Your case would be something like this:
map<Integer, String> map = new HashMap<Integer, String>();
Then put the values as so:
map.put("World/Value", "What is would equal");
in your case, it would be
map.put(1, "12").put(2,"13").build();
To do what you are trying to do you can create a for loop and
System.out.println(map.get(VALUE));
For example,
System.out.println(map.get(1));
and this would return "12"
edited Feb 26 at 22:01
answered Feb 26 at 20:47
RithvikK
114
114
Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
â Imus
Feb 28 at 8:16
add a comment |Â
Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
â Imus
Feb 28 at 8:16
Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
â Imus
Feb 28 at 8:16
Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
â Imus
Feb 28 at 8:16
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188395%2fprinting-a-number-based-on-a-user-provided-number%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password