• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

is this the way to convert from lower to upper case?  RSS feed

 
Ranch Hand
Posts: 177
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


does it have to captured in another variable?

i tried this:

but it says its ignored


 
wayne brandon
Ranch Hand
Posts: 177
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i know it should be
userAnswerUpper =
 
Ranch Hand
Posts: 1327
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you place the previous code, please? What is assigned to the userAnswer variable in the first place?
if you share the previous code I will try myself.
 
wayne brandon
Ranch Hand
Posts: 177
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Angus heres the whole thing im busy doing, please feel free to tell me if i can make it more effeicient etc. variable names too, everything please

 
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:

does it have to captured in another variable?

i tried this:

but it says its ignored




You could instead use method chaining and get it all in 1 statement

 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:


Well that looks very odd for a start. Not the assignment - which is fine - but the intent - which is not.

wayne brandon wrote:but it says its ignored


What does?

Winston

 
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston

wayne brandon wrote:sorry i know it should be
userAnswerUpper =

 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP:

The answer has to do with the fact that a String in Java is immutable. That is, answer.toUpperCase() returns a new String object rather than change the String that answer references to all uppercase letters. If you don't assign the result to another variable like uppercaseAnswer, then you're essentially throwing away the uppercase String object without using it.

 
wayne brandon
Ranch Hand
Posts: 177
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String userAnswer = sc.next().toUpperCase();

love it Ansamana thanks!!!
 
Saloon Keeper
Posts: 10136
214
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Drop the habit of making declarations public by default. Make classes and methods package private until you're certain they're going to be used in other packages.
  • Make your classes final, unless you have a REALLY good reason to extend them, and have designed them to be extended. This is not a trivial task.
  • You don't have to initialize your answer variable when you declare it, because the initial value is never used.
  • Why are the strings that you print enclosed in an extra pair of parentheses?
  • Take the effort to use variable names that make the code read fluently. Instead of answer, which implies that the variable holds the actual answer, use something like 'isAnswerValid'.
  • In this particular case it doesn't really matter, but in general avoid the toUpperCase() and toLowerCase() methods. Instead, use the overloads that take a Locale.

  • A common bug in many professional codebases is that they assume programs are going to be executed on a system with latin encoding. For instance, if one of the answers that the user could have given was the letter 'i', your code would have failed if you checked for the uppercase letter 'I' and the program was executed on a Turkish machine, because in Turkish, "i".toUpperCase() returns "İ".

    If your application is not internationalized and you use hard-coded strings in a particular language, then also perform string operations using the same Locale:
     
    Marshal
    Posts: 64172
    215
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    wayne brandon wrote:sorry i know it should be
    userAnswerUpper =

    But it's too late now; you will never be allowed to forget that

    I suggest you change your boolean to be called needsAnswer or similar; then you can say do { ... } while (needsAnswer); That is easier to read without the bang sign (! = not operator). You will notice my suggestion is rather different from Stephan's. Design your loop so you don't use try System#exit, which is hazardous in some circumstances.
    Why are you using nextLine() rather than next()?
     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    very interesting stephen thanks so much

    Why are the strings that you print enclosed in an extra pair of parentheses? was just an accident i made in the ide i know you only need 1

    do you always by default make classes and methods private? but in a program with one class you cant make it private.
     
    Campbell Ritchie
    Marshal
    Posts: 64172
    215
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As a general rule, give everything the most restrictive access you can get away with. You can always, if necessary, change something protected to public, or similar, but if you make the opposite change you can break code a long way away.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 10136
    214
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I give classes default access, unless I need to use them in a different package.

    I give methods private access, unless I need to use them in a different class. Then I give them default access, unless I need to use them in a different package.

    I always give fields private access. Period.

     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Campbell "I suggest you change your boolean to be called needsAnswer or similar; then you can say do { ... } while (needsAnswer); That is easier to read without the bang sign (! = not operator). You will notice my suggestion is rather different from Stephan's. Design your loop so you don't use try System#exit, which is hazardous in some circumstances.
    Why are you using nextLine() rather than next()?"

    i agree with the boolean name. and have changed it.
    sorry im confused
    "do { ... } while (needsAnswer); That is easier to read without the bang sign (! = not operator)"

    so should needsAnswer be set to true and then the while (needsAnswer) and then the condition changed to false in the loop?

    because i could have sworn you told me to keep the boolean false in the while and change it to true in the loop

    sorryi just dont get the right way to do this.
     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i thought it was always better to use nextLine as i had a problem with scanner carrying things over to other methods called when using next()
     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Whats the safest way to terminate a program?
     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    did it as a while loop too

     
    Campbell Ritchie
    Marshal
    Posts: 64172
    215
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let the loop complete, then let control pass to the end of the method.If you need an explanation of lines 7‑8, do ask. When you get to the end of the main() method in similar fashion, the whole app will close down.This is naughty having two statements in the main() method, but it only exists to show the possibilities. If you move the save call to the very end of the run() method, that problem will disappear.

    [edit]Correct spelling of If.
     
    Campbell Ritchie
    Marshal
    Posts: 64172
    215
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What I said only applies to singly‑threaded programming.
     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok thanks for that.

    can you please just tell me a little more about true false and ! true or ! false when it comes to while / do while loops im still unclear on when to set something to start as false (an entire block) or true

    does it just depend on what variable names you have and what the program is doing? sorry has been unclear to me for some time now. i always seem to use ! in the wrong place.

     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    like this, just making a simple menu and will use a switch



    you probarbly not going to like


    in which case is this the better option



    of course an exit will be in the loop

    to me this doesnt read well
    optionSelected = true
    while optionSelected is true

    wouldnt
    optionSelected =false (as an option hasnt been selected)

    and while its false
    (!optionSelected)

    do this

     
    Junilu Lacar
    Marshal
    Posts: 13447
    222
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's all about choosing a name, isn't it. Don't get hung up on the "option selected" idea. If you have to use ! with that, then find a different idea.
     
    wayne brandon
    Ranch Hand
    Posts: 177
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    right i thought as much

    so as it would read in english

    hopingForMillionDollars = true
    while (hopingForMillionDollars)

    etc, so you should always avoid ! in your while condition

    thanks so much
     
    Campbell Ritchie
    Marshal
    Posts: 64172
    215
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    wayne brandon wrote:. . . you should always avoid ! in your while condition . . .

    I think it is a bit of an exaggeration to say you should always avoid bang symbols (!) in a loop continuation condition. Say it is usually better to avoid bang symbols because the code is so much easier to read.
     
    His brain is the size of a cherry pit! About the size of this ad:
    Create Edit Print & Convert PDF Using Free API with Java
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!