Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Fixing the scan input

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fixing the scan input

    Im quite close to fixing this problem i've been having for a while, allow me to post my coding:

    1. Driver
     
    import java.util.*;
     
     
    public class Driver
    {
      public static void main(String[] args) 
      {
     
        Coin myCoin = new Coin();
        boolean bSuccess, bResult;
        Boolean str;
     
        Scanner scan = new Scanner(System.in);
     
        myCoin.setKey(1234);
        bSuccess = myCoin.flip(); // Should Fail, coin is locked
        bResult = myCoin.unlock(1234);
     
        System.out.println("Please enter the password:" + bResult);
        {
          if (bResult) // coin is unlocked
          {
            System.out.println("Access Granted");
            System.out.println(myCoin.toString());
          }
          else
          {
            System.out.println("Wrong password");
          }
     
        }
     
      }
     
     
    }

    2. Interface
    public interface Lockable
    {
      boolean locked();
      void setKey(int password);
      boolean lock(int password);
      boolean unlock(int password);
     
      }

    3. Coin
    public class Coin implements Lockable
    {
      private int key; // password to make it lockable
      private boolean locked; // if true, object is locked
      private final int HEADS = 0;
      private final int TAILS = 1;
      private int face;
     
      public boolean locked()
      {
        return this.locked;
      }
     
      public void setKey(int password)
      {
        this.key = password;
        this.locked = true;
     
      }
     
      public boolean lock(int password)
      {
        if (password == this.key)
        {
          this.locked = true;
          return true;
        }
        else
        {
          return false;
        }
      }
     
      public boolean unlock(int password)
      {
        if (password == this.key)
        {
          this.locked = false;
          return true;
        }
        else
        {
          return false;
        }
      }
     
      public Coin()
      {
        locked = true;
      }
     
      public boolean flip()
      {
        if (this.locked == false)
        {
          face = (int) (Math.random() * 2);
          return true;
        }
        return false;
      }
     
     
      public boolean isHeads()
      {
        return (face == HEADS);
      }
     
      public String toString()
      {
        if (this.locked == false)
        {
     
          String faceName;
     
          if (face == HEADS)
            faceName = "Heads";
          else
            faceName = "Tails";
     
          return faceName;
        }
        else
          return "";
      }
    }

    So when i try running the driver class with the current code:

    "Please enter your password: true"
    "Access Granted"
    "Heads"

    if i changed it to bResult = myCoin.unlock(12345) it will return

    "Please enter your password: false"
    "Wrong password"

    So here i have verified that the values do work and the code's right. Now i have to make it to it accepts a user input so ive tried implementing a boolean variable "str" and then implemening the scanner like so:

     
    import java.util.*;
     
     
    public class Driver
    {
      public static void main(String[] args) 
      {
     
        Coin myCoin = new Coin();
        boolean bSuccess, bResult;
        Boolean str;
     
        Scanner scan = new Scanner(System.in);
     
        myCoin.setKey(1234);
        bSuccess = myCoin.flip(); // Should Fail, coin is locked
        bResult = myCoin.unlock(1234);
     
        System.out.println("Please enter the password:");
        str = scan.nextBoolean();
        {
          if (bResult) // coin is unlocked
          {
            System.out.println("Access Granted");
            System.out.println(myCoin.toString());
          }
          else
          {
            System.out.println("Wrong password");
          }
     
        }
     
      }
     
     
    }

    And i get the following error:
     
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at java.util.Scanner.nextBoolean(Unknown Source)
    	at Driver.main(Driver.java:20)

    even if i inputted the right or wrong answer, it still results in the same error. I have tried changing boolean str to double, etc but that gives me wrong passwords even when inputting the right answer.

    What would be the solution to this issue im having?

    Thank you!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Fixing the scan input

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextBoolean(Unknown Source)
    at Driver.main(Driver.java:20)
    The Scanner class's nextBoolean method does like what was entered. Perhaps you should read in a String and compare it vs requiring the user to Enter true or false.

    Try debugging your code by adding printlns to show the values of variables as they change and are used by the program.

    In your last code you read a value into str but do NOT use it for anything???

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Fixing the scan input

    I have already replied such a post at other forums. I actually don't remember for now.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Fixing the scan input

    Quote Originally Posted by Norm View Post
    In your last code you read a value into str but do NOT use it for anything???
    Actually exception arises due to this statement.

    Well, i have tried this code and it works fine for me.
    And this program throws exception if i input other than
    true or false
    So, i can say that you are inputting values other than this. Right?
    if (bResult) // coin is unlocked
          {
            System.out.println("Access Granted");
            System.out.println(myCoin.toString());
          }
    And you are getting input in str but comparing bResult.

Similar Threads

  1. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  2. Scan the value of an object?
    By tuanvu_n in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: July 19th, 2011, 12:49 PM
  3. name=scan.nextLine();
    By Amro in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 12th, 2010, 07:01 AM
  4. Need simple JAVA program fixing ASAP
    By theviper in forum Paid Java Projects
    Replies: 1
    Last Post: April 14th, 2010, 10:59 AM
  5. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM

Tags for this Thread