• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with if then else statement  RSS feed

 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am having an issue with my if/then/else statement for one of my methods. The method is supposed to take the user input of a character between 'A' and 'Z' (or 'a' and
'z') and return the character to the variable c. If a character other than 'A' thru 'Z' is submitted, it needs to inform the user and loop back. I think my only issue with my code is with the parameter of the if statement.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char are like int. You can check ascii value of the corresponding char and then do as required
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the ASCII Table, A is 65, Z is 90, a is 97, and z is 122.
Based on how I would do an if statement for an int, this is what I come up with (which is not correct)

Where am I going wrong?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I would use char literals (like 'a') instead of numbers.
Second, you need a character that is between 'a' and 'z' OR between 'A' and 'Z'. Your code says something different.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński, is this what you are saying?


And in my code (in my main method) I ask for a Character between A and Z, I just need to be case sensitive.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You replaced int literals with char literals correctly. Now you need to fix the other thing I mentioned.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have, I am still having an issue with my if statement in my getChar(min,max) method [line 28 of code]:

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The condition should be true if
c is equal or greater than 'a' and c is equal or less than 'z' OR c is equal or greater than 'A' and c is equal or less than 'Z'.

Now read your code (the if condition) out loud.

Also, your method getChar(char min, char max) takes two parameters but then ignores them.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying its supposed to be => & =< ?

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

You need:
c is equal or greater than 'a' and c is equal or less than 'z' OR c is equal or greater than 'A' and c is equal or less than 'Z'

Your code says:
c is equal or greater than 'a' and c is equal or less than 'z' AND c is equal or greater than 'A' and c is equal or less than 'Z'
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this is what you are saying

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With
I get an error still:

"bad operand types for binary operator '>='
first type: String
second type: char

bad operand types for binary operator '<='
first type: String
second type: char

bad operand types for binary operator '>='
first type: String
second type: char

bad operand types for binary operator '<='
first type: String
second type: char"
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means c is a String. you should declare c as a char.

By the way, c should be a local variable inside a method where you need it, not a static variable.
The same is true for the rest of your static variables.
You should always declare variables in the minimal possible scope.

You said that in your comments. If c is a variable c for the getChar() method let it be declared in the getChar() method.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help on that. Changing my variable, c, to a char, and my methods from String to char solved those problems.
But now I have a new problem. Line #4 and Line #8.
I use c.charAt(0) to take the first character if a String typed in (since I am requesting a char), but with this I am now getting errors.

Line 4's error is: char cannot be dereferenced
Line 8's error is: incompatible types: String cannot be converted to char

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Char is a primitive type. You can not execute any method on it.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have them as static variables because I am using each of them in two methods, a method with no parameters, and a method with parameters. Same variable. Am I not correct in keeping them static?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not correct keeping them declared as class variables. They should be declared inside methods.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if I have a variable that I need to use in two methods, as a local variable in one method, I cannot use that variable in both, correct?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The more fields you have in the class, the more opportunity there is for things to go wrong.
Consider passing the local variable as a parameter to the other method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!