• Post Reply Bookmark Topic Watch Topic
  • New Topic

String vs. Boolean  RSS feed

 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my method I'm using a String and I'd like to use &&, like it's Boolean.....Is there a way?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think that you meant to use the equality comparison operator and not the assignment operator. The result of the first is a boolean, while the result of the second is a string.

Henry
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
I think that you meant to use the equality comparison operator and not the assignment operator. The result of the first is a boolean, while the result of the second is a string.

Henry


Im getting this error:

        else if(residency = "N" && sumCredits >= 12 && sumCredits <= 18)
                                 ^
Program4(2).java:110: error: bad operand types for binary operator '&&'
         else if(residency = "N" && sumCredits >= 12 && sumCredits <= 18)
                                 ^
  first type:  String
  second type: boolean

For:
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
I think that you meant to use the equality comparison operator and not the assignment operator. The result of the first is a boolean, while the result of the second is a string.

Henry


Ok I got it to compile, but the calculation produces $0 for each input....I'll clean up the design problems before I turn it in....any ideas?/**




 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use the == operator with Strings, use equals() instead:
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Don't use the == operator with Strings, use equals() instead:


I will try this first thing in the morning, thanks
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have cleaned up the worst of your formatting; you had lines too long and too many empty lines, which actually detract from legibility. I have sorted out some of those things; look at lines 36‑41 to see how it shou‍ld be done.
It is
public static void main(String[] args) ...
As you have already been told, don't add the additional spaces.
public static void main (String [ ] args) ...
You also have some inconsistent indentation, which I haven't the time to correct, possibly caused by your mixing tabs and spaces. Despite what the old Sun style guide says, don't mix spaces and tabs. Use a decent text editor (my favourite on Windows is Notepad++) and set up automatic indentation and automatic conversion of 1 tab → 4 spaces. I might sound all anally‑retentive about formatting problems, but you need to be able to read your own code easily. If you don't format it correctly and consistently, you will overlook serious errors because you are misreading your own work.

I shall tell you the old‑fashioned method of debugging. Start by putting a print instruction where you read N/P/I:-
System.out.printf("You entered %s%n", input);
and a similar instruction one line before “return tuition;”:-
System.out.printf("Tuition = %.2f%n", tuition);
If those two don't show where the problem lies, write a similar line isnide each if statement in the method to calculate tuition.

Why have you used the keyword static so often?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!