java - Exception, need to learn what is wrong? -
java - Exception, need to learn what is wrong? -
{
private static final int stringindexoutofboundsexception = 0; //values shared within class private static int sidea = 0; private static int sideb = 0; //main method public static void main(string [] args) { system.out.println("usage: supply 2 integer values triangle sides."); system.out.println(" a-integer value"); system.out.println(" b-integer value"); system.out.println(" c-attempt pythagorean calculation"); system.out.println(" q-quit program"); string value = null; string side; char c = 0; int s1 =stringindexoutofboundsexception; scanner scanner = new scanner(system.in); boolean carryon=true; while(carryon) //loop until user has finished. { side = joptionpane.showinputdialog(null, "a or b?"); seek { c =side.charat(0); } catch(nullpointerexception np){ system.out.println("thanks done!"); } switch(c) //which side user trying set { case 'q': carryon= false; //quit programme break; case 'a': try{ value = joptionpane.showinputdialog(null, "enter a"); sidea = integer.parseint(value); } catch(numberformatexception nf){ system.out.println("thats not number. type in integer."); break; } if (sidea<=0) { system.out.println("cannot compute because zero. seek integer"); break; } if(sidea>0) { system.out.println("you've inputed value. value "+sidea); break; } break; case 'b': try{ value = joptionpane.showinputdialog(null, "enter b"); sideb = integer.parseint(value); } catch(numberformatexception nf){ system.out.println("thats not number. type in integer."); break; } if (sideb<=0) { system.out.println("cannot compute because b zero. seek integer"); break; } if(sideb>0) { system.out.println("you've inputed b value. value "+sideb); break; } break; case 'c': //calculate reply double temporary = (sidea * sidea) + (sideb * sideb); if(sidea <=0 && sideb <=0){ system.out.println("you don't have triangle. seek again"); break; } double result = java.lang.math.sqrt(temporary); system.out.println("the hypotenuse value "+result); break; } } system.out.println("thank you. goodbye!"); return; }
}
and error is:
exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 0 @ java.lang.string.charat(string.java:658) @ lab1.lab01.main(lab01.java:42)
what wrong?
while string
isn't exactly char[]
in c, operations, it's improve think of one.
when effort index array empty or uninitialized, you'll similar exception - arrayindexoutofboundsexception
. means you're attempting index doesn't exist.
we know array of size n can indexed location n-1. n = 0, (by math) indexing location -1, which not permissible.
the same thing happening when perform charat()
. you're attempting retrieve value doesn't exist. in other words: your string empty.
the culprit line:
c =side.charat(0);
if side
empty, you're stuck.
when go retrieve value side
, in line:
side = joptionpane.showinputdialog(null, "a or b?");
...add check ensure isn't empty, , wait until valid length.
string side = joptionpane.showinputdialog(null, "a or b?"); while(side.isempty()) { joptionpane.showmessagedialog("invalid input!"); side = joptionpane.showinputdialog(null, "a or b?"); }
java exception
Comments
Post a Comment