0

I'm trying to write a loop that uses scanner to add integers to an established integer array list. It has to to be able to terminate the integer entries and proceed once "x" is entered, and prompt the user to re-enter an integer if any other letter is entered. My problem is that using .nextInt() I get run-time errors when entering a String. Am I simply using the wrong scanner method? This is what I have so far. I imported java.util.*

public static void main (String [] args)
   {
      Square square = new Square(); //constrcutor that establishes array list "square"
      Scanner in = new Scanner(System.in);
      int value = 0;

      while (value != "x")
      {
         System.out.print("Enter an integer(x to exit): ");
         value = in.nextInt();

         if (in.nextInt() != "x" || value !> Integer.MIN_VALUE && value !< Integer.MAX_VALUE)
         {
            System.out.println("end");
            System.exit(0);
         }
      square.add(value);   
      }
   }
Arkoh
  • 11
  • 1
  • 1
  • 2
  • 1
    [`Use equals to compare`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – sam Nov 04 '15 at 22:19
  • 1
    an `int` is *never* going to be equal to a `String` – azurefrog Nov 04 '15 at 22:21
  • 1
    `value = in.nextInt();` is pointless if you want to compare the value with a `String`, because a `int` is not `String` by any measure of the imagination. Instead use `in.nextLine`, check to see if the value `equals("x")`, if it doesn't use something like `Integer.parseInt(String)` to parse the value to a `int` value which you can process further – MadProgrammer Nov 04 '15 at 22:21

0 Answers0