944,001 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2467
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 9th, 2007
0

If else statements on compatibility, I'm so confused here?????

Expand Post »
I'm working on a code on compatibility and it seems that after I ask and answer the first question, the program just stops, I have no clue why it doesn't go on to ask the next question, can somebody help my figure this out, thanks, oh here is my code.

This is what it says on the bottom of my code after I run it and answer Y:
----jGRASP exec: java Compatibility

Do you either play or watch football? (Y/N) Y
Do you like going to the movies? (Y/N) Exception in thread "main" java.lang.NullPointerException
at Compatibility.main(Compatibility.java:33)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


Here is my code:

import java.util.Scanner;


public class Compatibility {
public static void main (String [] args){
Scanner boysScanner = new Scanner(System.in);
//input variables
char reply1;
char reply2;
char reply3;
char reply4;
char reply5;
char reply6;


//display questions

System.out.print("Do you either play or watch football? (Y/N) ");
reply1 = boysScanner.findInLine(".").charAt(0);


System.out.print("Do you like going to the movies? (Y/N) ");
reply2 = boysScanner.findInLine(".").charAt(0);

System.out.print("Do you like education? (Y/N) ");
reply3 = boysScanner.findInLine(".").charAt(0);



System.out.print("Do you watch football? (Y/N) ");
reply4 = boysScanner.findInLine(".").charAt(0);

System.out.print("Do you like watching movies? (Y/N) ");
reply5 = boysScanner.findInLine(".").charAt(0);

System.out.print("Do you really like college? (Y/N) ");
reply6 = boysScanner.findInLine(".").charAt(0);



//determine by pairing
if (reply1 == reply4 && reply2 == reply5 && reply3 == reply6) {
System.out.println("You two are great for each other.");
}

if (reply1 != reply4 && reply2 != reply5 && reply4 != reply6) {
System.out.println("You two better stay away from each other");
}
if (reply1 != reply4 && reply2 == reply5 && reply3 == reply6) {
System.out.println("You two are somewhat good for each other.");
}

if (reply1 != reply4 && reply2 != reply5 && reply4 == reply6) {
System.out.println("You two better talk somemore.");
}
if (reply1 == reply4 && reply2 != reply5 && reply3 != reply6) {
System.out.println("You two better talk somemore.");
}

if (reply1 == reply4 && reply2 == reply5 && reply4 != reply6) {
System.out.println("You two are somewhat good for each other");
}

if (reply1 == reply4 && reply2 != reply5 && reply4 == reply6) {
System.out.println("You two are somewhat good for each other");
}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Sep 9th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

your code seems to be working fine i jsut copy and pasted it and compiled it and ran it and there is no problem with it. The one thing you might want to do is to either turn the user input into uppercase or lowercase because 'y' and 'Y' are not the same thing
Reputation Points: 10
Solved Threads: 1
Light Poster
nnobakht is offline Offline
47 posts
since Nov 2006
Sep 9th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

Do you either play or watch football? (Y/N) Y
Do you like going to the movies? (Y/N) Exception in thread "main" java.lang.NullPointerException
at Compatibility.main(Compatibility.java:33)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Then why does it keep saying this and, how are you able to go to the next question, because it doesn't go to the next question, after you answer, "Do you either play or watch football? It just does nothing after that.
It may not have no errors, but it is basically a dead code because it is not working right and i'm been trying to trouble shoo this for hours.
Last edited by Dio1080; Sep 9th, 2007 at 9:47 pm. Reason: responding back
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Sep 9th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

im not too sure i jsut copied and pasted the code into textpad compiled it and executed it. asked me some question i said y to all then i did the same thing again and said n and it worked jsut fine the link below is the video i took and uploaded. take a look
http://s193.photobucket.com/albums/z...rrent=test.flv
Reputation Points: 10
Solved Threads: 1
Light Poster
nnobakht is offline Offline
47 posts
since Nov 2006
Sep 9th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

before I click on the solve button, how were you guys able to get it to work, is it my software jgrasp or my complier, which works, how no clue, plus, I tried to zoom in as much as I could, hard to see but it looks like it was working find?
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Sep 10th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

I does not work when I compile it either.

You will find that the method boysScanner.findInLine(".") is returning NULL when you call it the second time. I don't know if this is because of how our consoles work ( mine is within eclipse ) or if there is some other reason for this. The boysScanner is not waiting for a keybord response it seems before executing the boysScanner.findInLine()
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Sep 10th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

try replacing boysScanner.findInLine(".").charAt(0) with boysScanner.nextLine().charAt(0);
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Sep 10th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

Click to Expand / Collapse  Quote originally posted by Paul.Esson ...
I does not work when I compile it either.

You will find that the method boysScanner.findInLine(".") is returning NULL when you call it the second time. I don't know if this is because of how our consoles work ( mine is within eclipse ) or if there is some other reason for this. The boysScanner is not waiting for a keybord response it seems before executing the boysScanner.findInLine()
hmmmmmm, somehere, but, when you watch that clip it seems to work, do you have a clue to whats going on?
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Sep 10th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

Yes, if you look at the API documentation for the Scanner class it says the following about the find InLine method

Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.

so we have not asked the scanner for the nextLine() and since there are no more occorances of . on the same line it will return a null object.

All you are doing with your patten as well is getting the entire string on that line, therefor you don't really need to use a patten.

So I hope this helps, If you want to use the patten matching you will still need to advance the line with nextLine
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Sep 10th, 2007
0

Re: If else statements on compatibility, I'm so confused here?????

Click to Expand / Collapse  Quote originally posted by Paul.Esson ...
Yes, if you look at the API documentation for the Scanner class it says the following about the find InLine method

Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.

so we have not asked the scanner for the nextLine() and since there are no more occorances of . on the same line it will return a null object.

All you are doing with your patten as well is getting the entire string on that line, therefor you don't really need to use a patten.

So I hope this helps, If you want to use the patten matching you will still need to advance the line with nextLine

yes, it works, but what do you mean by pattern matching, so far, its doing what I told it to do, like if the both say yes, then they like each other blah blah blah, but if I'm missing something, let me know, but hey, thanks for the help, I'll feel better sleeping tonight.
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Else If statement
Next Thread in Java Forum Timeline: How to separate a string ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC