nvibest
10-08-2008, 12:19 AM
Hello all,
For the past 5 or so hours:confused::mad: I've been trying to figure out how to condense this basic code that uses nextInt(); to read the first user input integer.
import java.util.Scanner;
public class DateCalculator {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int month;
int day;
int year;
int[] dayCount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("Enter integers for month, day, and year using 4 digits for the year: ");
month = s.nextInt();
if (s.equals("1")){
month = dayCount[0];
}
else if (s.equals("2")){
month = dayCount[1];
}
else if (s.equals("3")){
month = dayCount[2];
}
else if (s.equals("4")){
month = dayCount[3];
}
else if (s.equals("5")){
month = dayCount[4];
}
else if (s.equals("6")){
month = dayCount[5];
}
else if (s.equals("7")){
month = dayCount[6];
}
else if (s.equals("8")){
month = dayCount[7];
}
else if (s.equals("9")){
month = dayCount[8];
}
else if (s.equals("10")){
month = dayCount[9];
}
else if (s.equals("11")){
month = dayCount[10];
}
else if (s.equals("12")){
month = dayCount[11];
}
day = s.nextInt();
year = s.nextInt();
The above code is what I would like to make short by using a 'for' / 'while' loop (nested) or by using the 'switch' statements.........
........This code below is just me testing for potential ways to accomplish this....
//Displays element value
if (){
day = dayCount[2];
for (int d=0; d<dayCount.length; d++){
}
}
else{
for (int item : dayCount) {
System.out.println(item);
System.out.println(day);
}
}
Then this is how I will display error messages if the user inputs the wrong integers ( I used the 'else if' statement so that if there is more than one error the program will only display one error message.I want the number the user inputs (1-12)--1 being January, 2 being February, etc.-- to be assigned a variable so that later on the program will know which how to differentiate between elements .........
......
if (year < 1582){
System.out.println("Invalid Year");
}
else if (month < 0 || month >12 ){
System.out.println("Invalid Month");
}
}
}
Any help would be greatly appreciated.
Thanks,
N
For the past 5 or so hours:confused::mad: I've been trying to figure out how to condense this basic code that uses nextInt(); to read the first user input integer.
import java.util.Scanner;
public class DateCalculator {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int month;
int day;
int year;
int[] dayCount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("Enter integers for month, day, and year using 4 digits for the year: ");
month = s.nextInt();
if (s.equals("1")){
month = dayCount[0];
}
else if (s.equals("2")){
month = dayCount[1];
}
else if (s.equals("3")){
month = dayCount[2];
}
else if (s.equals("4")){
month = dayCount[3];
}
else if (s.equals("5")){
month = dayCount[4];
}
else if (s.equals("6")){
month = dayCount[5];
}
else if (s.equals("7")){
month = dayCount[6];
}
else if (s.equals("8")){
month = dayCount[7];
}
else if (s.equals("9")){
month = dayCount[8];
}
else if (s.equals("10")){
month = dayCount[9];
}
else if (s.equals("11")){
month = dayCount[10];
}
else if (s.equals("12")){
month = dayCount[11];
}
day = s.nextInt();
year = s.nextInt();
The above code is what I would like to make short by using a 'for' / 'while' loop (nested) or by using the 'switch' statements.........
........This code below is just me testing for potential ways to accomplish this....
//Displays element value
if (){
day = dayCount[2];
for (int d=0; d<dayCount.length; d++){
}
}
else{
for (int item : dayCount) {
System.out.println(item);
System.out.println(day);
}
}
Then this is how I will display error messages if the user inputs the wrong integers ( I used the 'else if' statement so that if there is more than one error the program will only display one error message.I want the number the user inputs (1-12)--1 being January, 2 being February, etc.-- to be assigned a variable so that later on the program will know which how to differentiate between elements .........
......
if (year < 1582){
System.out.println("Invalid Year");
}
else if (month < 0 || month >12 ){
System.out.println("Invalid Month");
}
}
}
Any help would be greatly appreciated.
Thanks,
N