views:

377

answers:

5

These are my questions:

  1. I'm getting a couple of errors on the line "public static boolean validNumCheck(String num){" - "illegal start of expression", "';' expected", and "')' expected".

  2. How can I give the user 3 tries in total for each number? I believe right now the programme asks the user for 3 numbers and gives them 3 tries in total to get the numbers correct (My explanations suck... read the code to get a better idea of what I mean).

This is my code:

import javax.swing.JOptionPane;

public class Assignment3 {
    public static void main (String[] args){
        final int MAX_TRIES = 3;
        int[] attempts= new int[2];
        String[] getNumber= new String [2];

        //Ask the user for 3 integers.
        while(attempts[0]<MAX_TRIES){
            getNumber[0]= JOptionPane.showInputDialog(null,"Please enter an integer between 0-200.");

            //Pass the value to validNumChek
            validNumCheck (getNumber);

            //If it is not a valid number give the user 3 more tries.
            if (getNumber== false){
                while(attempts[1]<MAX_TRIES){
                    getNumber[1]= JOptionPane.showInputDialog(null,"Please enter an integer between 0-200.");
                    attempts[1]++;}
                }
            attempts[0]++;
        }

    //Parse the string to an integer and check if it is a valid number.
    public static boolean validNumCheck(String num){
        int number;

        try {
            number = Integer.parseInt(num);
            return number >= 0 && number <= 200;                
        }catch(NumberFormatException e){
            //If it is not a valid number return false.
            return false;
        }
    }
}
+1  A: 

In the method signature (that would be "public static int validNumCheck(num1,num2,num3)"), you have to declare the types of the formal parameters. "public static int validNumCheck(int num1, int num2, int num3)" should do the trick.

However, a better design would be to make validNumCheck take only one parameter, and then you would call it with each of the three numbers.


My next suggestion (having seen your updated code) is that you get a decent IDE. I just loaded it up in NetBeans and found a number of errors. In particular, the "illegal start of expression" is because you forgot a } in the while loop, which an IDE would have flagged immediately. After you get past "Hello world", Notepad just doesn't cut it anymore.

I'm not going to list the corrections for every error, but keep in mind that int[], int, String[], and String are all different. You seem to be using them interchangeably (probably due to the amount of changes you've done to your code). Again, an IDE would flag all of these problems.


Responding to your newest code (revision 12): You're getting closer. You seem to have used MAX_TRIES for two distinct purposes: the three numbers to enter, and the three chances for each number. While these two numbers are the same, it's better not to use the same constant for both. NUM_INPUTS and MAX_TRIES are what I would call them.

And you still haven't added the missing } for the while loop.

The next thing to do after fixing those would be to look at if (getNumber == false). getNumber is a String[], so this comparison is illegal. You should be getting the return value of validNumCheck into a variable, like:

boolean isValidNum = validNumCheck(getNumber[0]);

And also, there's no reason for getNumber to be an array. You only need one String at a time, right?

Michael Myers
+1  A: 

Every parameter in a method definition needs a type:

public static int validNumCheck(int num1, int num2, int num3){

but I wonder why you pass all three numbers in, when you only check one at the time. And you want to return if the number is true or not, so the return value should be an boolean:

public static boolean validNumCheck(int num){
    // test and return true or false

Also, if the user enters "abc", you will get an exception from the "Intger.pareInt(String)" method. Maybe you want to store the entered text as a String an give it to validNumCheck, try to convert it and check if it is between 0 and 200.

public static boolean isValidNumber(String num){
try {
    int number = Integer.parseInt(num);
    return number >= 0 && number <= 200;  
}catch(NumberFormatException e){
    return false;
}
}

EDIT 1: For the three tries, you need a loop, that runs till the number is valid or three tries are taken. Simply count the tries with in int.

By the way, are you sure you have to use JOptionPane and JMessageDialog? This is GUI stuff and just complicates that hole thing. You also could read and write text to the console by using System.out and System.in

EDIT 2: One more tip, when you create an int array with a given length, every place in the array is filled with 0, so you could write:

int[] count= new int [3];

and don't need that:

count[0]=0;
count[1]=0;
count[2]=0;

And if you want to use other values than 0, you could use a shorter form like this:

int[] count = {1, 5, 2}

EDIT 3: One thing you should do/learn is this: don't write the whole thing and end up with many errors and a not running program. Code a little, test a little. Do one part of it, see it run and you are happy.

Did this thing ever run? I mean, did you already saw that JOptionPane-InputDialog you try to use? If not, do only this first: (and run it!)

public class Assignment3 {

    public static void main (String[] args){

        int[] numbers = new int[3];

        for (int i = 0; i < numbers.length; i++) {
         JOptionPane.showInputDialog("enter something");
        }
    }
}

Got it to run? Great! Now, try to wrap that line with the JOptionPane in a loop, that runs till the user entered a valid number (using the checkValidNum method you already got) or used his three attempts.

Tim Büthe
Yes, I have to use JOptionPane.
에이바
Thanks for the info about the array, I wasn't aware that it started at 0 by default.
에이바
+1  A: 

This thread may be helpful. Those are hints to pass from "problem description" to "actual coding"

As per your specific questions:

How can I give the user three chances to enter a valid number for EACH number? (I don't want the 'answer' just some ideas/hints.

How would you do that if you were a cashier ( with bad memory ) in real life? And I mean very very bad memory, so much that you cannot remember if you're rejecting a customer for first time or second?

I would write it down in a piece of paper, and each time I reject the customer I add a "|"

"| | |"

When the paper has three lines in it I would call the police. .. :)

That's similar here. If you don't have some thing that may vary you will get in a infinite loop ;)

Is validNumCheck suppose to return a value AND THEN exit in the void main method

No

...or can I have it just exit the programme in that main method?

Yes.

OscarRyz
All right, I'm reading that thread now, thanks.
에이바
+1  A: 

When you count ( in real life ) you don't need to store the numbers in boxes, you just have to change the value of the count and have a limit.

For instance you can replace this:

        int[] count= new int[2];

With this:

        int attemtps = 0 ; // or count 
        int maxTries = 3;

You cannot assign an array of integers to an arrays of Strings.

       String[] getNumber= new int [2];

Using an array of Strings may work, but you have to assign an array of strings

       String[] numbers = new String[2];

Or you can use and array of int:

       int [] numbers = new int [2];

Pick one.

Remember arrays are like boxes where you can store values.

OscarRyz
Thanks Oscar. If I assign maxTries to 3 would it be better to usesomething like final int MAX_TRIES= 3; ?
에이바
@unknown: Yes. You could even pull it out of main() and make it private static final int MAX_TRIES = 3; if you want.
Michael Myers
Mmyers, why would I do that? (I don't meant to be sarcastic if it seems that way). How is that better/more effective?
에이바
@unknown: Yeap, mmyers is correct, but I would say at this point is not so important. You should probably attempt to have the pseudo code first and then deal how would it be programmed.
OscarRyz
@unknow: It's irrelevant. But the reason is because that's how constants values are declared in java. If yours were to be a larger program, it would be easier to adjust the attempts number in one place.
OscarRyz
I really don't understand how I would implement MAX_TRIES=3; to make sure if the user gets three tries... :/
에이바
@unknown: What Oscar said was to have a variable "int attempts = 0;". You can then increment this variable every time through the loop and check if it reaches MAX_TRIES.
Michael Myers
@unknown: It is very important to have the steps needed to solve the problem BEFORE going into code. You could do that as an algorithm or a s pseudo code. I expanded in a new answer below.
OscarRyz
+2  A: 
OscarRyz
Hey, thanks for your help. I'll be back around 9PM EST. I'll definitely read this.
에이바
hehehe ... Good!!.. we all have been here at some point. ;)
OscarRyz