0

So essentially this is what I want to do,

do{
    Object name = new Object();*Create new object*
    Object.method*run created object through a method*

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Do you wish to continue entering data");
    String answer = keyboard.nextLine();   
 } while (answer.equalsIgnoreCase("Yes"))

Do I need to create arrays to make this work? If so how would that work?

1
  • 2
    That depends; do you want to keep every object created, or just the last such object? Commented Dec 5, 2011 at 22:50

5 Answers 5

2

Depends. If your object has state (represents a row in a database for example), you would have to create it for each iteration. But if all you need to do is call a method in the object (i.e. if it is stateless, (has no non-static and non-final class variables or is immutable)), you should just create one instance, and call the method in each iteration. An example of that is your Scanner object. All you are doing is call a method in it, so instead of creating a new object each time, you would either create it before calling the method, or as an instance (class level, ideally private) field so it can be re-used in all methods. Here is how I would re-write your code:

public class MyClass {

private final Scanner scanner = new Scanner(System.in);

public void doSomething() {
    Object object = new Object();
    do {
        // call your method here
        // object.yourMethod();

        System.out.println("Do you wish to continue entering data?");
    } while (scanner.nextLine().equalsIgnoreCase("Yes"));
}

}

OTHO if you want to store the state associated with each instance you create, then you would do it like this:

public class MyClass {

private final Scanner scanner = new Scanner(System.in);

public void doSomething() {
    List<Object> data = new ArrayList<Object>();
    Object object;
    do {
        // call your method here
        object = new Object();
        // object.yourMethod();
        data.add(object);

        System.out.println("Do you wish to continue entering data?");
    } while (scanner.nextLine().equalsIgnoreCase("Yes"));

    for(Object d : data) {
        // do something with the info you captured
    }
}

}

Sign up to request clarification or add additional context in comments.

Comments

1

No it would not have to be saved for any reason if you just wanted it to run a program (I assume method).

Object name;
Scanner scan = new Scanner(System.in);
String answer = "";

do {
    name = new Object();
    name.method();

    System.out.print("Do you wish to continue entering data? ");
    answer = scan.nextLine().toLowercase(); //Get the response
} while(!answer.equals("yes"); //If they didn't enter yes then the loop stops

This method also saves you memory because you do not create a new Scanner or new memory each iteration of the loop.

Comments

1

You could use an ArrayList.

List<Object> names = new ArrayList<Object>();
Scanner keyboard = new Scanner(System.in);
String answer;
do {
    Object o = new Object();
    o.method();
    names.add( o );

    System.out.println("Do you wish to continue entering data");
    answer = keyboard.nextLine();
} while (answer.equalsIgnoreCase("Yes"));

Comments

1

I would use something like an array list. So you would have your do/while and on completion of whatever your method does, add the objects to the list:

List<Object> list = new ArrayList<Object>();
Object name;

do {

    name = new Object();
    name.method();

    list.add(name);

} while(answer.equalsIgnoreCase("Yes"));

Something along those lines.

Comments

0

typically it would look like this:

Collection<Object> objectCollection = new ArrayList<Object>();  
for(int i = 0; i < objectMax; i++)  
{  
    Object o = new Object();  
    o.doSomething();  
    objectCollection.add(o);  
}

If you don't want to store anything it is just this:

 for(int i = 0; i < objectMax; i++)  
    {  
        Object o = new Object();  
        o.doSomething();  
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.