Click to See Complete Forum and Search --> : I know how to use the switch statement with integers but need help for letters!


s3a
September 17th, 2009, 06:34 PM
The part in bold is the part I am having trouble with. What I am trying to do is this: If you don't do stupidities, you could enter 2009 9 17 for example and it will print the date, etc. Not so long ago, I had it set up successfully (no switch statement) so that if someone were to type lets say 2k9 9 17 it would print Invalid "2k9" and stuff like that. But if the user enters like 2009 nine 17, the program would get "confused" and crash. I'm reading my book which seems to indicate that case 0:, case 1:, case 2:, etc is for integers (as suggested also by the compilation error) and you need to do something a little different for letters.

I would very much appreciate it if someone could modify my code and paste it all back in a code window so that I can see how it works.

The following is my code:

import java.util.Scanner;



public class Assignment_2A



{



public static void main (String [] args)



{



Scanner kb = new Scanner(System.in);



System.out.print("Enter the Year, Month and Day: ");



if(! kb.hasNextInt())

{

String junk = kb.next();



switch(junk)



{

case 1:



System.out.println("Invalid " + "\"" + junk + "\"");

System.out.println("Must be an Integer." + "\n" + "Bye.");

System.exit(0);

break;



case 2:



System.out.println("Invalid " + "\"" + junk + "\"");

System.out.println("Must be an Integer." + "\n" + "Bye.");

System.exit(0);

break;



case 3:



System.out.println("Invalid " + "\"" + junk + "\"");

System.out.println("Must be an Integer." + "\n" + "Bye.");

System.exit(0);

break;







/*System.out.println("Invalid " + "\"" + junk + "\"");

System.out.println("Must be an Integer." + "\n" + "Bye.");

System.exit(0);*/



}

}



/* What do I modify so that the user can input anything and so that if it's a number, the calculations will follow

like normal but if it's letters, there will be a System.out.print(); error message? */



int year = (int)kb.nextDouble();

int month = (int)kb.nextDouble();

int day = (int)kb.nextDouble();



/* leap year if year is divisible by 4 AND is not century year

OR if it is divisible by 400 (explanation of the following leap year statement) */



boolean isLeapYear = (year % 400 == 0) && (year % 100 != 0) || (year % 4 == 0);



if(year < 1582)

{

System.out.println("Invalid year " + "\"" + year + "\"");

System.exit(0);

}



if((month < 1) || (month > 12))

{

System.out.println("Invalid month " + month);

System.exit(0);

}



/* Figure out what to do for the fact that months can be 30 or 31 days

so that it can say invalid day if a number 31 is entered on a month

that has only 30 days. */



if ((day > 31) & (day < 1))



{

System.out.println("Invalid day " + "\"" + day + "\"");

System.exit(0);

}





//if(month == 1)

// System.out.print("Date: January, " + day + ", " + year);

/*

if((month == 2) && (isLeapYear == true) && ((day > 29) || (day < 1)))

{

System.out.println("Invalid day " + "\"" + day + "\"");

System.exit(0);

}

else if(month == 2)

System.out.print("Date: February, " + day + ", " + year);

*/

switch(month)

{

case 1: System.out.print("Date: January, " + day + ", " + year);

System.out.println("\n" + "Day number: " + day); break;



case 2: if((((isLeapYear == true) && ((day > 29) || (day < 1)))) || ((isLeapYear == false) && (day > 28)))

{

System.out.println("Invalid day " + "\"" + day + "\"");

System.exit(0);

}

else

System.out.print("Date: February, " + day + ", " + year);

// The following line is not part of the conditional statement.

System.out.println("\n" + "Day number: " + (31 + day)); break;



case 3:

System.out.print("Date: March, " + day + ", " + year);



if(isLeapYear == true)

System.out.println("\n" + "Day number: " + (31 + 29 + day));



else

System.out.println("\n" + "Day number: " + (31 + 28 + day));



break;



case 4:

System.out.print("Date: April, " + day + ", " + year);



if(isLeapYear == true)

System.out.print("\n" + "Day number: " + ((31 + 29 + 31) + day));



else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + day)); break;



case 5:

System.out.print("Date: May, " + day + ", " + year);



if(isLeapYear == true)

System.out.print("\n" + "Day number: " + (31 + 29 + 31 + 30 + day));

else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + 30 + day));



break;



case 6:

System.out.print("Date: June, " + day + ", " + year);



if(isLeapYear = true)

System.out.print("\n" + "Day number: " + (31 + 29 + 31 + 30 + 31 + day));



else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + 30 + 31 + day));



break;



case 7:

System.out.print("Date: July, " + day + ", " + year);



if(isLeapYear = true)

System.out.print("\n" + "Day number: " + (31 + 29 + 31 + 30 + 31 + 30 + day));



else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + 30 + 31 + 30 + day));



break;



case 8:

System.out.print("Date: August, " + day + ", " + year);



if(isLeapYear = true)

System.out.print("\n" + "Day number: " + (31 + 29 + 31 + 30 + 31 + 30 + 31 + day));



else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + 30 + 31 + 30 + 31 + day));



break;



case 9:

System.out.print("Date: September, " + day + ", " + year);



if((month == 9) && (isLeapYear = true))

System.out.print("\n" + "Day number: " + (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day));



else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day));



break;



case 10:

System.out.print("Date: October, " + day + ", " + year);



if(isLeapYear = true)

System.out.print("\n" + "Day number: " + (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day));



else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day));



break;



case 11:

System.out.print("Date: November, " + day + ", " + year);





if(isLeapYear = true)

System.out.print("\n" + "Day number: " + (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day));



else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day));



break;



case 12:

System.out.print("Date: December, " + day + ", " + year);



if(isLeapYear = true)

System.out.print("\n" + "Day number: " + (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day));



else

System.out.print("\n" + "Day number: " + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day));



break;

}

}



}

Thanks in advance!

s3a
September 17th, 2009, 06:59 PM
Actually I don't want to use the switch statement, instead I want it to just take the first 3 inputted strings (seperated by a space).

jcaccia
September 18th, 2009, 03:34 AM
Actually I don't want to use the switch statement, instead I want it to just take the first 3 inputted strings (seperated by a space).
Then use sc.next() three times or perhaps better sc.nextLine(). This will read all the tokens to the end of the line; then you can split the input into an array of Strings and process them. Remember to check that the split produced the correct number of strings.

holestary
September 18th, 2009, 05:36 AM
have a look this links..

http://www.daniweb.com/forums/thread88098.html

http://www.java2s.com/Code/Java/Data-Type/switchwithcharvalue.htm

Deliverance
September 18th, 2009, 08:11 AM
http://mail.openjdk.java.net/pipermail/coin-dev/2009-February/000001.html

You'll have to wait for JDK 7 for switch support with Strings!

The full list is here:
http://blogs.sun.com/darcy/entry/project_coin_final_five

s3a
September 18th, 2009, 06:53 PM
Changing

if(! kb.hasNextInt())

{

String junk = kb.next();

System.out.println("Invalid " + "\"" + junk + "\"");

System.out.println("Must be an Integer." + "\n" + "Bye.");

System.exit(0);

}



int year = kb.nextInt();


int month = kb.nextInt();

int day = kb.nextInt();

to:

if(! kb.hasNextInt())

{

String junk = kb.next();

System.out.println("Invalid " + "\"" + junk + "\"");

System.out.println("Must be an Integer." + "\n" + "Bye.");

System.exit(0);

}



int year = kb.nextInt();



if(! kb.hasNextInt())

{

String junk = kb.next();

System.out.println("Invalid " + "\"" + junk + "\"");

System.out.println("Must be an Integer." + "\n" + "Bye.");

System.exit(0);

}

int month = kb.nextInt();



if(! kb.hasNextInt())

{

String junk = kb.next();

System.out.println("Invalid " + "\"" + junk + "\"");

System.out.println("Must be an Integer." + "\n" + "Bye.");

System.exit(0);

}

int day = kb.nextInt();

worked! (My friend told me)

s3a
September 18th, 2009, 06:59 PM
It'd be interesting to have that string and switch combination as for kb.next() I now understand what you mean because my friend told me but no offense I wouldn't have picked that up without an example.

Now, I'm satisfied and calm that I got this working successfully, I'm guessing that I can make my code more efficient with kb.nextLine() and seperate the three inputs instead of having three kb.next() blocks like you said but could you modify my "section" of code and send me the modification in a code window so that I can know how to do it please?