Hi there, I'm back again and this time I have another question:

So I figured out that I have to use Array Lists for my last question, but now, I have another problem: How do I get all the user inputs separated into different strings so that the user may be able to input data about those separate strings?
The code below is what I have so far, I'm sorry if my stupidity is over 9000 lol:

import java.util.Scanner;
import java.util.ArrayList;

public class SheetCounter 
{
    static Scanner userin = new Scanner (System.in);
    static ArrayList <String> bleh = new ArrayList <String>();
    static ArrayList <Integer> numberofsheets = new ArrayList <Integer>();

    public static void main (String[] args)
    {
        System.out.println("Welcome to the (Creative Name Here)");

        String test = "";
        while (test != "n")
        {
            System.out.println("Please input the name of a user you wish to count:");
            bleh.add(userin.next());
            test = userin.nextLine();
            System.out.println("Do you wish to add any other names?");
            System.out.println("Type 'y' if you do, type 'n' if you do not");
            test = userin.nextLine();
            if (test.equals("n"))
            {
                break;
            }
        }

        System.out.println("These are all the users you have added: " + bleh);
    }
}

Recommended Answers

All 8 Replies

You need a loop which keeps going round until the input is a certain thing but you need to append the answers to an array if you want to keep them. Here is the general syntax for this, I do apologise I havn't done java in a while but I hope you will get the gist.

while (true) {
    // take user input - http://www.java-made-easy.com/java-scanner.html

    if (userInput.equals("stop")) {
        break
    } else {
        // append users answer - http://stackoverflow.com/questions/14098032/add-string-to-string-array
    }
}

Hope this helps - Mat

Thanks, that really helped, but now I have another question as well; I updated the post so that the new question is up.

So, if you had your data in an array there first input would be array[0], second array[1] etc. Therefore i'm pretty sure you can put parts of the array into variables. Here is an example code of this:-

String[] array = {"a","b","c"};
String firstInput = array[0];   // This variable = "a"
System.out.print(firstInput);

So now you could edit this, for example:-

firstInput += " - this is the first value";

Hope this is what you're looking for.

Awesome thanks so much, one last question before I let you go if you would:

How would I be able to store integers under those specific strings?
For example, say I have user A and want to be able to add some information (such as his age) about him, would I have to create another arraylist and this time make it for integers and do the same thing?

Here is an idea:

String[] users = {};
int[] age = {};

while (true) {
    // take user input for user
    // take user input for age
    if (userInput.equals("stop")) {
        break
    } else {
        // append users answer in users array
        // append age answer in age array
    }
}

user1 = users[0] + age[0];
user2 = users[1] + age[1];

You will also want to use some verification method such as:

if(userInput == null) {
    System.out.print("Please enter an answer");
} else {
    // Do the rest of the stuff in the loop
}

You can put this in to a loop to make sure your program has minimal errors.

Sorry if any of my syntax is wrong (which I think it might be). I havn't used Java in a while :)

:) Thanks so much man. You helped a lot :D

No problem:)

say I have user A and want to be able to add some information (such as his age) about him, would I have to create another arraylist

You can do that, but you will find it hard to keep the two arraylists synchronised (eg, suppose you want to sort the data). And the more info you add the worse it will become.
The best way to do it is to define a Person class, with name and age (etc) as variables, and have a single ArrayList of Persons

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.