Friday, 15 February 2013

java - How to stop infinite loop for UPC check code -



java - How to stop infinite loop for UPC check code -

my code has infinite loop , cannot seem break it. also, when inserting blank line, told there index error. please help!

the first step of checking valid upc adding odd position digits , multiplying three, add together number sum of position digits. compute remainder when divided 10. if remainder not zero, subtract remainder 10 check digit. if remainder zero, check digit should 0.

public static void main(string[] args) { scanner in = new scanner (system.in); system.out.println("enter upc (enter blank line quit): "); string enterupc = in.nextline(); int length = enterupc.length(); int checkdigit=0; char char1, char2, char3, char4, char5, char6, char7, char8, char9, char10, char11, char12; int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12; char1 = enterupc.charat(0); num1 = character.getnumericvalue(char1); char2 = enterupc.charat(1); num2 = character.getnumericvalue(char2); char3 = enterupc.charat(2); num3 = character.getnumericvalue(char3); char4 = enterupc.charat(3); num4 = character.getnumericvalue(char4); char5 = enterupc.charat(4); num5 = character.getnumericvalue(char5); char6 = enterupc.charat(5); num6 = character.getnumericvalue(char6); char7 = enterupc.charat(6); num7 = character.getnumericvalue(char7); char8 = enterupc.charat(7); num8 = character.getnumericvalue(char8); char9 = enterupc.charat(8); num9 = character.getnumericvalue(char9); char10 = enterupc.charat(9); num10 = character.getnumericvalue(char10); char11 = enterupc.charat(10); num11 = character.getnumericvalue(char11); char12 = enterupc.charat(11); num12 = character.getnumericvalue(char12); while(length > 0) { //algorithm step 1 , algorithm step 2 int stepone = (num1 + num3 + num5 + num7 + num9 + num11) * 3; int steptwo = stepone + (num2 + num4 + num6 + num8 + num10); while (length == 12) { //algorithm step 3 if(steptwo%10!=0) { checkdigit = 10 - (steptwo%10); } else { checkdigit = 0;} system.out.println("check digit should : " + checkdigit); system.out.println("check digit is: " + num12); } } if(checkdigit == num12) { system.out.println("upc valid"); } else { system.out.println("upc not valid"); } if (length == 0){ system.out.println("error! upc must have 12 digits"); } else{ system.out.print("goodbye!"); } } }

you need decrement length in while loops, otherwise length equal 12 or greater 0, etc. also, may want consider using ints rather typing out declarations. loop through them using loop temp char variable don't have initialize of them that. loop this:

char[] arraychars = new char[enterupc.length()]; int[] arrayints = new int[enterupc.length()]; for(int i=0;i<enterupc.length();i++) { char temp=enterupc.charat(0); num[i] = character.getnumericvalue(temp); }

then in while loop, loop through int array , add together 0+all indices , multiply 3 , add together odd indices. also, never check if line blank.

java eclipse loops barcode infinite

No comments:

Post a Comment