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 16 of 16

Thread: Getting user input and storing it into an array

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Getting user input and storing it into an array

    What I am trying to do is have the user get a prompt "Enter in a name: " when they enter in the name it is then added to an array list. After the name is added in the array list the user is then prompted again to enter in information this time the house number, then the street name, zip, country, state, balance.
    After that is all done an auto created account number will be assigned to that information.

    public void addAccount(int accountNumber,String name,String houseNum,String streetName,String city,String state,String zip,String country) {
    		Scanner reader = new Scanner(System.in);
    		System.out.println("Enter clients name: ");
    		clients[1].add(reader.nextLine());
    		System.out.println("Enter clients house number: ");
    		clients[2].add(reader.nextLine());
    		System.out.println("Enter clients street name: ");
    		clients[3].add(reader.nextLine());
    		System.out.println("Enter clients city: ");
    		clients[4].add(reader.nextLine());
    		System.out.println("Enter clients state: ");
    		clients[5].add(reader.nextLine());
    		System.out.println("Enter clients zip: ");
    		clients[6].add(reader.nextLine());
    		System.out.println("Enter clients country: ");
    		clients[7].add(reader.nextLine());
    		System.out.println("Enter clients initial account balance: ");
    		clients[8].add(reader.nextLine());
    		reader.close();
     
    	}

  2. The Following User Says Thank You to J0k3r For This Useful Post:


  3. #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: Getting user input and storing it into an array

    Do you have any specific java programming questions about your program?
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following 2 Users Say Thank You to Norm For This Useful Post:

    J0k3r (March 14th, 2019)

  5. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Getting user input and storing it into an array

    Quote Originally Posted by Norm View Post
    Do you have any specific java programming questions about your program?
    How to go about getting the user input in that order into an array? I thought the way I was going about it was going to work however it didn't. I declared an array list at the top of my program so I can use it anywhere instead of just declaring it inside the method.
    Thank you for responding.

  6. The Following User Says Thank You to J0k3r For This Useful Post:


  7. #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: Getting user input and storing it into an array

    How to go about getting the user input in that order into an array?
    Use an asssignment statement to put data into an array:
      theArray[theIndex] = theData;   // put data into the array
    Increment the index to move to the next slot in the array:
      theIndex++;  // move to next slot in the array

    it didn't.
    Please explain what happened and what you want to happen.

    I declared an array list
    An ArrayList is not the same as an array.
    The ArrayList uses methods to access its contents:
       theArrayList.add(theValue);  // add a value to the list
    An array uses array notation: [] with an index to access its contents
       theArray[theIndex] =  theValue;  // change value of array at theIndex
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:


  9. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Getting user input and storing it into an array

    Regardless of the warnings you might get, don't do this.

    reader.close();

    This is because you will closing System.in and would never be able to take any input from the console again.

    Regards,
    Jim

  10. The Following 2 Users Say Thank You to jim829 For This Useful Post:

    J0k3r (March 14th, 2019)

  11. #6
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Getting user input and storing it into an array

    Quote Originally Posted by Norm View Post
    Use an asssignment statement to put data into an array:
      theArray[theIndex] = theData;   // put data into the array
    Increment the index to move to the next slot in the array:
      theIndex++;  // move to next slot in the array


    Please explain what happened and what you want to happen.


    An ArrayList is not the same as an array.
    The ArrayList uses methods to access its contents:
       theArrayList.add(theValue);  // add a value to the list
    An array uses array notation: [] with an index to access its contents
       theArray[theIndex] =  theValue;  // change value of array at theIndex
    I wanted question to pop up using a simple print statment. Receiveing what the user wrote and storing it into clients.
    for example:
    Enter the clients name:
    user inputs: John
    clients index spot [1] now has john
    enter clients house number:
    user inputs: 455
    clients index spot[2] now has 455
    This would take in all the information needed to create a new bank account for a client and could be done an infinite amount of times adding new clients.

    The way I had it originally would give me an error based on .add(reader.nextLine()); the error was the method add(string was undefined for type string.

  12. The Following User Says Thank You to J0k3r For This Useful Post:


  13. #7
    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: Getting user input and storing it into an array

    an error based on .add(reader.nextLine()); the error was the method add(string was undefined for type string.
    You left off the variable that would have been before the .add
    For example: someObject.add(...
    What class object was the code calling the add method?

    Please copy the exact text of the error message as it was written by the compiler. Don't try to post a message from memory.
    What you posted is confusing.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:


  15. #8
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Getting user input and storing it into an array

    Quote Originally Posted by Norm View Post
    You left off the variable that would have been before the .add
    For example: someObject.add(...
    What class object was the code calling the add method?

    Please copy the exact text of the error message as it was written by the compiler. Don't try to post a message from memory.
    What you posted is confusing.
    The way I had it originally would give me an error based on clients[1].add(reader.nextLine()); the error was "The method add(String) was undefined for type String". the .add portion is what causes the error.

  16. The Following User Says Thank You to J0k3r For This Useful Post:


  17. #9
    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: Getting user input and storing it into an array

    "The method add(String) was undefined for type String".
    The compiler can not find an add method in the client object class that takes a String as an argument.
    What class is the client array?

    When copying and pasting error messages, please do not mix the message's text with your comments. It makes it hard to see and understand the error message if there is other text mixed in with it.

    Please copy full text of the compiler's error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:


  19. #10
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Getting user input and storing it into an array

    This is all of my code as it is right now.
    Account Class
    public class Account implements IAccount{
    	private int accountNumber;
    	private double balance;
    	private static int numberOfAccounts = 0;
    	private String accountType;
    	private static int startingAccountNumbers = 1000000;
     
    	private ArrayList<Client>clientList;
     
    	public Account() {
    		clientList = new ArrayList<Client>();
    	}
    	public Account(int accountNumber,double balance){
    		this.setAccountNumber(accountNumber);
    		this.balance = balance;
    		numberOfAccounts++;
    	}
    	public Account(double balance) {
    		accountNumber = startingAccountNumbers++;
    		this.balance = balance;
    		numberOfAccounts++;
    	}
    	public void deposit(double howMuch) {
    		balance += howMuch;
    	}
    	public void withdraw(double amount) {
    		balance -= amount;
    	}
    	public void transfer(Account to, double amount) {
    			this.withdraw(amount);
    			to.deposit(amount);
    	}
    	public int getNumberOfAccounts() {
    		return numberOfAccounts;
    	}
    	public double getBalance() {
    		return this.balance;
    	}
    	public void setBalance(double balance) {
    		this.balance = balance;
    	}
    	public int getAccountNumber() {
    		return accountNumber;
    	}
    	public void setAccountNumber(int accountNumber) {
    		this.accountNumber = accountNumber;
    	}
     
    Client Class
     
    public class Client {
    	private String name;
    	private int clientID = 1000000;
    	private String houseNum;
     
    	private String streetName;
    	private String city;
    	private String state;
    	private String zip;
    	private String country;
    	private double accountBalance;
    	private String [] clients;
     
    	//ArrayList<String>accountList = new ArrayList<String>();
    	private ArrayList<Account>accountList;
     
    	public Client() {
    		accountList = new ArrayList<Account>();
    	}
    	public Client(String name,String houseNum,String streetName,String city,String state,String zip,String country, double accountBalance) {
    		this.name = name;
    		this.houseNum = houseNum;
    		this.streetName = streetName;
    		this.city = city;
    		this.state = state;
    		this.zip = zip;
    		this.country = country;
    		this.accountBalance = accountBalance;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getClientID() {
    		return clientID;
    	}
    	public void setClientID(int clientID) {
    		this.clientID = clientID;
    	}
    	public void deposit(double howMuch) {
    		setBalance(getBalance() + howMuch);
    	}
    	public void withdraw(double amount) {
    		setBalance(getBalance() - amount);
    	}
    	public double getBalance() {
    		return accountBalance;
    	}
    	public void setBalance(double balance) {
    		this.accountBalance = balance;
    	}
    	public void printInformation() {
    		System.out.println(accountList);
    	}
    	public void addAccount(int accountNumber,String name,String houseNum,String streetName,String city,String state,String zip,String country) {
    		Scanner reader = new Scanner(System.in);
    		System.out.println("Enter clients name: ");
    		clients[1] = name;
    		System.out.println("Enter clients house number: ");
    		clients[2] = houseNum;
    		System.out.println("Enter clients street name: ");
    		clients[3] = streetName;
    		System.out.println("Enter clients city: ");
    		clients[4] = city;
    		System.out.println("Enter clients state: ");
    		clients[5] = state;;
    		System.out.println("Enter clients zip: ");
    		clients[6] = zip;
    		System.out.println("Enter clients country: ");
    		clients[7] = country;
    		System.out.println("Enter clients initial account balance: ");
    		clients[8] = accountBalance;
    	}
     
    	public void addAccount(double balance) {
    		Account a = new Account(balance);
    	}
    	public String getHouseNum() {
    		return houseNum;
    	}
    	public void setHouseNum(String houseNum) {
    		this.houseNum = houseNum;
    	}
    	public String getStreetName() {
    		return streetName;
    	}
    	public void setStreetName(String streetName) {
    		this.streetName = streetName;
    	}
    	public String getCity() {
    		return city;
    	}
    	public void setCity(String city) {
    		this.city = city;
    	}
    	public String getState() {
    		return state;
    	}
    	public void setState(String state) {
    		this.state = state;
    	}
    	public String getZip() {
    		return zip;
    	}
    	public void setZip(String zip) {
    		this.zip = zip;
    	}
    	public String getCountry() {
    		return country;
    	}
    	public void setCountry(String country) {
    		this.country = country;
    	}
    	}

  20. The Following User Says Thank You to J0k3r For This Useful Post:


  21. #11
    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: Getting user input and storing it into an array

    Can you answer my question: what class is a clients object? What methods does that class have?
    The compiler does not see an add method for that class.

    I can see the code but you need to be able to read the code and find the answer to my question.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:


  23. #12
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Getting user input and storing it into an array

    Quote Originally Posted by Norm View Post
    Can you answer my question: what class is a clients object? What methods does that class have?
    The compiler does not see an add method for that class.

    I can see the code but you need to be able to read the code and find the answer to my question.
    Neither of my classes have an add method in them. I apologize but I dont understand your question of which class is my clients object. Are you asking me what class is my clientsList under? If so that would be my account class. My accountList is in my client class.

  24. The Following User Says Thank You to J0k3r For This Useful Post:


  25. #13
    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: Getting user input and storing it into an array

    Neither of my classes have an add method in them.
    Yes, I am not asking about your classes.
    I am asking what class the clients object is. For example from your code:
    	private String streetName;
    The above statement defines streetName as a String class object.

    And
      private ArrayList<Client>clientList;
    defines clientList as an ArrayList object. Its data type is ArrayList.

    What is clients?

    I see you have fixed the code.
    Does the program work now?
    If you don't understand my answer, don't ignore it, ask a question.

  26. The Following User Says Thank You to Norm For This Useful Post:


  27. #14
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Getting user input and storing it into an array

    clients is a String array.
    Once I do an int parse for account balance it should work
    Last edited by J0k3r; March 14th, 2019 at 08:01 PM.

  28. The Following User Says Thank You to J0k3r For This Useful Post:


  29. #15
    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: Getting user input and storing it into an array

    I guess you are making progress then.
    If you don't understand my answer, don't ignore it, ask a question.

  30. #16
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Getting user input and storing it into an array

    Slowly but surely. Thank you for your patience.

Similar Threads

  1. Filling an array with user input and a method
    By FlyingGvirus in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 4th, 2014, 03:26 PM
  2. Trying to validate a character user input in an array ?
    By lizzy2 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2013, 03:04 PM
  3. How to add to an array with user input
    By miller4103 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2013, 10:27 PM
  4. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 11:41 AM
  5. Append data from user input into array
    By brncao in forum Collections and Generics
    Replies: 1
    Last Post: October 11th, 2011, 04:37 AM