Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

slack-jeenyus

macrumors newbie
Original poster
Feb 14, 2008
7
0
I am new to the forums here, everything I have need to find I can usually get from previous posts. This maybe a weird request but can anyone help me on my Java assignment? I would post the code here but it could be called "academic dishonesty". I am having some troubles with input checking and while/if/else statements. My "algorithm is" ex.: "Is the 1+1=2? True or False"

The answer is true. I need inputs: "t", "f", "true", "false" to pass the input check and anything else other than those inputs to prompt for another answer.

I was hoping I could get some help via msn or aim. Is there anyone willing to give me 10-15 minutes of their time?
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
I didn't understand the second part of your post (its late) but

Code:
public boolean checkInputMethod(int someParam) {
 boolean answer; 
 
 answer = (someParam == 1)  ? true : false;

 return answer;

}

public void main(String args[]) {
...
if (checkInputMethod(someResponse)) {
System.out.println("Answer was True");
else
System.out.println("Answer was False");
}


Is this along the lines of what your looking for?
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
never mind, just re-read it. Your just looking to validate the input?

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String strInput;

boolean done=false;
do {
System.out.print("Enter your answer: ");
strInput = in.readLine();

if ( (strInput.equalsIgnoreCase("t")) ||
(strInput.equalsIgnoreCase("true") ) {
done = true;
} else {
System.out.println("Bad Answer, Try again");
}
while(! done);
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
I think it might be something more along the lines of:

[edit: sorry, wrote this before your latest post]
Code:
// Send the question here
public boolean getAnswerToQuestion(String question)
{
  while (true) // You could limit this to a finite series of attempts 
  {
    //Assume getRespose() returns the user's answer to the question
    String userResponse = getResponse(question).toLowerCase();

    if (userResponse.equals("t") || userResponse.equals("true)) return true;

    if (userResponse.equals("f") || userResponse.equals("false)) return false;

    // Let this method below output an indicator of an improper reply
    indicateBadAnswer();

  } 
}
 

slack-jeenyus

macrumors newbie
Original poster
Feb 14, 2008
7
0
This is what I have. I thought it was going to work.

inputTrueFalse = JOptionPane.showInputDialog("True or False: Is the sky Blue?");

upperCase = (inputTrueFalse.toUpperCase());

//Check to see if input passes validation.
While (!upperCase.equals("T") & !upperCase.equals("F")
& !upperCase.equals("TRUE") & !upperCase.equals("FALSE"));{

JOptionPane.showMessageDialog("Please re-enter a valid input format.");
System.out.println("Please re-enter a valid input format.");
inputTrueFalse = JOptionPane.showInputDialog("True or False: Is the sky Blue?");
upperCase = (input.toUpperCase());}

if (inputTrueFalse.compareTo("T") ==0 || inputTrueFalse.compareTo("False") ==0){
JOptionPane.showMessageDialog(null, "Good job, you answered correct!");
System.out.println("Good job, you answered correct!");
score++;}

else{
JOptionPane.showMessageDialog(null, "I'm sorry you failed to answer correct.");
System.out.println("I'm sorry you failed to answer correct.");}
 

slack-jeenyus

macrumors newbie
Original poster
Feb 14, 2008
7
0
after switching some variables and cleaning it up.

inputTrueFalse = JOptionPane.showInputDialog("True or False: Is the sky Blue?");

upperCase = (inputTrueFalse.toUpperCase());

//Check to see if input passes validation.
While (!upperCase.equals("T") & !upperCase.equals("F")
& !upperCase.equals("TRUE") & !upperCase.equals("FALSE"));{

JOptionPane.showMessageDialog("Please re-enter a valid input format.");
System.out.println("Please re-enter a valid input format.");
inputTrueFalse = JOptionPane.showInputDialog("True or False: Is the sky Blue?");
upperCase = (inputTrueFalse.toUpperCase());}

if (upperCase.compareTo("T") ==0 || upperCase.compareTo("False") ==0){
JOptionPane.showMessageDialog(null, "Good job, you answered correct!");
System.out.println("Good job, you answered correct!");
score++;}

else{
JOptionPane.showMessageDialog(null, "I'm sorry you failed to answer correct.");
System.out.println("I'm sorry you failed to answer correct.");}
 

slack-jeenyus

macrumors newbie
Original poster
Feb 14, 2008
7
0
there seems to be an error here:


//Check to see if input passes validation.
While (!upperCase.equals("T") & !upperCase.equals("F")
& !upperCase.equals("TRUE") & !upperCase.equals("FALSE"));{

the compile error reads: 6 errors found:
File: /Users/KPenner/Documents/Java/A2Q2.java [line: 51]
Error: cannot find symbol
symbol : method While(boolean)
location: class A2Q2
 

phannon666

macrumors regular
Feb 1, 2008
185
0
Bucks, UK
Simple one there, "while" should be with a lower case 'w'. Otherwise its assuming you've created a method called "While", which in turn takes some boolean parameters with a semi-colon at the end, and then a random opening brace. That probably accounts for a large portion of those 6 errors. Change the W and it will interpret the rest correctly.

I'm curious though, what IDE are you using to have missed that? Usually the while would be coloured and so misspelling it would be obvious.

And in future please use CODE tags.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.