• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with loop and arraylist  RSS feed

 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello my friends!
Trying to make a list of cities and their weather.
I´m using this while loop but cant find a nice why to brake it. As it is now the "Q" Im using to break the loop is stored as well in the array.
And therefore I have the remove line right after which dont feel as the best way to do it.
Also need a way to store an integer for the temperature in every city but Im not alowed to use hashmaps.

 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should know not to use the equality operator on reference types.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should now not to use ..


Of course. :-) Working the nightshift this week ;-) anyway any tips or clue tp put me in the right way?
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That "if" statement within while loop body seems redundant to me.
You're checking that within while loop condition, just in a wrong way, as it was pointed out already.
Why you all the time adding to 0 position? Wouldn't do-while loop look nicer in there?
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:That "if" statement within while loop body seems redundant to me.
You're checking that within while loop condition, just in a wrong way, as it was pointed out already.
Why you all the time adding to 0 position? Wouldn't do-while loop look nicer in there?


Yes of course was posting the wrong unedited code..

This is what it should look like and my question still remains...
public class WheaterStation {

public static void main(String[] args) {

ArrayList<String> city = new ArrayList<String>();

Scanner sc = new Scanner(System.in);

System.out.println("Add a city");

int i = 0;
String s = "";
while (!s.equals("q")){
s = sc.nextLine();

city.add(s);
i++;
}
city.remove("q");
for (String x : city)
System.out.println(x);
}
}


"Trying to make a list of cities with added weather in form of (x) degreese celsius. A list of cities. The scan is goiing to ask for name(city) and the wheather(degreese) of that city.
I´m using this while loop but cant find a nice why to brake it. As it is now the "Q" Im using to break the loop is stored as well in the array.
And therefore I have the remove line right after which dont feel as the best way to do it.
Also need a way to store an integer for the temperature in every city but Im not alowed to use hashmaps. "
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:As it is now the "Q" Im using to break the loop is stored as well in the array.

Be aware, Java is a case sensitive, so, when you check "!s.equals("q")" - "Q" wouldn't stop your loop.
Look for another method which would suit your needs. You could find them here (<< link) by looking at String class.
Declared and initialised "int i = 0;" does nothing so it is redundant - no more excuses about unedited code
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Daniel Ungerfält wrote:As it is now the "Q" Im using to break the loop is stored as well in the array.

Be aware, Java is a case sensitive, so, when you check "!s.equals("q")" - "Q" wouldn't stop your loop.
Look for another method which would suit your needs. You could find them here (<< link) by looking at String class.
Declared and initialised "int i = 0;" does nothing so it is redundant - no more excuses about unedited code


Some good hours of sleep and I came up with this :




Seems to work just fine. But I guess if I want to be able to print the degreese for a specific city I have to use HashMaps.
Other thoughts are that I acually never use constructors or getters/setters in my practice objects/programs. I use to start with them just because in every turtorial/book they teach to do this but ending up not needing them?

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have and use constructors in this code; they're just the default constructors that are added implicitly.

One other thing: in general, you should write something like this:



rather than something like this:



The reason is that List is an interface. This leaves open the possibility of using another implementation (other than ArrayList) latter on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!