This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

String vs. Boolean  RSS feed

 
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?

 
author
Sheriff
Posts: 23590
138
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?/**




 
Sheriff
Posts: 5135
138
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
 
Marshal
Posts: 60192
188
  • 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!