• Post Reply Bookmark Topic Watch Topic
  • New Topic

NEED URGENT HELP  RSS feed

 
Cyrill wanda
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the
ASCII file (see last page) with Cities and their Temperatures (in Celsius),
write a Java application to read the file sequentially into an array of
objects. Include the following functionality: 1. Print the array. [provide
output] 2. Return a reference to the object containing the highest
temperature [provide output] To do this you will require an array of some
object type defined by you. The array size should be set to 30. Each
element of the array will �store� (or reference) an object. As your
program reads each line (a string), it should split it into two distinct
fields in order to then form an object and store it in the array. The two
fields include a String for �City Name� and an integer for the
�Temperature�. You can use a String �Tokenizer� to split the string

Valletta 29 Alexandria 28 Cairo 20 Johannesburg 27 Lagos 31 Nairobi
27 Casablanca 24 Montreal 23 Washington 27 Boston 19 Moscow 9 Atlanta 24
Havana 30 Kingston 33 Santiago 26 London 21 Calcutta 34 Karachi 30 Madras
33 Shanghai 22 Tokyo 24 Tehran 20 Bombay 31 Rome 25 Paris 24 Madrid 20
Lisbon 23 Frankfurt 23 Amsterdam 22 Dublin 19
What I have attempted so far is;

public class WorldTemperatures { // keyboard input static BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in)); public static void main(String args[] )throws IOException { String name; int Temperatures, number; System.out.print("How many cities? "); number = Integer.parseInt(keyboard.readLine()); temperatures s[] = new temperatures[number]; // input into the array for(int index = 0; index < number; index++) { System.out.println(); System.out.print("Enter Name of the city" + (index+1) + "? "); name = keyboard.readLine(); System.out.print("Enter Name for " + name + "? "); age = Integer.parseInt(keyboard.readLine()); s[index] = new Cities(name,temperatures); } printArray(s); }// end of main // Prints elements of array - "cities objects" private static void printArray ( cities s[]) // int[] a { System.out.println("Cities List:"); for(int index = 0; index < s.length; index++) { System.out.println(s[index].getCities() + " " + s[index].getTemperatures()); } }// end of method printArray }// end of class ArrayStudent //{ // public static void main (String args[]) //{ //final int Array_Length = 30; //int array [] = new int [Array_LENGTH]; //System.out.printf("cities", "value"); //}
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you want someone here to do your homework for you?

I fear you have come to the wrong place.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you seem to be off right from the start
"write a Java application to read the file sequentially into an array of objects."

the items in the file are city name and city temperature, so I would imagine
the object mentioned in the assignment would be a City, with appropriate fields.

you would read in the line, tokenize it, then create a new City() object from
the tokens (converting to appropriate data type if required, to match constructor).
finally this new city becomes the next element of the array.

there is no need for this
System.out.print("How many cities? ");
you are given the count in the assignment

you can't do (1) or (2) unless you get the structure right.

this might start you off
City[] cities = new City[30];
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!