Results 1 to 6 of 6
  1. #1
    LukasHopkins is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    Default 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]);
    }

    }
    }

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default 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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: User input to Array Question

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    LukasHopkins is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    Default Re: User input to Array Question

    I Think that helped but I am Still getting a format error... Any ideas? Thanks.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: User input to Array Question

    Quote Originally Posted by LukasHopkins View Post
    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!

  6. #6
    LukasHopkins is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    Default 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

  1. Pass User Input into an Array
    By Rahim2312 in forum New To Java
    Replies: 2
    Last Post: 05-14-2012, 05:45 PM
  2. Creating 2D array from all user input
    By peek_a_boo in forum New To Java
    Replies: 1
    Last Post: 12-08-2011, 08:16 PM
  3. Very basic question regarding user input
    By fugazi in forum New To Java
    Replies: 6
    Last Post: 01-06-2011, 07:40 PM
  4. user input array
    By localhost in forum New To Java
    Replies: 5
    Last Post: 12-30-2010, 04:00 AM
  5. Read user input into integer array
    By varunb in forum New To Java
    Replies: 12
    Last Post: 07-09-2010, 12:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •