Reputation: 11
so im making a program and one of the the things im trying to do is print a table of converted ASCII Characters like this
DEC HEX OCT DEC HEX OCT DEC HEX OCT DEC HEX OCT
A 65 101 25 C 66 111 232 E 12 32 12 G 21 56 12
B 12 89 23 D 45 124 23 F 34 123 10 H 89 203 8
i have done the conversion part of the program i just need a bit of help getting the table to print the right way
anyone have any suggestions?
my code
public static void getInputs(String[] args) {
//c1, c2, and c3 are the characters the user enters
//selector is what determends how the method will print
char c1 = args[0].charAt(0);
char c2 = args[1].charAt(0);
char c3 = args[2].charAt(0);
int letter1 = (int) c1;
int letter2 = (int) c2;
String selector = Character.toString(c3);
char[] arr;
int index = 0;
arr = new char[c2 - c1 + 1];
//Yif the selector is the letter h the program will print horizontaly
//Xif the selector is the letter v the program will print vertically
//Yprogram prints horizontaly
//Xprogram prints vertically
//Yselector works
//Xclean up everything
if (letter2 >= letter1){
if (selector.equals("h")) {
//(char)x is the letter
//x is the number
int counter = 0;
for (int x = (int) c1; x <= c2 && counter < 4; ++x) {
System.out.print(" " + "DEC " + "Oct " + "Hex");
++counter;
}
System.out.println("\n");
for (int x = (int) c1; x <= c2; ++x) {
if (counter % 4 == 0) {
System.out.println("");
}
String hex = Integer.toHexString(x);
String oct = Integer.toOctalString(x);
arr[index++] = (char) x;
System.out.print(" " + (char) x + " "
+ x + " " + oct + " " + hex);
++counter;
}
pause();
} else if (selector.equals("v")) {
int counter = 0;
for (int x = (int) c1; x <= c2 && counter < 4; ++x) {
System.out.print(" " + "DEC " + "Oct " + "Hex");
++counter;
}
System.out.println("\n");
for (int x = (int) c1; x <= c2; ++x) {
if (counter % 4 == 0) {
System.out.println("");
}
String hex = Integer.toHexString(x);
String oct = Integer.toOctalString(x);
arr[index++] = (char) x;
System.out.print(" " + (char) x + " "
+ x + " " + oct + " " + hex + "\n");
++counter;
}
pause();
} else {
System.out.println("Error: Third input parameter must be h or v.");
System.out.println("Program now terminating.");
//?insert pause and end the program here
}
}else{
System.out.println("Error: First input parameter must precede "
+ "the second in the ASCII sequence.");
}
}
i know my code is a bit of a mess and you can put stuff in methods but right now im just focusing on getting the table to print properly.
also i apologize for the bad name for my question.
Upvotes: 0
Views: 886
Reputation: 313
Use System.out.printf(String, ....)
and it will get much easier - you can specify item as follows:
System.out.printf("%c %3d %03o %3x %c %3d %03o %x %c %3d %03o %3x\n",
'a', (int)'a', (int)'a', (int)'a',
'b', (int)'b', (int)'b', (int)'b',
'c', (int)'c', (int)'c', (int)'c');
Which gets you
a 97 141 61 b 98 142 62 c 99 143 63
Upvotes: 0
Reputation: 159086
I'm not going to show how to modify your code to print sequence of values in horizontal and vertical order, but instead show you a more generic implementation for simply printing numbers like that, so the answer to more generally useful to other people too.
I'll leave it up to you to apply the algorithm to your code.
public static void printHorizontal(int min, int max, int cols) {
for (int i = min; i <= max; i++) {
boolean last = (i == max || (i - min + 1) % cols == 0);
System.out.printf("%3d", i);
if (last)
System.out.println();
else
System.out.print(" ");
}
}
public static void printVertical(int min, int max, int cols) {
int rows = (max - min) / cols + 1;
for (int row = 0; row < rows; row++) {
for (int col = 0, i = min + row; col < cols && i <= max; col++, i += rows) {
boolean last = (col == cols - 1 || i > max - rows);
System.out.printf("%3d", i);
if (last)
System.out.println();
else
System.out.print(" ");
}
}
}
Tests
printHorizontal(5, 17, 5);
printVertical(5, 17, 5);
Outputs
5 6 7 8 9
10 11 12 13 14
15 16 17
5 8 11 14 17
6 9 12 15
7 10 13 16
Upvotes: 0
Reputation: 50
Print the values separated by tabs. Taken from your code snippet
HEADER:
System.out.print(" " + "\t\tDEC " + "\t\tOct " + "\t\tHex");
++counter;
}
VALUES:
System.out.print(" " + (char) x + "\t\t"
+ x + "\t\t" + oct + "\t\t" + hex);
++counter;
}
Upvotes: 1