-3

I'm making a program for my assignment. This is not the whole program but it's just a part of it.

I want from the user to enter some integer values to be stored in "items" arrays. When the user input "stop" the loop should close and here is the problem.. when I write stop the program stops and give me some errors.

 public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     int i=0, lines=1;
     int[] items = new int[100];
     int total = 0;
     System.out.println("Enter the items with its price");

     while(true){
          i=i+1;
          if ("stop".equals(scan.nextLine()))
              break;
          else
              items[i] = scan.nextInt();
     } 

 }
Roshana Pitigala
  • 7,058
  • 8
  • 38
  • 66
mkabbanii
  • 39
  • 1
  • 9
  • 3
    What are the errors? That's rather important – Carcigenicate Apr 20 '17 at 16:51
  • "some errors" - It is worth including these errors in your question. – px06 Apr 20 '17 at 16:51
  • There you go: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at mohammedkabbani_301502670.MohammedKabbani_301502670.main(MohammedKabbani_301502670.java:34) C:\Users\Mohammed\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 8 seconds) – mkabbanii Apr 20 '17 at 16:52
  • 1
    Also, please fix your indentation. The body of the loop is wonky. – Carcigenicate Apr 20 '17 at 16:53
  • 2
    Please don't try to comment on your own question to explain it. Instead, [edit] your question to add additional information. – azurefrog Apr 20 '17 at 16:53
  • `nextInt()` gives you an error because with `nextLine()` it would have read and discarded any number you typed. – Peter Lawrey Apr 20 '17 at 16:54
  • @Mick2160 and have you looked up what it means if `nextInt` throws a `InputMismatchException`? Everything you need to solve this is in that stack trace. – Carcigenicate Apr 20 '17 at 16:54
  • Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Tom Apr 20 '17 at 17:15

3 Answers3

1

There are certain mistakes in your code. It's more better if you could just add the error.

Try this code.

public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     int i = 0, lines = 1;
     int[] items = new int[100];
     int total = 0;
     System.out.println("Enter the items with its price");

     while(true){
          String InputTxt = scan.nextLine();
          if (InputTxt.equals("stop"))
              break;
          else{           
              try{
                 items[i] = Integer.parseInt(InputTxt);
                 i++;
              }catch(Exception e){
                 System.out.println("Please enter a number");
              }
          }

     } 

 }
Roshana Pitigala
  • 7,058
  • 8
  • 38
  • 66
1

On top of other answers, I would like to advise you to change the looping from

while(true)

to

//first you need to remove the local variable i 
for(int i = 0; i < items.length; ++i)

Using this approach will help you to avoid IndexOutOfBoundsException when users key in more than 100 integer values.

Jie Heng
  • 146
  • 4
-1

your problem is this line : items[i] = scan.nextInt(); because you are trying to get integer while the input is string stop

EDIT one possible solution is that you get your data as string and check if it is stop or not and if not then try to parse it to integer like code bellow:

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
int i=0, lines=1;
int[] items = new int[100];
int total = 0;
System.out.println("Enter the items with its price");
while(true)
    {
    i=i+1;
     String str = scan.nextLine()
     if ("stop".equals(str))
         break;
     else
         {
         items[i] =  Integer.parseInt(str)
         }
    }
}
pouyan
  • 3,192
  • 2
  • 21
  • 38
  • Ok so what should I do now? I want from the user to input integers and when he finish writing the integers he should write stop to end the program. – mkabbanii Apr 20 '17 at 16:57
  • I don't think this is the issue as `scan.nextLine()` will read anything at that stage, this isn't a `NumberFormatException` where there is problems with conversion. – px06 Apr 20 '17 at 17:03
  • @px06 you are right about `scan.nextLine()` but consider that in the code there is another line that is `items[i] = scan.nextInt();` so it will try to get input as integer. – pouyan Apr 20 '17 at 17:05