Pergunta

I am having problems implementing the printf method into my code. I started my first Java course this week and am trying to get ahead of the class. Essentially, I am to create an output of ASCII characters, hexadecimal numbers, and their decimal equivalents, up to 127 entries. The number of rows created in the output is chosen by user input. This is the code I have so far:

package lab1;
import java.util.Scanner;

public class Lab1 {
    public static void main(String[] args) {

        //declare 
        Scanner scan = new Scanner(System.in);

        //prompt the user to enter an integer, num defines the # of rows displayed in output
        System.out.print("How many groups? ");
        int num = scan.nextInt();

        //print ascii, then hex value, then dec value
        for (int c = 0; c < 128; c++) { 
            String hex = Integer.toString(c , 16);
            String output = (char)c + " " + hex + " " + c;
            System.out.println(output);
        /*
            //print the output with printf to create the columns
            //character (c), string(s), decimal integer(d)
            System.out.printf("%-2c %-2s %-2d", output);
        */
        }
    }
}

I would have posted an image here showing what the final result should look like but I need 10 reputation. I am able to email a copy to you privately though.

I hope you can help me understand how to accomplish this, or direct me to a resource where I am able to learn myself.

Thanks!

Foi útil?

Solução

You need to pass the same number of arguments as you have flags in your printf.

for (int c = 0; c < 128; c++) { 
    // String hex = Integer.toString(c , 16); - No need for this anymore.

    // Print the output with printf to create the columns
    // character (c), string(s), decimal integer(d)
    System.out.printf("%-2c 0x%-2X %-2d%n", (char)c, c, c);
}

With 0x%-2X, you can print out uppercase Hex values. I added 0x as a prefix to specify the base.

Example output:

...
A  0x41 65
B  0x42 66
C  0x43 67
D  0x44 68
E  0x45 69
F  0x46 70
G  0x47 71
H  0x48 72
I  0x49 73
J  0x4A 74
K  0x4B 75
L  0x4C 76
M  0x4D 77
N  0x4E 78
O  0x4F 79
P  0x50 80
Q  0x51 81
R  0x52 82
S  0x53 83
T  0x54 84
U  0x55 85
V  0x56 86
W  0x57 87
X  0x58 88
Y  0x59 89
Z  0x5A 90
...

Outras dicas

You don't actually need to pass multiple arguments.

for (int c = 0; c < 128; c++) {
     // Print ASCII, then hex, then dec
     System.out.printf("%1$-2c %1$-2x %1$-2d%n", c);
}

See the docs here: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

The problem is you need three arguments for you printf, one each for char, String and decimal. Since you are passing only one variable (which is of type String) it gets confused. Remember, concatenating everything in one String(output) makes all a type String.

You need each tag to correspond with a variable, and your tags need to be formatted correctly.

To print an ascii, hex, and decimal value, your printf statement should look something like this:

System.out.printf("%-2c -  %-2x -  %-2f", someChar, someHex, someFloat);

where someChar is a char, someHex is an int whose hex value you wish to display, and someFloat is the float/double value you wish to display.

For more information on format strings in Java see: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top