0

how can I get it to go back to start another directory when the user types yes and also how do I print the arraylist out using a print format

thanks

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

public class AAA
{
    public static void main(String[] args) 
{

    ArrayList<String> name = new ArrayList<String>();
    ArrayList<Integer> phone = new ArrayList<Integer>(); 
    Scanner scanner = new Scanner(System.in); 


    {
        System.out.println("Please enter your name: ");
        name.add(scanner.next());
        System.out.println("Please enter your number: ");
        phone.add(scanner.nextInt());

        System.out.println("Do you want to add a directory yes/no?");
        String answer = scanner.nextLine(); 

        if (answer.equals("yes"));
        //want it to go back to start another direcotry here

        else 
    {

        System.out.println("Thanks for adding to the directory");
        System.out.println(answer());
    }


    }

    }
}
1

4 Answers 4

1

Try to use a while true, and then, when the user types no, you break the look. The code seems like this:

ArrayList<String> name = new ArrayList<String>();
ArrayList<Integer> phone = new ArrayList<Integer>(); 
Scanner scanner = new Scanner(System.in); 

while(true){
    System.out.println("Please enter your name: ");
    name.add(scanner.next());
    System.out.println("Please enter your number: ");
    phone.add(scanner.nextInt());

    System.out.println("Do you want to add a directory yes/no?");
    String answer = scanner.next(); 

    if (answer.equals("no")){
        System.out.println("Thanks for adding to the directory");
        System.out.println(answer());
        break; //
    }

}

There are another strategies, like using a do/while. But essentially you have to use a loop.

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

2 Comments

hi thanks for the help with break in that position in the code it does not let me add more and when i remove it i cant stop adding can you help ?
Ok, I forgot something. Change the line: String answer = scanner.nextLine() to String answer = scanner.next(), and it will work.
1

You can easily print out an ArrayList:

System.out.println(list);

It will print something like:

[one, two, three]

where one, two, three are the results of invocation of toString() or array list's elements.

You can remove [] easily by:

System.out.println(list.toString().replace("[", "").replace("]", ""));

You you need to use your custom format implement print in loop:

for (String s : list) {
    System.out.println(format(s));
}

Comments

1
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class COL {
    public static void main(String [] args) {


        List<Integer> phone = new ArrayList<Integer>(); 
        Scanner scanner =new Scanner (System.in);

        while (true) {
            System.out.println("Please enter your number: ");
            phone.add(scanner.nextInt());

            System.out.println("Do you want to add a directory yes/no?");
            String answer = scanner.next(); 
            if (answer.equals("no")){
                System.out.println("Thanks for adding to the ArrayList");
                System.out.println("\t "+phone);
                break; //
            }

        }
    }

}

1 Comment

Addinf brief explanation would be better than just posting a piece of code
0
import java.util.ArrayList;
import java.util.Scanner;

public class AAA {

    public static void main(String[] args) {

        ArrayList<String> name = new ArrayList<String>();
        ArrayList<Integer> phone = new ArrayList<Integer>();

        Scanner scanner = new Scanner(System.in);
        String answer = "";

        {
            do {
                System.out.println("Please enter your name: ");
                name.add(scanner.next());
                System.out.println("Please enter your number: ");
                phone.add(scanner.nextInt());
                System.out.println("Do you want to add a directory yes/no?");
                answer = scanner.next();
            } while (answer.equals("yes"));
            if (answer.equals("yes")); //want it to go back to start another direcotry here
            else {

                System.out.println("Thanks for adding to the directory");
                for (int i = 0; i < name.size(); i++) {
                    System.out.print(name.get(i)+"\t");
                    System.out.print(phone.get(i));
                    System.out.println("");
                }
            }


        }

    }
}

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.