-2

I've tried the following but am not getting any output:

ArrayList<String> list=new ArrayList ();
Scanner s=new Scanner(System.in);

for(int i=0; i<list.size(); i++)
{
    System.out.println("Enter string  "+(i+1));
    String se = s.next();        
    list.add(se);
}

for(int i=0; i<list.size(); i++)
{
    System.out.print(list.get(i));
}
4
  • 1
    You can't use a for-loop on the list's size for the purpose of creating the list in the first place. You need to have some other control mechanism, such as a while-loop that continues until the user enters some sort of "finished" value. Commented Jun 10, 2019 at 13:49
  • i don't actually know how to use while here actually i am new to array list Commented Jun 10, 2019 at 14:03
  • Using a while loop has nothing to do with ArrayList. Imagine that you have a program that reads user input and prints it out, and keeps going until the user enters a special value like "done" or "stop". Just, instead of printing the values out, add them to the list instead. Commented Jun 10, 2019 at 14:04
  • Possible duplicate of how can i use java scanner in while loop Commented Jun 10, 2019 at 14:08

4 Answers 4

1

You need to loop on your Scanner input until you get an empty line, not on your List. Your List is empty to start with so you will not enter your loop.

List<String> list = new ArrayList<>();
Scanner s = new Scanner(System.in);

int counter = 1;
String userInput;
System.out.println("Enter string "+ counter);
while (true) { // Infinite loop, you need a break inside of the loop to get out of it
    // Assign the input value to the userInput variable
    userInput = s.nextLine();
    // Stop looping when it is an empty line
    if (userInput.isEmpty()) {
        break;
    }

    list.add(userInput);
    counter++;
    System.out.println("Enter string "+ counter);
}

for (String st : list) {
    System.out.println(st);
}
Sign up to request clarification or add additional context in comments.

Comments

0
here the size is specified by the user...
now it works...

 ArrayList<String> list=new ArrayList ();
         Scanner s = new Scanner(System.in);
            System.out.println("How many strings to add");
            int a = s.nextInt();
            for(int i=0; i<a; i++)
            {
                System.out.println("Enter string  "+(i+1));
                String se = s.next();        
                list.add(se);
            }

            for(int i=0; i<list.size(); i++)
            {
                System.out.print(list.get(i));
            }

5 Comments

i don't actually wants to get size from user i want to make it infinite
It will not work, you have to change in first loop i<list.size to i < a
To make it “infinite” you need a while-loop as others have said.
OK thanks yes that was done by mistake .i have corrected that .
@TeekhiiMirchii You want an infinite user-input loop that collects all inputs and stores them in a list?
0

The reason you are not getting any output is because you are using list.size() as a comparison value in a loop before you've populated the list with elements. It is empty so it's size will always be 0 until you add some elements to it.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

The quote above is from the List Javadoc. Keep in mind that it's always a good idea to read the documentation of new concepts you are trying to use.

You can't use a for-loop on the list's size for the purpose of creating the list in the first place. You need to have some other control mechanism, such as a while-loop that continues until the user enters some sort of "finished" value.

So instead of using the list size (like the comment above states) you should be using another control mechanism like a local variable which can define the size of your list. It can also be used to set the initial capacity of your list.

    // Use this local variable as a control mechanism
    final int listSize = 10;

    // Create new array with the initial capacity set to 10
    List<String> list = new ArrayList<>(listSize);
    Scanner s = new Scanner(System.in);

    // Use a dedicated integer value for the loop
    for(int i = 0; i < listSize; i++)
    {
        System.out.println("Enter string  " + (i+1));
        String se = s.nextLine();
        list.add(se);
    }

    // Once the list has been populated we can use it's
    // size as a comparison value in a loop
    for(int i = 0; i < list.size(); i++)
    {
        // Print each string in a new line
        System.out.println(list.get(i));
    }

Couple of notes that might help you in the future:

  • Use System.out.println instead of System.out.print whenever you want to print each log in a separate line.

  • Format your code in a readable manner so it's easier for both you and others to review it. In my opinion this includes separating each element in a syntax with at least a single whitespace as well as following the proper naming convention.

Comments

0

First loop in your code tries to iterate over values between 0 and list.size() - also 0, because your list is empty.

In this example your program will keep asking for string unless user provide STOP_WRITING_CODE value which is exit.

static final String STOP_WRITING_CODE = "exit";

ArrayList<String> list=new ArrayList();
Scanner s=new Scanner(System.in);
String se = "";

while (true) {
    System.out.println("Enter string: ");
    se = s.next();
    if(se != STOP_WRITING_CODE)
        break;

    list.add(se);
}

for(int i=0; i < list.size(); i++) {
    System.out.print(list.get(i));
}

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.