Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22

Thread: Need help

  1. #21
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Ah, now you're running into one bit of funny business that can occur with use of Scanner objects. When you call Scanner#next() or Scanner#nextInt() or similar Scanner methods, you get the next appropriate token, but scanner doesn't get and swallow the nextLine, which can be fed to the next Scanner call as the next token, messing you up royally. Solution, use nextLine() instead of next() if possible, or if you need nextInt() and similar methods, then call nextLine() soon after to swallow the end of line token. For instance:

    Java Code:
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.math.BigInteger;
    
    public class Conversion {
      public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        String repeatChoice = "";
        do {
          System.out.println("Enter number to convert: ");
          String number = kbd.next();
          kbd.nextLine();
    
          Pattern p = Pattern.compile("[0-9a-fA-F]");
          Matcher m = p.matcher(number);
          if (m.find()) {
          } else
            do {
              System.out.println("Enter number to convert Again: ");
              number = kbd.next();
              kbd.nextLine();
              m = p.matcher(number);
            } while (!m.find());
    
          int originalBase = -2;
          while (originalBase < 2 || originalBase > 16) {
            System.out.println("Enter the Orginal Base: ");
            originalBase = Integer.parseInt(kbd.next());
            kbd.nextLine();
            if (originalBase < 2) {
              System.out.println("Too low");
            } else if (originalBase > 16) {
              System.out.println("Too high");
            }
          }
          BigInteger n = new BigInteger(number, originalBase);
          int base = -2;
          while (base < 2 || base > 16) {
            System.out.println("Enter base FROM 2 - 16 ONLY: ");
            base = Integer.parseInt(kbd.next());
            kbd.nextLine();
            if (base < 2) {
              System.out.println("Too low");
            } else if (base > 16) {
              System.out.println("Too high");
    
            }
    
          }
          System.out.println("Result is:" + n.toString(base));
    
          // Let's get the user's input from within the loop:
          System.out.println("Do you want to try it again? [yes/no]: ");
          repeatChoice = kbd.nextLine();
    
        } while (repeatChoice.equalsIgnoreCase("yes"));
      }
    }

  2. #22
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    Ah, now you're running into one bit of funny business that can occur with use of Scanner objects. When you call Scanner#next() or Scanner#nextInt() or similar Scanner methods, you get the next appropriate token, but scanner doesn't get and swallow the nextLine, which can be fed to the next Scanner call as the next token, messing you up royally. Solution, use nextLine() instead of next() if possible, or if you need nextInt() and similar methods, then call nextLine() soon after to swallow the end of line token. For instance:

    Java Code:
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.math.BigInteger;
    
    public class Conversion {
      public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        String repeatChoice = "";
        do {
          System.out.println("Enter number to convert: ");
          String number = kbd.next();
          kbd.nextLine();
    
          Pattern p = Pattern.compile("[0-9a-fA-F]");
          Matcher m = p.matcher(number);
          if (m.find()) {
          } else
            do {
              System.out.println("Enter number to convert Again: ");
              number = kbd.next();
              kbd.nextLine();
              m = p.matcher(number);
            } while (!m.find());
    
          int originalBase = -2;
          while (originalBase < 2 || originalBase > 16) {
            System.out.println("Enter the Orginal Base: ");
            originalBase = Integer.parseInt(kbd.next());
            kbd.nextLine();
            if (originalBase < 2) {
              System.out.println("Too low");
            } else if (originalBase > 16) {
              System.out.println("Too high");
            }
          }
          BigInteger n = new BigInteger(number, originalBase);
          int base = -2;
          while (base < 2 || base > 16) {
            System.out.println("Enter base FROM 2 - 16 ONLY: ");
            base = Integer.parseInt(kbd.next());
            kbd.nextLine();
            if (base < 2) {
              System.out.println("Too low");
            } else if (base > 16) {
              System.out.println("Too high");
    
            }
    
          }
          System.out.println("Result is:" + n.toString(base));
    
          // Let's get the user's input from within the loop:
          System.out.println("Do you want to try it again? [yes/no]: ");
          repeatChoice = kbd.nextLine();
    
        } while (repeatChoice.equalsIgnoreCase("yes"));
      }
    }
    Thanks , can you visit my thread too here.
    Assistance about this.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •