2

My program is that the ArrayList stores the entered number, and when the user enters the number -1, the program will stop end print out all the entered number in ascending order. My program runs, but let the number enters, it doesn't print the numbers out. it seems like if statement doesn't working. how to call the ArrayList in if statement to work right?

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

public class testArrayList {        

    public static void main(String[] args) {

        Scanner inputs = new Scanner(System.in);
        ArrayList <Integer> nums = new ArrayList<Integer>();

        System.out.println("Enter a number(-1 to end): ");

        while(inputs.hasNextInt()) {
            nums.add(inputs.nextInt());
        }

        // it looks like it runs until here. 

            for (Integer n : nums) {
                if (n == -1) {
                    Collections.sort (nums);
                    System.out.println("Here is the list of numbers : ");
                    System.out.println(n);
                }  
            }        
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Michelle
  • 41
  • 1
  • 8

7 Answers7

0

System.in will block and wait for a new input, and as long as you continue entering numbers, hasNextInt() will always be true. Instead, you should check for -1 in the initial input (while) loop:

while(inputs.hasNextInt()) {
    int num = inputs.nextInt();
    if (num == -1) {
        break;
    }
    nums.add(num);
}

nums.sort();
System.out.println("Here is the list of numbers : ");
System.out.println(nums);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • We said the same. Your code differs in the output, though. Your output uses the toString-method of the list leading to output like {1, 2, 3, 7, 10] whereas the original code outputs each number in a single line. – Lothar Aug 31 '17 at 19:35
0

Your loop adding numbers to the list don't check for the value -1 to stop the loop. -1 is an int, so it's continuing.

After you've fixed that you will run into the next problem. You're iterating over the elements of the list and do a sort each time the user entered 1. I haven't checked but this will most likely lead to an exception because iterators get not amused if you change the underlying list while iterating over it. According to your description, you should call sort before the loop and then just do the System.out.println within the loop.

Lothar
  • 5,323
  • 1
  • 11
  • 27
0

My program runs, but let the number enters, it doesn't print the numbers out. it seems like if statement doesn't working.Trying performing once step at a time:

In your code, it would do so only is n a value from the list nums is equal to -1.

Instead you should try to put your code as:

// Accept user input
Integer input = inputs.nextInt();

// check if it is not -1
while(input != -1) {
    nums.add(input);
    input = inputs.nextInt()
}

// sort the arraylist
Collections.sort (nums);

// print the elements
System.out.println("Here is the list of numbers : ");         
for (Integer n : nums) {
    System.out.println(n);
}
Naman
  • 27,789
  • 26
  • 218
  • 353
0

The if statement is never reached, you can check this by adding a println just before it which is never printed if because the scanner ignores whitespace. It will keep trying to take input until you enter a letter. To get the desired effects, I would probably just do n = inputs.nextInt(); then if n is -1 use break. Also your whole for loop and if condition is unnecessary, just sort the list and print it -using .toString because otherwise it will just be a memory address-because you do that in every case (although you may want to remove the -1).

Dom
  • 69
  • 4
0

i know there is a few answers, but here's my version :

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

    public class testArrayList 
    {        

    public static void main(String[] args) 
    {

        Scanner inputs = new Scanner(System.in);
        ArrayList <Integer> nums = new ArrayList<Integer>();

        System.out.println("Enter a number(-1 to end): ");

        while(inputs.hasNextInt()) 
        {
            //check if the next value is -1
            int nextVal = inputs.nextInt();
            if(nextVal == -1)
            {
              break; //then exit the loop
            }
            else
            {
              nums.add(inputs.nextInt()); //else add to the list
            }

        }

      //since we exited the loop, we can sort the list, and print header 
      Collections.sort (nums);
      System.out.println("Here is the list of numbers : ");
      for (int i = 0 ; i < nums.size(); i++) 
      {                             
            System.out.println(nums.get(i)); // print every sorted int from 
                                             // the list
      }  
    }        
}

There was a problem with the logic of your code when printing, and also when reading for the -1 exit code. Now it's in the read loop, and then we print the whole sorted list.

tested here : https://www.jdoodle.com/online-java-compiler

TinkeringMatt
  • 181
  • 1
  • 9
0

I have reorganized the code and make it much more simpler, as you are trying to exit the map when -1 value is entered.

public class TestArrayList {

     public static void main(String[] args) {

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

            while(true ) {
                System.out.println("Enter a number(-1 to end): ");
                int value = scanner.nextInt();

                /*read until your input is -1*/
                if ( value != -1 ){
                    nums.add(value);
                }else {
                    Collections.sort (nums);
                    System.out.println("Here is the list of numbers : ");
                    for ( int i : nums){
                        System.out.println(i);
                    }
                    break;
                }
            }
            scanner.close();
        }
}
Mad-D
  • 4,479
  • 18
  • 52
  • 93
0

Scanner inputs = new Scanner(System.in); ArrayList nums = new ArrayList();

    System.out.println("Enter a number(-1 to end): ");

    while (inputs.hasNextInt()) {
        //Accepts the inputs
        int input = inputs.nextInt();
        //Checks condition
        if (input == -1) {
            break;// breaks out of the loop is fails
        }
        nums.add(input);// else keep adding
    }
    System.out.println("Here is the list of numbers : ");
    Collections.sort(nums);
    for (Integer n : nums) {
        System.out.println(n);
    }
Africanfruit
  • 212
  • 3
  • 6