• 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

Help with loop and arraylist

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic