aspose file tools*
The moose likes Beginning Java and the fly likes How to compare String and int? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

How to compare String and int?

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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:



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38780
    
  23
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

Joined: Jun 28, 2008
Posts: 99
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

Joined: Jun 28, 2008
Posts: 99
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
Sheriff

Joined: Oct 13, 2005
Posts: 38780
    
  23
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

    Joined: Jun 28, 2008
    Posts: 99
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38780
        
      23
    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

    Joined: Jun 28, 2008
    Posts: 99
    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!
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18825
        
      40

    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


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Amit Ghorpade
    Bartender

    Joined: Jun 06, 2007
    Posts: 2716
        
        6

    (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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38780
        
      23
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38780
        
      23
    Originally posted by myself:
    . . . case = "oops";


    Well, that line deserves an "oops", doesn't it!
    Ls chin
    Ranch Hand

    Joined: Jun 28, 2008
    Posts: 99
    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

    Joined: Jun 28, 2008
    Posts: 99
    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

    Joined: Jun 28, 2008
    Posts: 99
    I think Garrett has provided the pseudocode in post #3!

    Originally posted by Garrett Rowe:


    Thank you all!
    kenken wala
    Greenhorn

    Joined: Aug 15, 2009
    Posts: 1
    how to convert 0-999 in string
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14144
        
      18

    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)


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to compare String and int?