Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Placing user input to a array and to then display it as a string

  1. #1
    Junior Member LaliB's Avatar
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Placing user input to a array and to then display it as a string

    I've been asked to create a program where I make up 3 passwords for users. That's not the problem and I'm able to do that. My actual problem is the part where we must take these 3 designed passwords and place them inside a String array and then get it to loop through this array and display out the 3 different passwords. I really don't know how to go about from here and have been stuck on this. I've tried so many resources on this but still can't find anyone that can tell me what exactly to do in a case where the array values aren't pre-given.

    import java.util.Scanner;  
    import static java.lang.System.out;  
    import java.io.*;  
     
    public class BirdTag {  
     
        public static void main (String args [])  
        {  
            Scanner myScanner= new Scanner (System.in);  
            String ProvinceName,SpotterName,Surname;  
            int Year=12  
     
            for (int i=0; i<3; i++) //I PLACE EVERYTHING IN HERE IN ORDER TO GET THE PROGRAM TO RUN 3 TIMES . I MIGHT BE WRONG HERE TOO.  
            {  
            out.println ("What is your province name, please enter E for Eastern Cape, N for Northern Cape and W for Western Cape");  
            ProvinceName = myScanner.nextLine ();          
            out.println ("Province Name is " + ProvinceName);  
            out.println ("Please enter your name");  
            SpotterName= myScanner.nextLine ();  
           String Extract1= SpotterName.substring(0,2);  
           out.println ("Name is " + Extract1);  
           out.println ("enter your surname please ");  
           Surname= myScanner.nextLine ();  
           String Extract2=Surname.substring(0,2);  
           out.println ("Surname is " + Extract2);  
                   String FinalPassword= ProvinceName+ Year+ Extract1+Extract2;  
           out.print ("Your password is ");   
           String Password = myScanner.nextLine(); //THIS IS THE PART WHERE EVERYTHING GOES WRONG AND I DON'T THINK I'M DOING THE RIGHT THING  
            out.print (FinalPassword);  
            out.println () ;  
           String arrTags[] = new String[3];// THIS IS WHERE I TRY TO MAKE MY ARRAY BUT THEN FROM HERE ONWARDS I'M STUCK  
            }     
        }  
    }

    THANK YOU SO MUCH IN ADVANCE !


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Placing user input to a array and to then display it as a string

    There are lots of examples on this forum that use loops and get values from arrays.
    Also see:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Last edited by Norm; January 11th, 2012 at 11:55 AM. Reason: remove Search arg

  3. #3
    Junior Member LaliB's Avatar
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Placing user input to a array and to then display it as a string

    I've looked at that link you provided me with before and did so again but I can't see how that is helping me at all. I'm finding it difficult to get my program to use the values entered and then placing them into a array. I know how to make a array when values are given but in this case I don't have the luxury of knowing beforehand what my values will be.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Placing user input to a array and to then display it as a string

    If you must use an array, and do not know how many values you will put into it, do you know a maximum number that could possibly be used? Would 2000 be more than the user would input? Or 200 or 75?
    If you can estimate the max number, make an array of size bigger than the number the user will enter.
    Put the user's values into the array and use the index for putting the values into the array as a count of the number of elements in the array. Start the index at 0 and increment by one as each value is put into the array.

  5. #5
    Junior Member LaliB's Avatar
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Placing user input to a array and to then display it as a string

    See this is the problem here. My values won't be numbers. It will be things like "E12SMJO", "N12HOME" ect. because in the beginning of my coding I make up a unique password of the person's province, name and surname. And there's a 12 that has to be hardcoded. I did all this. So I cannot make any underestimate anything here because I'm not working with integers, I'm working with strings. That's what's making this so tricky And I don't know how to do the "Put the user's values into a array part".

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Placing user input to a array and to then display it as a string

    You assign values to slots in an array the same way if the values are int or objects:
    theArray[theIndex] = theValue; // put theValue into theArray at index: theIndex

    theArray and theValue must be the same data type.

Similar Threads

  1. Append data from user input into array
    By brncao in forum Collections and Generics
    Replies: 1
    Last Post: October 11th, 2011, 04:37 AM
  2. Parsing a string to an int while receiving user input?
    By happyxcrab in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2011, 05:38 PM
  3. Parsing many Ints from a user input String.
    By helpmeplease111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2011, 08:41 PM
  4. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  5. [SOLVED] placing packages of the same directory into an array
    By javanub:( in forum Java Theory & Questions
    Replies: 16
    Last Post: May 18th, 2010, 07:49 AM