• 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 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

Problem with using String Array index in a for loop inside a method

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello dear Coders,
in the following code:


I get the following error on the reference of the array index inside the for loop:
"The operator < is undefined for the argument type(s) int, String"

What is causing this error?
How can it be solved?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Room[i] is returning a String while you are trying to compare it with an int :



write this:

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I get the method to return type Room?
 
Aman Grover
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method will return type Room, since you have written return Room[i], you just need to change what I have written in my last post.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did and that part solved the problem.
But I still have the following problem on the return Room[i] part:
"Type mismatch: cannot convert from String to Room"
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because your Room variable (named exactly like class Room!!!) is of type String[] thus Room[i] is of type String.
And String is not a Room.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please forgive the naming of class and variable the same, it is like this in the assignment and I cannot change it.
Do I have to convert the String to a Room variable?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point of this class is to create an array of size 100 that contains the names of certain School rooms. My thinking was to iterate through array and compare it to String name that is given to this method.
Once it finds an equal name it returns that, else it returns null.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Von Valkenburgh wrote:Please forgive the naming of class and variable the same, it is like this in the assignment and I cannot change it.


Sorry, I can not believe any teacher would require you to use the same names for classes and variables. Maybe you misunderstood some instructions?

Dennis Von Valkenburgh wrote:Do I have to convert the String to a Room variable?


Probably. How should I know? What is your code supposed to do?
Please, read this: Strings Are Bad.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Von Valkenburgh wrote:The point of this class is to create an array of size 100 that contains the names of certain School rooms. My thinking was to iterate through array and compare it to String name that is given to this method.
Once it finds an equal name it returns that, else it returns null.


What is "that"? A room? A room name?
I would write a getName() method in Room class and compare this name to those array entries.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood the assignment. He wants us to create a class called "Room". And within this class is an array called "Room".
What confuses me is that he does not explicitly say the array should be String.
Yet the method getRoom receives a String that compares the to the array entries.
Rooms are in format such as "HS 252", thats why I assumed the array would be a String array.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

Dennis Von Valkenburgh wrote:The point of this class is to create an array of size 100 that contains the names of certain School rooms. My thinking was to iterate through array and compare it to String name that is given to this method.
Once it finds an equal name it returns that, else it returns null.


What is "that"? A room? A room name?
I would write a getName() method in Room class and compare this name to those array entries.


it returns a Room.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic