User input to Array Question
Hello I am new to programming and i am trying to make this simple array that takes user input of type string and then compiles it. Does anyone know what is wrong with it? Thank you.
package arraypractices;
import java.util.Scanner;
public class ArrayPractices {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String X;
String B;
String C;
System.out.print("Please enter First name\n");
X = in.next();
System.out.print("Please enter Middle name\n");
B = in.next();
System.out.print("Please enter Last name\n");
C = in.next();
String[] array = {"X", "B", "C"};
System.out.printf("%s%10s%10s\n", "First", "Middle", "Last");
for (int counter = 0; counter < array.length; counter++) {
System.out.printf("%s%10s%10s\n", counter, array[ counter]);
}
}
}
Re: User input to Array Question
The contents of your array are 3 literal Strings: "X", "B", and "C". You want the variables named X, B, and C.
Re: User input to Array Question
Re: User input to Array Question
I Think that helped but I am Still getting a format error... Any ideas? Thanks.
Re: User input to Array Question
Quote:
Originally Posted by
LukasHopkins
System.out.printf("%s%10s%10s\n", counter, array[ counter]);
Inside the parameters for printf you specifiy 3 String parameters but there are only two arguments, one for counter and the other for array[counter]. What is prompted is X, B and C and I can't get the logic!
Re: User input to Array Question
I changed the Printf to this:
System.out.printf("%s%10s%10s", (Object[]) array);
and it seems to work now (minor spacing issues which I can play with or will vary with the length of the persons name).
I was trying to use the example in my book and elaborate it further myself and I got confused. Since I wasnt using any integers in my program, there was no reason to have a for statement in it with a counter. Especially since I was only using a two column array.
Your Thoughts??