Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default Making arrays by reading user input

    Arrays are definitely one of my weak points, and I'm confused as crap as to how to make an array of a string input.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    What do you mean? every input shall be stored in and array?

    Correct me if im too far from your point
    freedom exists in the world of ideas

  3. #3
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    From what I get from your question is yes. Maybe this will help.

    Java Code:
    //call for input
    Scanner input = new Scanner(System.in);
    System.out.println("Enter sentence: ");
    String sent = input.nextLine;
    
    // How do I get sent into an array?

  4. #4
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I would use a vector here but you should just do my
    String[] myString = new String[What Ever Size];
    myString[i] = input.getNextLine();

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    String array[] = {"Me","You","Them","Us","Other","They"};

    Can you access every element of an array?

    If not, try to have an experiment....

    String s = array[3];
    int len = array.length;
    String s1 = array[7];
    String s2 = array[-1];
    Try to observe the output....

    By storing an input to an array,
    array[0] = "Chupacabras";
    array[1] = "Rasta";
    array[2] = true;
    array[3] = false;
    Try to observe what happens now when you seek again the value of every element of the array.... ( showing it again ).....

    But it is important to read a tutorials about arrays....
    There are more and understandable explanations there....
    freedom exists in the world of ideas

  7. #7
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    I'm pretty familiar with making an array with hardcoded information. But what's confusing me is the array with user input.

    Java Code:
    Scanner input = new Scanner(System.in);
        		System.out.println("Enter String to use: ");
        		String list = input.nextLine();
        		String trimmedword = list.trim();
        		System.out.println("Enter Index: ");
        		int index = input.nextInt();
    Is what I have to start with.

    I'm using the trim because I need to able to manipulate this array with the index choice. So I need to turn that trimmed string into an array.
    Last edited by apfroggy0408; 04-25-2008 at 05:32 AM.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    Would that work using my trim variable? I'm kind of confused by it.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  11. #11
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    How would I go about using ArrayList?

    would it be like
    Java Code:
    ArrayList[] myString = new ArrayList[];
    System.out.println("Enter sentence: ");
    myString[] = input.getNextLine;

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Defining array list is wrong there.

    Java Code:
            ArrayList al = new ArrayList();
            Scanner input = new Scanner(System.in);
            
            System.out.println("Enter the string: ");
            String str = input.nextLine().trim();
            
            System.out.println("Enter an index: ");
            int index = input.nextInt();
            
            al.add(index, str);
    Loop this to handle for any number of inputs.

  13. #13
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    I'm guessing add adds the string and index to the array correct?

  14. #14
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    --------------------Configuration: <Default>--------------------
    Note: G:\AP Comp. Sci\Labs\p499proj12l2.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.


    eh...?

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you want to do it with an array, do it as follows.

    Java Code:
            String[] mm = new String[10];
            Scanner input = new Scanner(System.in);
            
            System.out.println("Enter the string: ");
            String str = input.nextLine().trim();
            
            System.out.println("Enter an index: ");
            int index = input.nextInt();
            
            mm[index] = str;
    Issue is there, defining the size of the array. According to my code, you can't exceed 10 for index.

    ArrayList is something different. Size is grow depending on your insertion into the list.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by apfroggy0408 View Post
    --------------------Configuration: <Default>--------------------
    Note: G:\AP Comp. Sci\Labs\p499proj12l2.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.


    eh...?
    Ah, it's the warning that ArrayList is not defined as an expandable array. It's not a big case for the simple processing, like we do here.

    If you want to avoid it, change the ArrayList definition as follows.

    Java Code:
    ArrayList<String> al = new ArrayList<String>();

  17. #17
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    Oh, alright. Now the question I have now is why do you have index placed in []? I'm going to be using the inputted index to find a certain spot in the array. As in index 2 in shoe would be h.

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by apfroggy0408 View Post
    Oh, alright. Now the question I have now is why do you have index placed in []?
    That's the inbuilt pattern pal.

  19. #19
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    So, haven't been able to get back to this program 'til now, and I'm still lost.

    I have the code

    Java Code:
    ArrayList<String> mm = new ArrayList<String>();
            	Scanner input = new Scanner(System.in);
            
           	 	System.out.println("Enter the string: ");
            	String str = input.nextLine().trim();
            
            	System.out.println("Enter an index: ");
            	int index = input.nextInt();
            
            	mm[index] = str;
            	System.out.println(mm);
            	System.out.println(index);
    But I can't get the array to show.

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Page 1 of 2 12 LastLast

Similar Threads

  1. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 06:23 PM
  2. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 07:03 AM
  3. Help with making this algorithm better
    By RLRExtra in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 04:11 PM
  4. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

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