Results 1 to 9 of 9
  1. #1
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default This Is Not Working.

    I am trying to make it so when a user inputs anything other than the string "y" or "n", it throws an exception and says "Illegal Character". Now, I got it working like I want it too, but it throws an exception for EVERYTHING I type, even "y" and "n". Can someone help me? I'm not even sure an exception is the correct way to handle mis-entered input.

    Important Code:
    Java Code:
    import java.util.*;
    
    public class ReviewAgain {
    
         private String ifAgain;
         
         void secondReview() {
             Scanner prompt = new Scanner(System.in);
             System.out.println("Would you like to test another review? ");
             ifAgain = prompt.next();
             
             String ifIsAgain = ifAgain;
                while (!ifIsAgain.equals("y") || !ifIsAgain.equals("n")){
                try {
                    throw new InputMismatchException();
                    } catch (InputMismatchException ime) {
                        System.out.println("Illegal Character");
                    }
                    secondReview();
                }
         }
    }
    Main class:
    Java Code:
    public class ReviewLauncher {
    	
    public static void main (String[] args) {
    
    	ReviewHandler start = new ReviewHandler();
    	start.reviews();
    		
    	ReviewAgain rA = new ReviewAgain();
    	rA.secondReview();
    }
    }
    Other parts of the whole program:
    Java Code:
    import java.util.*; 
    
    public class KeyboardInput {
    
    private String name;
    private int firstStar, secondStar, thirdStar, fourthStar, fifthStar;
    
    public void fiveStar() {
    
    Scanner thisIsName = new Scanner(System.in);
    System.out.println("\n" + "Name Of Game? ");
    name = thisIsName.next();
    	
    Scanner starScanner = new Scanner(System.in);
    System.out.println("Number of 1 star reviews: ");
    firstStar = starScanner.nextInt();
    
    System.out.println("Number of 2 star reviews: ");
    secondStar = starScanner.nextInt();
    
    System.out.println("Number of 3 star reviews: ");
    thirdStar = starScanner.nextInt();
    
    System.out.println("Number of 4 star reviews: ");
    fourthStar = starScanner.nextInt();
    
    System.out.println("Number of 5 star reviews: ");
    fifthStar = starScanner.nextInt();
    }
    
    public String getName() {
    	return name;
    }
    public int getStar1() {
    	return firstStar;
    }
    public int getStar2() {
    	return secondStar;
    }
    public int getStar3() {
    	return thirdStar;
    }
    public int getStar4() {
    	return fourthStar;
    }
    public int getStar5() {
    	return fifthStar;
    }
    }
    Java Code:
    public class ReviewHandler {
    	
    public void reviews() {
    
    KeyboardInput k = new KeyboardInput();
    k.fiveStar();
    
    int starOne = k.getStar1() * 1;
    int starTwo = k.getStar2() * 2;
    int starThree = k.getStar3() * 3;
    int starFour = k.getStar4() * 4;
    int starFive = k.getStar5() * 5;
    
    int reviewCount = k.getStar1() + k.getStar2() + k.getStar3() + k.getStar4() + k.getStar5();
    
    double reviewStarCount = starOne + starTwo + starThree + starFour + starFive;
    
    double reviewAverage = reviewStarCount / reviewCount; 
    
    reviewAverage = reviewAverage * 100;
    reviewAverage = Math.round(reviewAverage);
    reviewAverage = reviewAverage / 100;
    
    System.out.println("\n" + k.getName() + " has " + reviewCount + " total reviews.");
    System.out.println("Average review score of " + k.getName() + " is: " + reviewAverage);
    }}

    Basically, as I've been learning Java, I have implemented all I've learned into this program. In the end it will be a GUI with all kinds of cool stuff, but right now I am focusing on handling exceptions. If the user inputs a String in an int, for example. Help and criticism is appreciated!

    Thanks.
    Last edited by FatalSylence; 10-15-2010 at 10:17 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    When is this conditional ever false?
    Java Code:
    while (!ifIsAgain.equals("y") || !ifIsAgain.equals("n")){


    Shoot test it for yourself:
    Java Code:
    public class Foo {
        public static void main(String[] args) {
            for (char myChar = 'a'; myChar <= 'z'; myChar++) {
                String string = String.valueOf(myChar);
                
                if (!string.equals("y") || !string.equals("n")) {
                    System.out.println("True for:  " + string);
                } else {
                    System.out.println("False for: " + string);
                }
            }
        }
    }
    Does it ever return false?

    Solution: re-think your logic; then test it.

  3. #3
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    When is this conditional ever false?
    Java Code:
    while (!ifIsAgain.equals("y") || !ifIsAgain.equals("n")){


    Shoot test it for yourself:
    Java Code:
    public class Foo {
        public static void main(String[] args) {
            for (char myChar = 'a'; myChar <= 'z'; myChar++) {
                String string = String.valueOf(myChar);
                
                if (!string.equals("y") || !string.equals("n")) {
                    System.out.println("True for:  " + string);
                } else {
                    System.out.println("False for: " + string);
                }
            }
        }
    }
    Does it ever return false?

    Solution: re-think your logic; then test it.

    I wanted to set it so that there was only two answers, y and n. So, while the user inputs something other than y or n, it throws an exception. But when they DO enter y or n, it ends the loop.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by FatalSylence View Post
    I wanted to set it so that there was only two answers, y and n. So, while the user inputs something other than y or n, it throws an exception. But when they DO enter y or n, it ends the loop.
    I know what you want. Run my code to see why yours is not working, then you may wish to do as I suggest: re-think the logic of your boolean condition so that it works. It will only require a single simple change, and my little test harness will help you to test your logic so you'll know when you have it right.

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    :eek:

    your if-statement is wrong....

    what happens if the user typed y ? the first statement is false (okay!) but whats about the second one? its true (and vice versa)
    false OR true --> ???

    EDIT: @ Fubarable, ähh yes, not the if, the while :) i looked at your code ^^ (or in other words: i mean his code ;> )
    Last edited by eRaaaa; 10-15-2010 at 11:24 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by eRaaaa View Post
    :eek:

    your if-statement is wrong....

    what happens if the user typed y ? the first statement is false (okay!) but whats about the second one? its true (and vice versa)
    false OR true --> ???
    Don't you mean his while condition?

    I've been trying to get him to see just this by running my code which mimics this condition in an if block. If he can actually see the results, hopefully it will be enough of a knock on the head to have him change the condition. Again, it's just a slight change he has to make...

  7. #7
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I know what you want. Run my code to see why yours is not working, then you may wish to do as I suggest: re-think the logic of your boolean condition so that it works. It will only require a single simple change, and my little test harness will help you to test your logic so you'll know when you have it right.
    Java Code:
             ReviewHandler start = new ReviewHandler();
             
             String ifIsAgain = ifAgain;
             if (ifIsAgain.equals("n")) {
                 System.exit(0);
             }
             if (ifIsAgain.equals("y")){
                 start.reviews();
                } else {
                    try {
                        throw new InputMismatchException();
                    } catch (InputMismatchException ime) {
                        System.out.println("Invalid character, enter only 'y' or 'n'.");
                    }
         }
      }
    }

    Thank you very much! Now that I figured it out, I am glad you did not tell me but made me think about it. Thank you!

    My logic was flawed in that I thought it had to be a while loop, but I misinterpreted the try catch methods, because I thought without a loop, the exception would terminate the program.

    By the way, how was my encapsulation? I did notice a definite advantage in making things private, because even if something was broken, I could narrow it down to one class.

    Oh, and, are exceptions the correct way to handle things like that?
    Last edited by FatalSylence; 10-15-2010 at 11:41 PM.

  8. #8
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    So, let's say I wanted to make an exception for all of the inputs that I have. Would I have to create an exception for every one? Or could I do some sort of global exception handler?

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
     if (ifIsAgain) {
                  throw new InputMismatchException();
             }
    should do the job.

Similar Threads

  1. Why is my Do/While Not working?
    By Meta in forum New To Java
    Replies: 1
    Last Post: 05-11-2010, 06:05 PM
  2. working with JC
    By yuhobebbho in forum New To Java
    Replies: 0
    Last Post: 02-10-2010, 11:22 PM
  3. ActionListener NOT working
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 03:40 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Tags for this Thread

Posting Permissions

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