3 Replies Latest reply: Jan 26, 2010 5:29 PM by 843789 RSS

    Testing Scanner class,  compile succes, but never end execute

    843789
      Hi, im studyng the Katy Sierras book, "sun ceritified programmer java 6", in chapter 6 there are a code that show how Scanner works, i tried to test this code (im using NetBeans IDE), but the problem is that never it can stop executing, no result, no exception, only continues executing without ending

      Here is the code:
      import java.util.Scanner;
      
      public class ScannerNext {
          public static void main(String [] args){
              boolean b2, b;
              int i;
              String s, hits = " ", input ="1 true 34 hi" ;
              Scanner s1 = new Scanner(input);
              Scanner s2 = new Scanner(input);
              while(b = s1.hasNext()){
                  s = s1.next(); hits += "s";
              }
              while(b = s2.hasNext()){
                  if(s2.hasNextInt()){
                      i = s2.nextInt(); hits += "i";
                  } else if (s2.hasNextBoolean()){
                      b2 = s2.hasNextBoolean(); hits += "b";
                  } else {
                      s2.next(); hits += "s2";
                  } // if else if
              }//while
              System.out.println("Hits: " + hits);
          }//main
      }
      Apologies about my english.
      Greetings from Orizaba, Veracruz, Mex.