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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to compare String and int?

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

How do we write a code that does this:

a) if the user key-in any number from 0 to 100, it will loop and perform some task.
b) if the user key-in any number less than 0 or more than 100, it will prompt the user to re-enter the correct number.
c) if the user type-in "quit", it will exit the loop and terminate the program.

I know how to check the numbers.



But how do I use the same 'input' variable as a String to check if it is equal to "quit" or not?


The 'input' variable can only be an 'int' or a 'String', right? So, how do we use the same variable to compare 2 different things?

Or do we need to use a different loop and ask the user "Do you want to continue, y or n? after every loop?

Is it possible to keep looping until the user key-in "quit", without asking the same question over and over again?

Thanks for your replies.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Is it possible to keep looping until the user key-in "quit", without asking the same question over and over again?


Yes, and the while loop which compares the input with "quit", should do the task.


The 'input' variable can only be an 'int' or a 'String', right? So, how do we use the same variable to compare 2 different things?


Keep the input variable a String and parse it into an int.But be advised, since you will be using "quit", the parsing will throw an exception.You need to take care of this.

Hope this helps
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
First the input variable has to be either an int or a String. In Java a reference can only be a single type, and it cannot change its type. So what you can do is:

 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Amit, Garrett,

Thank you for your replies.
I've changed my code and use Integer.parseInt() but the code is still incorrect. I don't know how to get rid of the method that asks "do you want to continue?" after every line. How do I make it loop automatically?

Furthermore, when I press "N", it will print "end" many times. I think it's repeating the entire loop again. I can't use the 'break'; statement, it gives me an error which says "the break is outside the loop".

Any idea how to rectify it?





Thank you.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Somebody else had a rather similar problem back in the Spring; maybe her thread will help: here.

But it is particularly awkward trying to check whether a String is a number or "quit" in the same statement.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Campbell,

Originally posted by Campbell Ritchie:
Somebody else had a rather similar problem back in the Spring; maybe her thread will help: here.

But it is particularly awkward trying to check whether a String is a number or "quit" in the same statement.


Thanks for the link to the thread. It was very helpful. There are many while loops in her method and the method gets quite big. I was thinking of a more OO approach. I want to make my methods smaller.

The method for checking invalid number is working fine. But when it comes to checking for Y or N - it is giving me a "NullPointerException" error. Any idea why? I think, I know, it's because it's asking for a number input again and I don't have it. But how do I bypass that?


It's a simple program but I just can't figure it out yet. Need some pointers in the right direction, please.
Thank you!
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi all,

I got my code to work. It's weird, it's messy, it's the least robust code ever, and I by-pass the "InputMismatchException" by catching it, but hey it's working!! (working means it's behaving the way I wanted it to).

Any suggestions how I can make it neater and better?

Thanks for your suggestions.



Thanks.

edit: small error.
[ September 22, 2008: Message edited by: LS chin ]
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Suggested enhancements, nothing to do with the input:

  • Alter the set of ifs to dispense with the <= tests.
  • Work out why while (input3 > 0 | input3 <= 100) is of no value, and what the logicians call that sort of construction.
  • Find out why you ought to use || and && rather than | and &.
  • Reduce the bank of ifs to a switch with not more than 11 cases.
  • [edit]Add: all those points have to do with the doComparison method or the old doCompare method.[/edit]
    [ September 22, 2008: Message edited by: Campbell Ritchie ]
     
    Ls chin
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hi Campbell,
    Thank you for your suggestions. I really appreciate it.

    Originally posted by Campbell Ritchie:
    Suggested enhancements, nothing to do with the input:

  • Alter the set of ifs to dispense with the <= tests.

  • I managed to do the ifs using <= but is >= a better design pattern?

    Originally posted by Campbell Ritchie:

  • Work out why while (input3 > 0 | input3 <= 100) is of no value, and what the logicians call that sort of construction.

  • I don't understand, why is it of no value? How do we test if input is in the range of 0 to 100? In maths, it is (0 < input < 100). How do we write this in programming format?

    Originally posted by Campbell Ritchie:

  • [list]Find out why you ought to use || and && rather than | and &.
  • Reduce the bank of ifs to a switch with not more than 11 cases.

  • It is better to use && and || because they are short-circuit operators, they are more efficient. Right?

    Originally posted by Campbell Ritchie:
    all those points have to do with the doComparison method or the old doCompare method.


    I've reformatted my methods using <= and also using switch and case. The case one is not displaying the grade at all. /sigh/


    The code above does not display the grade.


    This code is okay.

    Why the switch one is not displaying the grade?

    Thank you.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Two challenges completed successfully

    You can eliminate the <= test with else if instead of it. Correct.
    || and && are short-circuit operators, giving slightly better performance than | and &. Correct. You can sometimes improve this enhancement if you can put something likely to be false before a && or something likely to be true before a ||.

    Using a switch? Why is (input3 > 0 | input3 <= 100) valueless? Back to the drawing board for those two. I'll look tomorrow morning and see how you have got on.
     
    Ls chin
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hi Campbell,

    Originally posted by Campbell Ritchie:
    Using a switch?


    I think in this case, switch won't work. Switch is only for options like 1, 2, or 3. And we choose which option we want to execute. Right?


    Originally posted by Campbell Ritchie:
    Why is (input3 > 0 | input3 <= 100) valueless? Back to the drawing board for those two. I'll look tomorrow morning and see how you have got on.


    This, I have absolutely no idea. I don't even know how to google it. What's the keyword?

    Need your help, please.

    Thank you!
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    I think it may be a good idea to do a reset -- and answer the original question. I am also having a difficult time following the current direction.

    Take a look at the Integer class. With this class it is possible to either convert a String to an int, or an int to a String. Once converted, you can compare String to String, or int to int.

    And once you are able to compare ints and Strings, maybe you can apply that to the problem. Or at least, get to the next issue.

    Henry
     
    Amit Ghorpade
    Bartender
    Posts: 2856
    10
    Firefox Browser Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    (input3 > 0 | input3 <= 100)


    Her I think you need the (input3 > 0 && input3 <= 100). The logical operator and(&&). As Campbell said, bitwise operators add performance unless really needed.Also it will not give the desired result every time since it is associated with the bits rather than the value.

    The case one is not displaying the grade at all.


    It is never evaluated to true!!
    Just take a look at the switch, you switch on input, then there are cases 1 to 5. Then in each case there is a if checking the input value.
    If the input is say 50, then none of the cases match.For a input 1, case 1 has a match, but the if fails.

    I dont think switch is the solution, you should consider using if-else if construct.


    Hope this helps
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Lateral thinking, please. It is really easy

    But it only works when the marks go in 10s!

    And Amit Ghorpade has given you hints about (input3 > 0 | input3 <= 100).

    Henry's suggestion about converting Strings to ints or vice versa looks useful; there are methods in the String class which allow you to compare Strings. Then you need to work out what you are comparing the String to.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Originally posted by myself:
    . . . case = "oops";



    Well, that line deserves an "oops", doesn't it!
     
    Ls chin
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hi Amit,
    Thank you!

    Originally posted by Amit Ghorpade:
    It is never evaluated to true!!
    Just take a look at the switch, you switch on input, then there are cases 1 to 5. Then in each case there is a if checking the input value.
    If the input is say 50, then none of the cases match.For a input 1, case 1 has a match, but the if fails.

    I dont think switch is the solution, you should consider using if-else if construct. Hope this helps


    Yes, I've forgotten that the switch was actually checking the 1, 2, and 3, which are the actual input value. So I would need like 100 cases! The if-else construct is much better, yup.

    Thanks a lot!
     
    Ls chin
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hi Campbell, Henry,

    Originally posted by Campbell Ritchie:
    Henry's suggestion about converting Strings to ints or vice versa looks useful; there are methods in the String class which allow you to compare Strings. Then you need to work out what you are comparing the String to.


    Okay ~. So, everytime the user input something, the program will check if it is a String or a number, right? Something like this?

    String input;
    if input equals to String "n"
    exit the program
    if input is not "n" or any other alphabets
    convert the String to int
    int a = Integer.parseInt(input);
    do comparison and display the grade

    How would the program know it's not a alphabet and not throw a InputMismatchException? Hmm, I have to think about it for a day or two.

    Right now I'm just happy that the while loop and switch works!

    Thanks again.
     
    Ls chin
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    I think Garrett has provided the pseudocode in post #3!

    Originally posted by Garrett Rowe:



    Thank you all!
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    how to convert 0-999 in string
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    kenken wala, welcome to JavaRanch.

    Please start your own new topic, instead of appending your question to another (old) topic. I'm closing this one.

    Jesper Young (moderator)
     
    Consider Paul's rocket mass heater.
      Bookmark Topic Watch Topic
    • New Topic