Hello everyone,

I am making a Fortune Teller program for a University project, and I am a little confused as to why I keep getting this error when i try to compile. Here is my code:

public class Fortune
{
private int insertNumber;
private int showFortune;


/**
* Constructor for objects of class Fortune.
*/
public Fortune()
{
insertNumber = 0;
}


/**
* User inserts a number and a fortune is told.
*
*/
public int insertNumber(int number)
{
if(number < 1){
System.out.print("Please insert a number between 1 and 4");
}


if(number > 1){
System.out.print("You will become rich");
System.out.print("You will meet your evil twin in 2 days");
System.out.print("Look out for the number 8. It means danger");
System.out.print("You will lose your memory after today!");
}


if(number < 4){
System.out.print("You will become rich");
System.out.print("You will meet your evil twin in 2 days");
System.out.print("Look out for the number 8. It means danger");
System.out.print("You will lose your memory after today!");
}


if(number > 4){
System.out.print("Please enter a number between 1 and 4");
}
}  -- ERROR IS HERE -- It highlights this bracket.
}

Could you please help me?

Also, if there are too many brackets at the end, i take one away but i get another error message saying 'reached end of file while parsing' that highlights the LAST bracket. I am so confused!

Thanks everyone!
T.1024

Recommended Answers

All 12 Replies

The message is pretty clear about what's wrong. You say that insertNumber returns an integer value, but you fail to return a value with a return statement:

return 0; // Or some other number

Ah i see, thank you. It works fine now. I am new to Java language and so i didnt know what to do.

Thanks again!
T.1024

Ah I have another problem now. I am sorry for any inconvenience but my slight lack of java knowledge is handicapping me! I have compiled my code, but now whenever i enter a number into my 'Fortune Teller' machine, it keeps adding to the fortunes printed before. Is there a way to delete its memory once the system print window has been closed?

Here is my new code:

public class Fortune
{
    private int insertNumber;
    private int showFortune;
    private int number;

    /**
     * Constructor for objects of class Fortune.
     */
    public Fortune()
    {
        insertNumber = 0;
        showFortune = 0;
        number = 0;
    }

    /**
     * User inserts a number and a fortune is told.
     * 
     */
    public void insertNumber(int number)
    {
       if(number < 1){
           System.out.print("Please insert a number between 1 and 4.");
        }
       if(number > 1){
           System.out.print("You will become rich.");
           System.out.print("You will meet your evil twin in 2 days.");
           System.out.print("Look out for the number 8. It means danger.");
           System.out.print("You will lose your memory after today!.");
        }

        if(number > 4){
            System.out.print("Please enter a number between 1 and 4.");
        }
    }
}

Thank you again
T.1024

start quote:

   if(number > 1){
       System.out.print("You will become rich.");
       System.out.print("You will meet your evil twin in 2 days.");
       System.out.print("Look out for the number 8. It means danger.");
       System.out.print("You will lose your memory after today!.");
    }

end quote.

This statement reads "for any number entered that is greater than one, do all of this in order." I don't believe that is what you are trying to achieve here.

Also if number = 1, then nothing will happen in your code.

And if the number is greater than 4, the above statement will execute as well as the code following it! You need an if-else statement rather than a series of if-statements.

I know, i am all confused, hehe. This is what i am trying to achieve:

- If 0 is entered, say "need to enter more", etc.
- If 1, 2, 3 or 4 is entered, show any fortune out of the 4.
- If anything above 4 is entered, say "need to enter less", for example.

I am pulling my hair out lol.

Thanks,
T.1024

So your basic algorithm is:

if (number < 1) {
  // print out "enter more"
} else if (number <= 4) {
  // print one of the fortunes
} else {
  // print out "enter less"
}

You have the first and last section done, it's just a matter of working out how to choose a particular fortune. Do you want the number 1 to correspond to the first fortune, 2 to the second etc or do you want it to be random?

Yes I would like the responses to be random. Thanks ever so much for your help, guys.

T.1024

Place the responses in a String[] array. Select a random index within that array with a Random.nextInt() call.

In fact, it would be easier to make them not random, but how would i do this? So 1 would correspond to the first fortune..etc

Thanks :)

You can keep it in the Fortune class. I was suggesting a plain String[] array, not ArrayList though. Since you have a fixed number of hard-coded phrases, you really don't need an ArrayList.

Well, it's fairly simple if you think about it. Your array indexes run from 0 to n-1, where n is the number of entries. If you read the API doc for the Random.nextInt(int) method, you should see how it would be easy to randomly select an index from that String array.

Edit: Ok, this post is now a bit out of order after your edit.

Yeah sorry about that edit, i changed my mind a second later, heh. Shouldve written 'Edit' really. Ok thank you for that info, i should be able to implement a string Array without too much trouble now. If i have any trouble i hope you dont mind me posting back here (again!)

Thanks,
T.1024

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.