• Post Reply Bookmark Topic Watch Topic
  • New Topic

compare a list array and a string  RSS feed

 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,

Just a quick question, ive delcared and initialised a list array to contain the numbers 1 - 100 as follows;



i think so far so good, now all i want to do is compare the list to a string which may contain a number btween 1 & 100. If yes print out has number else no number.



Now i know the above is wrong, just giving an idea of what i want, how would i compare the string to the list of numbers?

Thanks
Mark
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How wrong do you think it is? It might be closer than you think.

Does it compile? Does it give wrong answers?
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

just ran this code



it ran but said it did not contain a number, when num actually contains 1.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your list called "number" (wouldn't it be more clear if you would call it "numbers"?) now contains Integer objects.

(You're putting ints into it in the loop, but Java converts those into Integer objects by autoboxing, because a List can only contain objects and an int is a primitive; not an object).

So now you have a String that contains the number, and you want to check if there is an Integer object in your list that represents the same number as what you have in the string.

One way you could do this is convert the String to an Integer, and using that Integer to check if the number appears in the list. You can convert a String to an Integer like this:

Now you can use contains(...) to see if x is in the list.

Note 1: If the string contains text that cannot be converted to a number, Integer.valueOf(...) will throw a NumberFormatException. You may want to deal with that in your code.

Note 2: Your list contains the integers 0 to 99, so 100 will not be in the list.
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for that,

i did this


but it says it contains a number but displays the numbers as -1 no matter what number i stick in. Not sure why it shows -1

ill put in try and catch's once i get this part working.

is there a way to test if a string contains a integer at all?
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry noticed the mistake now in the indexOf(num)
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys got it working,

Hey if anyone knows of a methid to test if a string contains an integer at all could ya leave me know thanks.

Mark
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See "Note 1" in my post above.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!