Martin Vajsar wrote:Using an ArrayList<Double> to store the numbers might better suit your needs, unless you know beforehand how many numbers there are in your file.
How are the numbers stored in your file? If there is one number per line, then using Double.valueOf(String) might suit your needs. If there can be more numbers on one line, then perhaps String.split() to separate them apart could be used.
Brandon Buchanan wrote:@Jeff
Answers to your questions:
1. Yes, just those 3 amounts that are in the double interest[] array are all that I need. I have a text file that has each of those numbers on their own line.
2. Yes I do know how to do that, wouldn't it be something like this: ?
3. I am willing and able to do whatever works, there are no restrictions, just need to make it happen. I have never used an ArrayList so I am not sure how to do that?
Jeff Verdegan wrote:
Okay, so if you know there are exactly 3 lines, and since you know how to declare an array, then the easiest approach would be to just declare the array like in your answer above, then loop over the lines, parseDouble for each line, and put it at the current index in the array. Note that this is not a flexible or scalable approach, however.
Google for java collections tutorial. Even if you don't use it here, it's something you'll need if you do any significant Java programming. And in this case it's quite simple. Just create the ArrayList, and then call list.add() for each double that you parse.
Brandon Buchanan wrote:
Jeff Verdegan wrote:
Okay, so if you know there are exactly 3 lines, and since you know how to declare an array, then the easiest approach would be to just declare the array like in your answer above, then loop over the lines, parseDouble for each line, and put it at the current index in the array. Note that this is not a flexible or scalable approach, however.
I recieve that. So how can I make it more scalable?
Obviously by making it look in the file and read all the lines in there, not just limited to 3, but how do I do that?
Also, I am not sure what you mean by parseDouble for each line?
Sorry, I am still so new at this, only 9 weeks in to Java programming adventure.
Google for java collections tutorial. Even if you don't use it here, it's something you'll need if you do any significant Java programming. And in this case it's quite simple. Just create the ArrayList, and then call list.add() for each double that you parse.
I will do this and see what I come up with. Will this all be able to be incorporated into what I have currently posted or will I need to scrap what I have and try a different approach?
Jeff Verdegan wrote:
If you've already written it with arrays, it's very straightforward to replace those arrays with a Lists. The only real burden is that if you have a lot of uses of the arrays, then there will be a lot of places to make the same, simple replacements. Structurally, however, there's no real change to your code.
Jeff Verdegan wrote:
Okay, so if you know there are exactly 3 lines, and since you know how to declare an array, then the easiest approach would be to just declare the array like in your answer above, then loop over the lines, parseDouble for each line, and put it at the current index in the array.
Google for java collections tutorial. Even if you don't use it here, it's something you'll need if you do any significant Java programming. And in this case it's quite simple. Just create the ArrayList, and then call list.add() for each double that you parse.
Brandon Buchanan wrote:
I've been going at this for a while now and still can't figure out what to do?!
1. I don't know where to declare the array? Here in the try section that I posted or in place of the interest array that I already declared?
2. I don't know how to loop over the lines and parseDouble?
I am still trying all kinds of things, but reaching out again for maybe a little hint?
Jeff Verdegan wrote:I don't know what you mean by "here in the try section", but if you had it working with an array, than you can simply replace the array declaration with a List declaration
Jeff Verdegan wrote:So, you don't know how to read a text file line by line?
Google for something like java io tutorial or java read text file for an explanation and examples.
Jeff Verdegan wrote:An important note: You're talking about two totally separate and independent things here: 1) Reading the lines from a text file, and 2) Parsing a String into a Double. Study them and practice them separately. Learn how to read the lines from a file without worrying about parsing doubles. Totally separately from that, learn how to turn a String into a double--just a hardcoded String, without worrying about reading from a file.
Like I said, there are plenty of examples out there. After you do some research and try to make it work on your own, if you get stuck, post what you tried and a specific question about the part that's tripping you up.
Brandon Buchanan wrote:
Jeff Verdegan wrote:So, you don't know how to read a text file line by line?
Google for something like java io tutorial or java read text file for an explanation and examples.
Well, I do, as represented by my code above and my statement that it is reading the file, but I can honestly say that I don't fully understand how it all works at this point? Like I said, I can get it to read the 3 amounts from the file, and it spits them out in the console, but I need to store them in the array, that is what I am after here.
Jeff Verdegan wrote:So you don't know how to store something in an array? (Or, once you make the switch, in a List?)
You see where I'm going here? If you know how to read a line in a file, and you know how to loop, and you know how to store a value in an array, then you know everything you need to know to read a text file into an array. It's just a matter of putting the pieces together.
Now, I'll grant you, even though it sounds like it should be simple, you wouldn't be the first person to get tripped up at the "putting the pieces together" part. If you do, post what you tried, and explain as clearly and precisely as you can what you're still having trouble with.
Jeff Verdegan wrote:Now, I'll grant you, even though it sounds like it should be simple, you wouldn't be the first person to get tripped up at the "putting the pieces together" part. If you do, post what you tried, and explain as clearly and precisely as you can what you're still having trouble with.
Brandon Buchanan wrote:
Jeff Verdegan wrote:Now, I'll grant you, even though it sounds like it should be simple, you wouldn't be the first person to get tripped up at the "putting the pieces together" part. If you do, post what you tried, and explain as clearly and precisely as you can what you're still having trouble with.
OK, so here is what I have now. I think I am correctly reading the contents of my file into a String array... but I can't tell because the program still doesn't execute, because I am missing the part that puts the elements of this string array into my interest array. Did I get this part right?
Jeff Verdegan wrote:1) Get rid of DataInputStream. That's for reading raw primitives--for instance the binary representation of a double, exactly as it would be stored in the JVM. You're reading text, so you don't need that.
Jeff Verdegan wrote:2) Your while (...) { for (...) {...} } nested loops say, roughly, "For each line in the file, read 3 lines and put them into the array". That is, your outer loop (while) contains the set of steps you want to execute for each line in the file. What do you want to do for each line? Add that one line to the array. But then your inner loop (for) has steps that you want to execute for each element in the array. So this means that for each line in the file, you're touching every element in the array. In short, you don't want nested loops. Get rid of the inner for loop and just to the single step of putting the current line into the array.
Jeff Verdegan wrote:3) In your catch block, rather than just printing e.getMessage(), instead call e.printStackTrace(). That will provide more details about what went wrong. Even that isn't proper exception handling, but it's better than nothing, and for educational programs, it's often sufficient.
Jeff Verdegan wrote:4) This is more for future reference, but you should close() in a finally block. There are a couple of different patterns. Here's the one I usually use:
Campbell Ritchie wrote:If you use Java7, you should look into “try-with-resources”.
Brandon Buchanan wrote:I have calculations that depend on an empty double array called interest and I need to get the 3 values in this text file into that specific array. Is this going to be possible?
Joanne
Joanne Neal wrote:Yes. Jeff has already told you how to do this. Re-read his posts in this thread.
Brandon Buchanan wrote:This doesn't work. It gives me an error that says 'void' type not allowed here System.err.println("Error: " + e.printStackTrace()); So I have left it at e.getMessage()
Joanne
Joanne
Joanne Neal wrote:
Brandon Buchanan wrote:This doesn't work. It gives me an error that says 'void' type not allowed here System.err.println("Error: " + e.printStackTrace()); So I have left it at e.getMessage()
The clue is in the name. You don't need the System.err.printl, just e.printStackTrace()
Brandon Buchanan wrote:
So my question is, is this actually putting each line of my file into my interest array?
Also, in this example I commented out 2 lines because I am not even sure that they are being used for anything, or that they are even necessary? Am I wrong about this?
Jeff Verdegan wrote:
Brandon Buchanan wrote:
So my question is, is this actually putting each line of my file into my interest array?
Well, after your read/populate loop completes, print out each element of the array and see if it's what you expect. In fact, this is a good debugging tip in general, and even professionals do it: When you don't know what your code is doing, or it's not behaving as you want or expect, ad a bunch of print statements to see which code is executing and what various values are.
Jeff Verdegan wrote:
Brandon Buchanan wrote: Also, in this example I commented out 2 lines because I am not even sure that they are being used for anything, or that they are even necessary? Am I wrong about this?
Well, if you're not using them, and your code works, then obviously they're not needed.![]()
Brandon Buchanan wrote:
Well I tried to put print out the elements of the array and nowhere I put the System.out.println(interest[i]); it never prints anything out, so either the array is not filling up or I can't figure out how to get it to print the array? *feeling very dumb at the moment*
Jeff Verdegan wrote:So write a program that does nothing but populate an array with hardcoded values and then prints it out. If you can't get it to work, post here. If it does work, then in your main code, either you're never reaching the array printing code, or the array's length is 0. You can figure these out by following my earlier advice to add lots of print statements.
Brandon Buchanan wrote:it does not work, and only throws errors
Jeff Verdegan wrote:Please paste in the exact, complete error message, and indicate clearly which line is causing it.
Jeff Verdegan wrote:Oh, a GUI problem.
Sorry, I don't do GUIs, so I can't help you there. You should be able to write your program to do the same thing without a GUI, so you can verify that the file-reading and array-populating logic works. Independently of that, you can write a GUI app that just responds to a button push and prints out "Hello World!", or whatever specific GUI part you're having trouble with. Once you get that figured out, put them together.
Jeff Verdegan wrote:Actually, something just occurred to me.
Before you had something like
Right?
Did you do something with that array outside of the getInterestRates() method? Or was that method exactly the same (println()ing the 3 elements) except that it didn't read from the file?
If the latter--if, in the working hardcoded version, that array existed outside of getInterestRates(), then that's most likely the source of the problem. You went from declaring an array and populating it, to just declaring it and then ignoring it, and instead creating a different array, populating it, printing it, and letting it go out of scope, never to be seen again.
Brandon Buchanan wrote:
I think this is spot on. I tried to change my initial declaration to the following but you're right, it did not work.
Also, I will post my code as long as you don't give me a hard time for all of the bad formatting... ;)
Jeff Verdegan wrote:In this case, though, I'm going to guess that you only changed that, but did not change your getInterestRates() method.
Jeff Verdegan wrote:Do you know what the void means there? Do you know how to return a value from a method?
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |