loops - Java switch not working -
im having problem getting switch working. uncompilable source code date.java:75. also, when programme returns results - homecoming month name dd, yyyy along have doing mm/dd/yyyy. if point me in right direction, i'd appreciate it.
import java.util.calendar; import java.util.gregoriancalendar; import java.util.scanner; public class date { private gregoriancalendar date = null; private string[] months = new string[]{ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; public date(int month, int day, int year) { date = new gregoriancalendar(year, month-1, day); } public date(string month, int day, int year) { date = new gregoriancalendar(year, this.getmonth(month), day); } public date(int dayofyear, int year) { date = new gregoriancalendar(); date.set(calendar.day_of_year, dayofyear); date.set(calendar.year, year); } private int getmonth(string month) { (int i=0; i<months.length; ++i) if (month.tolowercase().equals(months)) //equals(months)) homecoming i; homecoming 0; } public string tostring() { homecoming date.get(calendar.month)+1 + "-" + date.get(calendar.date) + "-" + date.get(calendar.year); } public static void main(string[] args) { int mo; int dy; int yr; string moo; // month name string boolean wronginput = false; { scanner input = new scanner( system.in ); // scanner read input wronginput = false; int menu = input.nextint(); // menu selection system.out.printf( "enter 1 format: mm/dd/yyyy \n"); system.out.printf( "enter 2 format: month dd,yyyy \n"); system.out.printf( "enter 3 exit \n"); system.out.printf( "choice:"); switch(menu) { case '1' : // mm/dd/yyyy ui system.out.printf( "enter month (1-12): "); mo = input.nextint(); system.out.printf( "enter day of month: "); dy = input.nextint(); system.out.printf( "enter year: "); yr = input.nextint(); date = new date(mo, dy, yr); //chew system.out.println(a); //spit break; case '2' : // month dd,yyyy ui system.out.printf( "enter month name: "); moo = input.next(); system.out.printf( "enter day of month: "); dy = input.nextint(); system.out.printf( "enter year: "); yr = input.nextint(); date b = new date(moo, dy, yr); //chew system.out.println(b); //spit break; case '3' : // eop system.exit(0); break; default: system.out.println("invalid selection."); wronginput = true; break; } while(wronginput); }
you passing integer , checking character. 1
int
while '1'
character in java.
in code menu
int
not char
cases should case 1:
, not case '1':
so alter switch
switch(menu){ case 1: //your code break; case 2: //your code break; //..and on }
right ascii
values of characters '1','2'..
compared int
value passed menu
not going equal ascii
value of char '1'
81
while 82
'2'
totally useless in scenario.
java loops switch-statement
No comments:
Post a Comment