I have an assignment that I'm finding a bit difficult. We're reading an external file to get the numbers needed for the program, which is not a problem. Here is the prompt:

To check the validity of the code you need to analyze each digit in the code including the check digit. Assume that the first digit is in position zero, add the digits in the even positions and multiply the result by 3. Add this to the sum of the odd positioned digits. Only if the total is a multiple of 10 is the UPC number valid. Write a program that reads a UPC code (as a string) from an external file, analyze the code, and output the code and a label “Invalid” or “Valid”.

Sample Output: UPC Code:

016499215511 Valid

372415613274 Invalid

155512994610 Valid

011165459192 Invalid

838241762110 Invalid

Here is what I have in my code so far:

import java.io.*; import java.util.*; public class Prog700d { public static void main(String[] args) throws IOException { Scanner kbReader = new Scanner(new File("C:\\Users\\Guest\\Documents\\java programs\\Prog700d\\Prog700d.in")); while(kbReader.hasNextLine()) { String upc = kbReader.nextLine(); int length = upc.length(); int a = upc.charAt(0); System.out.println(a); 

My main issue is with the charAt(). I think that if I use charAt(), then I will get the character at the given index. So I decided to test this by using charAt(0), and I ended up getting these numbers:

49 55 53 49 51

I am really confused on how I got these numbers, and honestly have no idea what went wrong. I can do the rest of the program (such as the formula, printing out everything, etc.), but I don't know how to fix the charAt() problem. I know I can handle the rest of the assignment, but I have no idea what went wrong with this. If someone could provide some guidance on how to fix issue, it would be greatly appreciated. Thank you!

1

3 Answers

These numbers are UNICODE code points of the digits. If you need to get the digit value, subtract '0' from them:

int a = upc.charAt(0) - '0'; System.out.println(a); 

The reason this expression gives you the digit's value is that UNICODE code points of decimal digits have consecutive integer values: '0' is 48, '1' is 49, '2' is 50, and so on. Subtracting '0' from the code point yields the value of the digit.

EDIT : As of Java 1.5 you can use Character.digit to get the numeric value of the corresponding digit:

int a = Character.digit(upc.charAt(0), 10); System.out.println(a); 
5

charAt returns a value of char type.

change your code to:

String upc = kbReader.nextLine(); int length = upc.length(); char a = upc.charAt(0); int b = Character.getNumericValue(a); System.out.println(b); 

Should be ok like that.

Maybe a bit verbose but....

% cat check.java public class check { public static void main(String[] arg) { String s = arg[0]; if (s.matches("[0-9]{12}")) { int evens = Character.digit(s.charAt(0),10) + Character.digit(s.charAt(2),10) + Character.digit(s.charAt(4),10) + Character.digit(s.charAt(6),10) + Character.digit(s.charAt(8),10) + Character.digit(s.charAt(10),10); int odds = Character.digit(s.charAt(1),10) + Character.digit(s.charAt(3),10) + Character.digit(s.charAt(5),10) + Character.digit(s.charAt(7),10) + Character.digit(s.charAt(9),10) + Character.digit(s.charAt(11),10); int sum = evens * 3 + odds; if ((sum % 10) == 0) { System.out.println("YES"); return; } } System.out.println("NO"); } } % javac check.java % java -classpath . check 016499215511 YES % java -classpath . check 372415613274 NO % java -classpath . check 37241561327 NO % java -classpath . check 01649921551 NO % 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy