| | |
If else statements on compatibility, I'm so confused here?????
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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");
}
}
}
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");
}
}
}
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.
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
•
•
Join Date: Nov 2006
Posts: 47
Reputation:
Solved Threads: 1
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
http://s193.photobucket.com/albums/z...rrent=test.flv
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()
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()
•
•
•
•
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()
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
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, 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.
![]() |
Similar Threads
- Compatibility (Windows NT / 2000 / XP)
- Using a switch statement with nested if/else statements (C++)
- Sony ICD-R100 recorder in windows XP (Windows NT / 2000 / XP)
- please help me - extremely clueless :( (C++)
- have you ever abused ur powers? (Geeks' Lounge)
- oracle or sql server2000, which is best? (Oracle)
- Microsoft .NET FAQ (ASP.NET)
- I'm confused? How do I know which chipset to use? (Motherboards, CPUs and RAM)
Other Threads in the Java Forum
- Previous Thread: Else If statement
- Next Thread: How to separate a string ?
Views: 2131 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database developmenthelp draw eclipse encode error event exception file fractal game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number object online oracle print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing system template test textfields threads time title transfer tree tutorial-sample update windows working





