Bookmark Topic Watch Topic
  • New Topic

Can somebody help me understand this problem?

 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
ok. This is probably a very simple problem, I am just having trouble understanding what the problem is here. I am trying to read in data from a text file, then convert it to an array to be used in a heat index calculation. Following is the code that I have so far. I don't see why is is not recognizing my attempt to create an array from the information that has been read in from the text file. If anybody knows anything about this...

 
Henry Wong
author
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator

Java doesn't automatically convert a string reference to a string array reference, with multiple reassignments of a string reference. You actually have to instantiate the array, and assign the strings to the individual elements of the array.

Henry
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
In line# 35 of your code. I can see that you are trying to use an enhanced for-loop using a String object. In line 20,

A String cannot be used in an enhanced for-loop as it expects an Iterable object or an array. You can read the contents from the file one by one, store it in a new String array and finally use an enhanced for-loop to print the content of this array.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
Probably better to add the lines from the file to a List<String> because you can't predict how many there will be. You can use a for-each loop on a List.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator


You have declared temp to be a String. It will hold exactly ONE string. So, on each iteration through this loop, you read a line, and blow away the previous line. So, when this loop is done, temp simply holds the last line of the file.
 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
Well how would I make it hold the entire thing, then?

fred rosenberger wrote:

You have declared temp to be a String. It will hold exactly ONE string. So, on each iteration through this loop, you read a line, and blow away the previous line. So, when this loop is done, temp simply holds the last line of the file.
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
You can do something like this
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
Note that you may have the same issue with your second loop. {edit - maybe not. I see you're just reading and printing the lines, not doing anything with them elsewhere]

You need to declare something that holds a LOT of strings. There are various tradeoffs that have to made, depending on your speed, memory and various other things, but the solution listed above will probably be ok.



What is being done here is you are declaring a reference called 'temp' that will point to something that impelemnts a 'List' interface, and it will hold Strings (that's what the left side of the '=' means).

The right-hand side then creates an actual ArrayList object (which fortunatly implements the List interface) that holds Strings.

You can then iterate across this List to get all the Strings out of it.
 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
Is there any special thing that I need to initialize at the beginning of the program to work with the list class? I am trying out the list class, however, it says that it cannot find symbol class list. I was just wondering if I needed something else to work with this? Thanks
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
You have to add two more import statements to your existing code

 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
ok. Thanks for the info. However, I am calculating heat index. So I am calling the temps and humidity levels from two separate text files, as you have seen. However, I am not getting the program to calculate the way I would like. I did the List thing with the arrays, however, you will see that it still does not work. I was wondering what I have wrong this time

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
it helps greatly if you tell us what you expect it to do and what it is actually doing. Nobody here can run it, as we don't have your input files. Even if we did, it is still a pain to grab your source, compile it, run it, then try and guess what the problem is (although it may be obvious when the program runs if it throws an error or something).

So, please tell us what the problem is - with the exact text of any error message.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
Also, you are going to have too much in your main method. You ought to make methods for all the thing syou are doing, so you will have a readTemperature() method, a readHumidity() method, a calculateHeatIndex() method, etc, etc.
You can have the List<String> mentioned previously as a field of the class, but remember to initialise it in the constructor.
 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
Ok. This program is supposed to calculate the heat index of a certain city based on temperature and humidity levels. So, I am reading in the temperature and humidity levels from text files. Then I am calculating heat index with the numbers that I read in. So, that is what line 49 does. It is the beginning part of the code to calculate the first heat index. The error message I am getting when I run the program is: "array required, but java.util.List<java.lang.String>found" and the compiler highlights line 49. So I am trying to find the simplest way to read in the data files and use those numbers to calculate separate heat indexes.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
You can put a List into the last part of a for-each look.
for (String s : temperatureList) . . .

You may need to use the parse methods of the Integer or Double classes to turn them into numbers.

You can get a size() method in the List which tells you how many objects it contains.

What you actually want is to go through the List, calculate the heat index from each entry in the temperature List, and add it to a List<Double> which holds the indices. Because of auto-boxing you can say this sort of thing:
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic