Basic Java Help

Discussion in 'Mac Programming' started by slack-jeenyus, Feb 14, 2008.

  1. macrumors newbie

    Joined:
    Feb 14, 2008
    #1
    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?
     
  2. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    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?
     
  3. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #3
    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);
     
  4. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #4
    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();
    
      } 
    }
    
     
  5. thread starter macrumors newbie

    Joined:
    Feb 14, 2008
    #5
    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.");}
     
  6. thread starter macrumors newbie

    Joined:
    Feb 14, 2008
    #6
    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.");}
     
  7. thread starter macrumors newbie

    Joined:
    Feb 14, 2008
    #7
    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
     
  8. macrumors regular

    Joined:
    Feb 1, 2008
    Location:
    Bucks, UK
    #8
    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.
     

Share This Page