This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

else/if problem

 
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem with getting the wrong output in this program. Here is the code:

It compiles, but always prints out zsa-zsa every time.
 
pollinator
Posts: 1195
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to use single quotes around your character constants instead of double quotes.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

You don't compare Strings using ==; you use the equals() method.

if (firstLetter.equals("e")) ...

The "==" operator asks if two objects are physically the same bit of memory; the equals() methods asks if two objects are "the same", which for Strings, means they have the same characters in the same order.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ryan McGuire:
You want to use single quotes around your character constants instead of double quotes.



Nope -- then this wouldn't even compile! They're (correctly) Strings, not character constants.
 
Preston Kruckow
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright I did that, but I'm still getting 25 errors when I compile it. Here is the revised code:

Currently 25 errors...
 
Preston Kruckow
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, I did what you said, but now the compiler is complaining that variable first might not have been initialized.
 
pioneer
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, Preston (a friend in my AP Comp Sci A class) got it to work. He just initialized first to "adadada" and it started working correctly. Thanks for your help everyone!
 
Ryan McGuire
pollinator
Posts: 1195
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:

Nope -- then this wouldn't even compile! They're (correctly) Strings, not character constants.



Oops, I didn't even look back at the type of firstLetter. I had just assumed from the if/else if's that it was a single character. That's what I get for responding BC (before coffee).
 
What's a year in metric? Do you know this metric stuff tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!