• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • 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?
 
Aman Grover
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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"
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!