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

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

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

 
0
  #1
Sep 9th, 2007
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");
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

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

 
0
  #2
Sep 9th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

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

 
0
  #3
Sep 9th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

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

 
0
  #4
Sep 9th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

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

 
0
  #5
Sep 9th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

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

 
0
  #6
Sep 10th, 2007
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()
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

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

 
0
  #7
Sep 10th, 2007
try replacing boysScanner.findInLine(".").charAt(0) with boysScanner.nextLine().charAt(0);
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

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

 
0
  #8
Sep 10th, 2007
Originally Posted by Paul.Esson View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

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

 
0
  #9
Sep 10th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

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

 
0
  #10
Sep 10th, 2007
Originally Posted by Paul.Esson View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2131 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC