Greetings,
I have been trying to figure this out for hours with no avail. Basically I need to print out user input as Initial value, ASCII(char), ASCII(int), and Hex. I can get everything to work however I am not supposed to have the char values print if its a digit, and int values print if its a char. I can get everything to work except I cannot figure out how to exclude both the letters and digit in the same for loop. The way it has to be printed is quite strict. My code below will print the Initial, ASCII(char) and Hex values. I have another if statement that will work by itself to print the ASCII(int) - but obviously if I include that if statement it will exclude everything.. Any help would be Amazing thank you..
import java.util.*;
/****************************************************************************************
HNUnit3Ch12.java
xx
Write a program that gets input for a string, then output the initial character, ascii
values of the character and number, and hex values of each character. Repeat until quit.
Get input for a string containing both numbers and letters of any length
Use a for loop with postfix notation to increment through the string, printing the
following:
The initial digit/character
The ASCII value of each character
The ASCII value of each integer converted to a string
Only output the ASCII(char) for letters and ASCII(int) for integers
Spaces should output ASCII(char) and hex only
The Hex value of each integer/character output in upper case
Output “Thank you for playing!”
****************************************************************************************/
public class HNUnit3Ch12
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
final String HEADER = "%-10s%-15s%-15s%s";
//final String OUTPUT = "%s\n %-15s\n %-15s\n %s";
String userString;
System.out.print("Please enter a string of any length: ");
userString = input.nextLine();
System.out.printf(HEADER, "\nInitial", "ASCII(char)", "ASCII(int)", "Hex\n");
for (int i = 0; i < userString.length(); i ++)
{
char ch = userString.charAt(i);
char initialCase = userString.charAt(i);
if (!Character.isDigit(ch))
{
int letters = (int) ch;
//if (!Character.isLetter(ch))
// {
// int numbers = (int) ch;
String hex = Integer.toString(ch, 16).toUpperCase();
System.out.printf("%-9s%-9s%-9s%n",initialCase,letters,hex);
}
// }
}
} // End main
} // End public class HNUnit3Ch12