Tuesday 15 May 2012

java - Variable might not have been initialized when it has been initialized and declared -



java - Variable might not have been initialized when it has been initialized and declared -

when compiled says variable secondnumberstring might not have been initialized. have declared string , initialized in secondnumber switch. doing wrong here? want convert secondnumber, come in 5, string called seondnumberstring , have 5 , display it. must using switch.

/* programmer: date: wednesday, oct 8, 2014 description: simple calculator */ import javax.swing.joptionpane; // imports joptionpane class public class calculatorstevenhasaka { public static void main(string[] args) { string input; // temporarily hold input string firstnumberstring; // hold string name of first number string secondnumberstring; // hold string name of sec number string operatorstring; // hold string name of operator int firstnumber; // hold first number int secondnumber; // hold sec number int answer; // hold reply char operator; // hold operator // inquire user number 0-9 (the first number) input = joptionpane.showinputdialog(null, "please come in first number. \nit must 0 9. \nno decimals, please.", "calculator v1.0", joptionpane.question_message); // convert input integer firstnumber = integer.parseint(input); // validate input of firstnumber while (firstnumber < 0 || firstnumber > 9) { // inquire user number 0-9 input = joptionpane.showinputdialog(null, "invalid number! \nit must number 0 9. \nno decimals, please.", "invalid number", joptionpane.warning_message); // convert number integer firstnumber = integer.parseint(input); } // end of firstnumber validation // inquire user operator input = joptionpane.showinputdialog(null, "please input operator. \nyou can utilize +, -, *, /, or ^", "calculator v1.0", joptionpane.question_message); // convert input character operator = input.charat(0); // validate input of operator while ((operator != '+') && (operator != '-') && (operator != '*') && (operator != '/') && (operator != '^')) { // inquire user operator input = joptionpane.showinputdialog(null, "invalid operator! \nyou can utilize +, -, *, /, or ^", "invalid operator", joptionpane.warning_message); // convert input character operator = input.charat(0); } // end of operator validation // inquire user number 0-9 (the sec number) input = joptionpane.showinputdialog(null, "please come in sec number. \nit must 0 9. \nno decimals, please.", "calculator v1.0", joptionpane.question_message); // convert number integer secondnumber = integer.parseint(input); // validate input of secondnumber while (secondnumber < 0 || secondnumber > 9) { //ask user number 0-9 input = joptionpane.showinputdialog(null, "invalid number! \nit must number 0 9. \nno decimals, please.", "invalid number", joptionpane.warning_message); // convert number integer secondnumber = integer.parseint(input); } // end of secondnumber validation // convert firstnumber string switch (firstnumber) { case 0: firstnumberstring = "zero"; break; case 1: firstnumberstring = "one"; break; case 2: firstnumberstring = "two"; break; case 3: firstnumberstring = "three"; break; case 4: firstnumberstring = "four"; break; case 5: firstnumberstring = "five"; break; case 6: firstnumberstring = "six"; break; case 7: firstnumberstring = "seven"; break; case 8: firstnumberstring = "eight"; break; case 9: firstnumberstring = "nine"; break; default: joptionpane.showmessagedialog(null, "invalid input!", "error", joptionpane.error_message); } // end of firstnumber switch // convert secondnumber string switch (secondnumber) { case 0: secondnumberstring = "zero"; break; case 1: secondnumberstring = "one"; break; case 2: secondnumberstring = "two"; break; case 3: secondnumberstring = "three"; break; case 4: secondnumberstring = "four"; break; case 5: secondnumberstring = "five"; break; case 6: secondnumberstring = "six"; break; case 7: secondnumberstring = "seven"; break; case 8: secondnumberstring = "eight"; break; case 9: secondnumberstring = "nine"; break; default: joptionpane.showmessagedialog(null, "invalid input!", "error", joptionpane.error_message); } // end of secondnumber switch // convert operator string , perform calculations if (operator == '+') { operatorstring = "plus"; reply = firstnumber + secondnumber; joptionpane.showmessagedialog(null, "blah: " + secondnumberstring); } else if (operator == '-') { operatorstring = "minus"; } else if (operator == '*') { operatorstring = "multiplied by"; } else if (operator == '/') { operatorstring = "divided by"; } else if (operator == '^') { operatorstring = "to powerfulness of"; } else { joptionpane.showmessagedialog(null, "invalid input!", "error", joptionpane.error_message); } // end of operator if/else if/else } // end of main } // end of public class

but have declared string , initialized in secondnumber switch.

well, have if nail of specified cases. default case just:

default: joptionpane.showmessagedialog(null, "invalid input!", "error", joptionpane.error_message);

what expect value of secondnumberstring after that? should assign value there - or exit @ point. given you're expecting have validated secondnumber before switch, i'd throw sort of runtimeexception instead of showing message dialog. compiler know @ point you're not going go ahead , seek utilize variable, would assigned after switch statement.

note though we can tell you'll never nail default case in switch statement, rules definite assignment , reachability in java don't cover thought compiler should reason secondnumber has 1 of 0..9. need tell really, don't expect here - , that's why exception best alternative here.

as aside, code much cleaner if you'd break 1 enormous method lots of different methods. aside else, wouldn't need many local variables. i'd recommend declaring variables @ point of first use, rather declaring @ top. should larn utilize arrays - rid of switch statements entirely...

java

No comments:

Post a Comment