If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Sum of numbers

Hi,

First off I am new to java and I am doing some self-study.
I have wrote a program that asks the user to enter two numbers and displays the sum of these two numbers, it then gives the user a opportunity to have another go or to quit the program:

I have a couple of questions:
• How could I adapt the above program so the user would be given another go irrespective of whether or not an upper case or lower case ‘y’ is entered?
• How could I make it so instead of displaying the sum of the numbers, it asks the user to enter the sum. Then if it is correct a congratulations message is displayed and if incorrect then the correct answer is displayed.

when the user enters in their selection into your "choice" variable, you can use the String method:
equalsIgnoreCase(String anotherString) this will do what you asked.

so your while statement would become:

while(choice.equalsIgnoreCase("y"));

for your second question, just make another 3 variables called "sum", "guessString" and "guess", then after the second number is inputted, add the 2 numbers together into the "sum" variable.
Then ask the user for the appropriate input like so:

There are two types of variables in java, primitives and objects. Primitives simply represent a structureless value, whereas objects represent something with structure.

Dereferencing is what you do to objects, to refer to a particular method or variable within them. You can only do this to objects though, as it is referencing the internal structure of the object. It makes no sense to do this to a primitive, becuase it has no internal structure.

In your code the problem is the variable "choice" is a primitive (char), yet you are attempting to access the method "equalsIgnoreCase()". A char has no methods, so you can't do this.

When you are comparing primitives you use the == operator. In this case you should either compare the char you have to the char 'y' and the char 'Y' to see if it equals either of them using if(choice == 'y' || choice == 'Y'), or you should create a String object from it, and use the equalsIgnoreCase() method.