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 7 of 7

Thread: Exception error display with one input and not the other

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception error display with one input and not the other

    The following code is supposed to generate random numbers and give some output as long as the user wants to continue. But the problem is it gives an exception if I press 'y'. No problem occurs if I enter 'n'. Kindly check and tell what wrong I am doing:

    import java.util.*; 
    import static java.lang.System.out;
    public class IsaacDice {
     
        public static void main(String[] args) {
     
        	out.println("Welcome to the dice game! Want to play?: Y/N");
        	Scanner reply=new Scanner(System.in);
        	char answer = reply.findInLine(".").charAt(0);
     
        	Random value=new Random();
        	while (answer=='y')
        	{
         		int randomNumber = value.nextInt(4) + 1;
    switch (randomNumber) {
    case 1:
    out.println("Yes. Isnít it obvious?");
    break;
    case 2:
    out.println("No, and donít ask again.");
    break;
    case 3:
    out.print("Yessir, yessir!");
    out.println(" Three bags full.");
    break;
    case 4:
    out.print("What part of Ďnoí");
    out.println(" donít you understand?");
    break;
    default:
    out.print("You think you have");
    out.print(" problems?");
    out.print(" My random number");
    out.println(" generator is broken!");
    break;
    }
    out.println("Goodbye! Want to play again? y/n:");
    out.println();
     
    answer = reply.findInLine(".").charAt(0);
    }
     
    out.println("Exiting game!");
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Exception error display with one input and not the other

    it gives an exception if I press 'y'
    Please post the full text of the error message so we can see what the problem is and where it happened.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception error display with one input and not the other

    If I press 'n' the output is as desired:

    Welcome to the dice game! Want to play?: Y/N
    n
    Exiting game!

    Process completed.

    However, if I enter 'y', the exception shows up:

    Welcome to the dice game! Want to play?: Y/N
    y
    What part of ‘no’ don’t you understand?
    Goodbye! Want to play again? y/n:

    Exception in thread "main" java.lang.NullPointerException
    at IsaacDice.main(IsaacDice.java:50)

    Process completed.

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception error display with one input and not the other

    hi the problem is solved here..
    you were actually reading "." after y or n, but m sure you have never terminated your input with "."
    because in the line char answer=reply.findInLine(".").charAt(0); means the reply will be the char at position 0, provided it should find "."

    i have simplified your code
    check this out
     
     
    import java.util.*; 
    import static java.lang.System.out;
    public class IsaacDice {
     
        public static void main(String[] args) {
        	for(;;){
        	out.println("Welcome to the dice game! Want to play?: Y/N");
        	Scanner reply=new Scanner(System.in);
        	char answer = reply.findInLine("y|Y|n|N").charAt(0);
           	Random value=new Random();
     
        		if(answer=='y'||answer=='Y'){
         		int randomNumber = value.nextInt(4) + 1;
         		switch (randomNumber) {
         		case 1:
         			out.println("number is 1");
         			break;
         		case 2:
         			out.println("number is 2");
         			break;
         		case 3:
         			out.println("number is 3");
         			break;
         		case 4:
        			out.println(" number is 4");
         			break;
         		default:
         			out.print("You think you have");
         			out.print(" problems?");
         			out.print(" My random number");
         			out.println(" generator is broken!");
         			break;
         		}
         		out.println("Goodbye!");
         		out.println();
     
        		}
        		else{
     
        			out.print("BYE!!");
        			System.exit(0);
        		}
        	}
     
    }
    }

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception error display with one input and not the other

    thank you for replying.
    but what if I simply change "reply.findInLine(.).charAt(0)" to "reply.findInLine("y").charAt(0)" in my code (I want to do it the way I have done i.e., while loop)?
    Shouldn't it remove this error? I still get the same exception, now with both inputs 'y' and 'n'.
    Kindly explain me what exactly I should do. Sorry, I'm a beginner to java.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,334
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Exception error display with one input and not the other

    @bhavesh, please read Announcements - What's Wrong With My Code? and The Problem with Spoon-feeding Another one of your posts has been edited, and the above could be considered borderline

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Exception error display with one input and not the other

    I still get the same exception,
    You should test what the findInLine() method returns BEFORE trying to use it.
    Save the value in a variable and test it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. NullPointer exception error
    By davx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2012, 12:08 PM
  2. Jsp Exception Error
    By toky in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: April 22nd, 2012, 05:04 PM
  3. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 10:41 AM
  4. How to display error message box
    By jasonxman in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 21st, 2011, 02:47 PM
  5. [SOLVED] Need help with input/output error
    By stefan2892 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2011, 09:44 AM