0

I am trying to make a program where you can enter a credit card number and it will spit out the number back at you with a ASCII letter/symbol on the end using the remainder of the added digits divided by 26. I feel like my code is right although when I run the program, no symbol shows up. I do not get debug errors or anything, but my (char) symbol just doesn't show up. All it shows is the numbers. Can someone help me please?

Here is what I have so far:

import java.util.*;
import java.text.*;
import java.math.*;

public class Program{

public static void main (String []args){

Scanner keyboard = new Scanner(System.in);

int CC, CC2, CC3, CC4;


System.out.println("Enter your credit card number 2 numbers at a time (XX XX XX XX)");
CC=keyboard.nextInt();
CC2=keyboard.nextInt();
CC3=keyboard.nextInt();
CC4=keyboard.nextInt();

 int CC6;

CC6= (CC+CC4+CC2+CC3)%26;

char CC7;

CC7 = (char)CC6;


System.out.println("The correct number and code is:" +CC+CC2+CC3+CC4+CC7);    

}
}
2
  • Can you post expected input/output? Commented Sep 26, 2013 at 3:47
  • Pro tip: If you're using Eclipse, pressing Ctrl+Shift+F will format your code nicely, and pressing Ctrl+Shift+O will clean up your imports. Consider doing that before posting! Commented Sep 26, 2013 at 3:58

3 Answers 3

1

I believe you're looking for

Character.toChars(CC6);

Make sure that when you're testing, you're using a value which actually maps to a decent looking value. For example Character.toChars(65) results in 'A'.

For further reference, see: Converting stream of int's to char's in java

Sign up to request clarification or add additional context in comments.

2 Comments

When I tried that it just gave me the number instead of the character? code CC6= ((CC+CC4+CC2+CC3)%26)+65; Character.toChars(CC6); System.out.println("The correct number and code is:" +CC+CC2+CC3+CC4); System.out.println(+CC6);
Character.toChars(int) returns a char[]. You need to either store it somewhere, or use it directly in the println.
0

Print them separately and you can see some weird symbol coming in the console.

System.out.println("The correct number and code is:" + CC + CC2 + CC3 + CC4);
System.out.println(CC7);

The thing is, you'll get the CC7 in the range of 0-25 only since you're doing a mod 26 and this range contains ASCII codes of non-character keys.

Actual character(or for that case, special characters) start from ASCII code 33. Have a look at the ASCII table here.

Comments

0

There is a character there its just one that isnt display well with the console. Probably being rendered as a blank space. I see you are using the remainder of 26 so im guessing you want it to be a letter (a-z). The ascii characters for letters start at 65 for capitals and 97 for lower case.

Make this change

CC6= (CC+CC4+CC2+CC3)%26+65;

and you will see a letter between A and Z being printed out.

You can view the full ASCII table here http://www.asciitable.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.