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.
Code :
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 !
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)
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.
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.
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".
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.