File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can somebody help me understand this problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Can somebody help me understand this problem?

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
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
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

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.


SCJP 5.0 and now fighting with ExtJS
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
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

Joined: Oct 02, 2003
Posts: 11494
    
  16



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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
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

Joined: Oct 18, 2004
Posts: 136

You can do something like this
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

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

Joined: Jan 13, 2009
Posts: 83
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

Joined: Oct 18, 2004
Posts: 136

You have to add two more import statements to your existing code

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
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

Joined: Oct 02, 2003
Posts: 11494
    
  16

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

Joined: Oct 13, 2005
Posts: 40027
    
  28
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

Joined: Jan 13, 2009
Posts: 83
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

Joined: Oct 13, 2005
Posts: 40027
    
  28
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can somebody help me understand this problem?