Results 1 to 6 of 6
Thread: User input to Array Question
- 08-14-2012, 06:14 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 6
- Rep Power
- 0
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]);
}
}
}
- 08-14-2012, 06:53 AM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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.
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-14-2012, 09:50 AM #3
Re: User input to Array Question
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-14-2012, 12:10 PM #4
Member
- Join Date
- Aug 2012
- Posts
- 6
- Rep Power
- 0
Re: User input to Array Question
I Think that helped but I am Still getting a format error... Any ideas? Thanks.
- 08-14-2012, 12:27 PM #5
Re: User input to Array Question
- 08-14-2012, 01:12 PM #6
Member
- Join Date
- Aug 2012
- Posts
- 6
- Rep Power
- 0
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??
Similar Threads
-
Pass User Input into an Array
By Rahim2312 in forum New To JavaReplies: 2Last Post: 05-14-2012, 05:45 PM -
Creating 2D array from all user input
By peek_a_boo in forum New To JavaReplies: 1Last Post: 12-08-2011, 08:16 PM -
Very basic question regarding user input
By fugazi in forum New To JavaReplies: 6Last Post: 01-06-2011, 07:40 PM -
user input array
By localhost in forum New To JavaReplies: 5Last Post: 12-30-2010, 04:00 AM -
Read user input into integer array
By varunb in forum New To JavaReplies: 12Last Post: 07-09-2010, 12:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks