java - Close Scanner when entering "0" -


i writting program user enters string input , program finds number of words , number of int numbers. scanner should close when user enters "0".

my code

import java.util.scanner;  public class runme { public static void main(string[] args) {     scanner in = new scanner(system.in);     string  input = in.nextline();     counter numbers = new counter();     counter words = new counter();     scanner s = new scanner(input).usedelimiter("\\s");      while(s.nextint() != 0)     {         if(s.hasnextint())         {             numbers.add();         }         else         {             words.add();         }     }      s.close();     in.close();     system.out.println("number of numbers : " + numbers.value());     system.out.println("number of words : " + words.value());  } } 

and class counter

public class counter {     private int value;      public int add()     {         value++;         return value;     }     public int value()     {         return value;     } } 

when run program following excepion:

exception in thread "main" java.util.inputmismatchexception     @ java.util.scanner.throwfor(scanner.java:909)     @ java.util.scanner.next(scanner.java:1530)     @ java.util.scanner.nextint(scanner.java:2160)     @ java.util.scanner.nextint(scanner.java:2119)     @ runme.main(runme.java:13) 

with scanner console input must feed using "enter". avoid this, other native library required, such jline. there other options in this similar question.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -