PDA

View Full Version : Basic Java Help




slack-jeenyus
Feb 14, 2008, 10:04 PM
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
Feb 14, 2008, 10:41 PM
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
Feb 14, 2008, 10:48 PM
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
Feb 14, 2008, 10:55 PM
I think it might be something more along the lines of:

[edit: sorry, wrote this before your latest post]

// 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
Feb 14, 2008, 10:58 PM
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
Feb 14, 2008, 11:13 PM
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
Feb 14, 2008, 11:17 PM
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
Feb 15, 2008, 02:48 AM
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.