Hi,
I have done this code to learn ArrayList. I keep getting errors. Basically I want to accumulate names in list and when user types 'n' at prompt, it prints out names. What is wrong?

package mtangoo;

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

public class MainClass {
    public static void main(String args[]){
        Listed jobject = new Listed();

    }
    Listed lst = new Listed();
}

class Listed{
    Scanner getValues = new Scanner(System.in);
    ArrayList<String> peopleList = new ArrayList<String>();
    //looping
    while (true){
        System.out.println("Please Enter the Name:");
        String name = getValues.nextLine();
        peopleList.add(name);
        System.out.println("Continue? y/n");
        if(getValues.nextLine()=="n"){
            break;
        }//endif
        else{
            continue;
        }
    }

        //loop finished
        for(int i = 0; i<peopleList.size();i++){
            System.out.println(peopleList.get(i));
        }

}

Recommended Answers

All 20 Replies

It might help to know what these nebulous "errors" are. complete error message with stack traces (where applicable), please.

I see one thing that could be a problem, getValues.nextLine()=="n" will (should) always return false, because it compares the memory addresses, so you should replace it with getValues.nextLine().equals("n")

I changed the code to match as per sciwizeh but still persists. Here is the error (I use easy eclipse by the way)
Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at mtangoo.MainClass.main(MainClass.java:7)

There is more than that. That occurs when you try to execute a project within Eclipse that hasn't been successfuly compiled yet. It would definately help to have the compilation messages.

So what should I do? I use easy eclipse

Compile the project?

Read the manual to find out how, if you must.

But If I write hello world program and run it, all goes well. Also eclipse complains there is ''Syntax error on token ";" , { expected after this token"

So what is that? As far as I can see, all is fine.

Then that is the compilation error. On which line does that occur.

But I can tell you, that most of the class defination of "Listed" needs to be contained within a Method and not directly under the class.

is there anything wrong with my ArrayList?

Your main problem lies in understanding the operation of subclasses in OOP. Subclasses are templates for creating objects which do not exist in memory until a new instance is created and given a variable name. Then the code will exist but still needs to be called for it to do anything!

Your create an instance of the class in your main class using;

Listed lst = new Listed();

This creates the class and its routines but does not instruct it to do anything! You need to call a method on lst to invoke the code but before you can do this you will have to create one.

If the code where contained in a method (perhaps loadData()) you could then use;

lst.loadData();

where you have

class Listed{
public void loadData(){
      your lines 15 to 34
};

Thanks alot Sir! It helped me fix alot.
The program now runs but it keep asking names and even when I press 'n' it continues. How to break out of the loop when user presses n?

Thanks buddies

Did you change the =="n" to equals("n") as Sciwizeh mentioned?

Yes I have done that but problem is still there

Well, since it's difficult to see over your shoulder, perhaps you should post the current version of the code.

here it is

package mtangoo;

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

public class MainClass {
    public static void main(String args[]){
    Listed lst = new Listed();
    lst.testArrayList();
}
}//end class

class Listed{
    public void testArrayList(){
        Scanner getValues = new Scanner(System.in);
        ArrayList<String> peopleList = new ArrayList<String>();
        //looping
        while (true){
            System.out.println("Please Enter the Name:");
            String name = getValues.nextLine();
            peopleList.add(name);
            System.out.println("Continue? n to end ");
            if(getValues.equals("n")){
                break;
            }//endif
            else{
                continue;
            }
        }

        //loop finished
        for(int i = 0; i<peopleList.size(); i++){
            System.out.println(peopleList.get(i));
        }

}
}//end class

You are comparing a Scanner object with a String object.

You are comparing a Scanner object with a String object.

I thought scanner object returns a string (assumption from printing hello world) :shock:

So what can I do to compare what user enters with string?

Use getValues().nextLine().equals("n") instead. The Scanner name is confusing BTW, just name it scanner instead of getValues which is more appropriate for a method name.

That completed the solving of this headache
Thanks all of you guys!

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.