• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Populate Array of type Double from text file

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My primary issue is I need to know how to fill an array of type double with values read from a text file. I have made some good progress at doing this but am now stuck, and now here is what I need help with. In my variable declarations at the beginning of my file I have declared this double array

When I get this working properly I know that I will need to remove the values from this array so that the text file can populate them, but for testing purposes they are in there.
Next, I have successfully been able to read my text file and to output the contents to the console window using the following:
BUT, that is not what I want to do.
So what I am guessing is that instead of using System.out.println (strLine) to print the contents of the text file to the console window, I will need to change that line to somehow fill the interest[] array that I have already declared earlier, but I don't know how to do that.
Any help is much appreciated!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several possible approaches to what you're trying to do.

1) Do you know ahead of time how many lines will be in your file?

2) Do you know how to declare an array of a particular size, without setting any of the elements with specific values? If not, google for java array tutorial.

3) Are you willing or able to use a Collection, such as java.util.ArrayList?

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

This assumes that the numbers are stored in English locale in the source file. If you need to support multiple locales, you'll probably have to employ the NumberFormat or related class.

Try to look these up in the javadoc, if it is still unclear, come back for additional help.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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?
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Martin, the numbers are in a .txt file, one number per line, and only the 3 numbers that were previously declared. I am also not sure on how to implement an ArrayList<Double>, would that completely replace the declaration of the interest array at the beginning of the file or would that be in addition to?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: ?



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.


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?



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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Using a List instead of an array would be a big step in that direction.

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?



Not sure how to answer that until I see what you're doing for 3 lines and get a more specific question about what's giving you trouble there.

Also, I am not sure what you mean by parseDouble for each line?



I was assuming you'd use BufferedReader to read a line. That gives you back a String. If you want a double, you have to call Double.parseDouble() to create a double from that String. If you're using Scanner, however, it has a nextDouble() method that does that for you. Unless you're already comfortable with Scanner and reluctant to take on another new concept, I'd recommend the BufferedReader approach. Scanner seems to cause a lot of confusion, and I personally don't care for it.

Sorry, I am still so new at this, only 9 weeks in to Java programming adventure.



No worries, nothing to apologize for. We're all beginners at some point.

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?



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.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Thanks, Jeff, a lot of great stuff to go off of here. I am going to give it a go and see what I come up with. I appreciate all the help!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! And welcome to the Ranch!
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



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



2. I don't know how to loop over the lines and parseDouble?



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.

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.

I am still trying all kinds of things, but reaching out again for maybe a little hint?



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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Thank you, this is going to help.

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: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.



Thank you so much... back to the drawing board I go.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Yes I see where you're going, and yes I know how to do all of the individual pieces, but putting them together is causing me to lose hair. ;)

Take this for example, I have actually tried what you suggested before with the arrayList and here is what happens: I have an array that I declare at the beginning of my file and I just tried changing it to and I get an error when trying to compile: reference to List is ambiguous, both class java.util.List in java.util and class java.awt.List in java.awt match
List<Double> interest = new ArrayList<Double>();


This is why I am so frustrated, I can't even figure out how to make the arrayList work.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



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.

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.

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.

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:

 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


DONE!

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.


Thanks for the clarification here, it seemed like it could be off, but some of the examples that I have found had it in there so I thought I would give it a try? It's gone.

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.


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()

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:



This also doesn't work and I think it's because my declaration of the BufferReader is inside the first try loop and maybe it should be moved out of it like you have done here?
 
Marshal
Posts: 80656
477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use Java7, you should look into “try-with-resources”.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you use Java7, you should look into “try-with-resources”.


I actually got it to work by moving a couple of things around.

Here is what I have now, and it is compiling


Now, we are down to the final issue (for the first half of this project that is). 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? Monday is my deadline.

Thanks so much for all the help, this forum is awesome!

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


Yes. Jeff has already told you how to do this. Re-read his posts in this thread.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Yes. Jeff has already told you how to do this. Re-read his posts in this thread.



Thanks, Joanne. I have re-read and re-read and re-searched but I am just having a really hard time figuring out how to make it happen, forgive my lack of knowledge and understanding at this point, I am still very new to this and am having a really hard time figuring this out.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This won't quite work because you can't put a string into a double array. So you need to convert the string to a double. Jeff has already told you which method to use to do this.
So, find out what that method is and try putting it in your code. If you can't get it to work, post the new code and someone should be able to correct it for you.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()



Thank you for that, it does compile now, and I will forever remember that little nugget.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so I found an example earlier this morning that had something similar (I think) to what I am trying to do, but I am not sure if I put everything in the right place because my application still does not work properly, but it does compile just fine.


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
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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:

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.



I am quickly becoming very frustrated with all of this... almost ready to throw in the towel. I am trying everything I can think of and all to no avail...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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*



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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



OK, here is what I have now. I was able to get the code to compile and at execution it does print the correct 3 amounts in the console window, and it generates the GUI with no errors. So it would seem to me, according to my print statements that the array is populated, but upon hitting the calculate button it does not work, and only throws errors, which makes me think that the array is not actually populated? Any thoughts?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brandon Buchanan wrote:it does not work, and only throws errors



ItDoesntWorkIsUseless(⇐link). Please paste in the exact, complete error message, and indicate clearly which line is causing it.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Please paste in the exact, complete error message, and indicate clearly which line is causing it.



Exact error message upon calculate is as follows, and I am not sure how to tell what line is causing it with this error?:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PaymentCalculatorWeek5.button1ActionPerformed(PaymentCalculatorWeek5.java:201)
at PaymentCalculatorWeek5.access$000(PaymentCalculatorWeek5.java:19)
at PaymentCalculatorWeek5$1.actionPerformed(PaymentCalculatorWeek5.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6373)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6138)
at java.awt.Container.processEvent(Container.java:2085)
at java.awt.Component.dispatchEventImpl(Component.java:4735)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
at java.awt.Container.dispatchEventImpl(Container.java:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:652)
at java.awt.EventQueue$2.run(EventQueue.java:650)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

And if the GUI specifically is what's giving you trouble, you should probably start a new thread. I think this site has a Swing forum. Otherwise, Beginning Java would be okay.

(Yes, it's the same old broken record about divide-and-conquer. Trust me, even though it may seem like a pain, it works. I've been programming professionally for almost 20 years, about 12 of them in Java, and I still do it whenever I come upon a new concept, or get something that I just can't figure out.)
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



My GUI works beautifully when I manually put the amounts in the array like this:


So I don't need help with the GUI, what I need help with is getting those amounts in to the array from the file... now I sound like the broken record, but just really need the help.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your getInterestRates() method looks correct to me, and, since the exception came from the GUI, that's where the problem is. Since you say it works fine with hardcoded values, the problem would seem to be in the GUI's response to the file being read.

Like I said, I don't do GUIs, but if you post your entire code, I might be able to spot something.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

If this is the case, then instead of getInterestRates() returning void, it should return the double[] it has constructed, and you would change

to


Note that this won't work in all cases--GUI interactions with the rest of the app can be tricky because of their asynchronous nature--but it might fix your problem here.

Also, what are on these lines of code?

 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



I think this is spot on. I tried to change my initial declaration to the following but you're right, it did not work.


So, I am not sure how to get the rest of the program to see that array that I populated? Is that possible?

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
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.




Once again: ItDoesntWorkIsUseless.

In this case, though, I'm going to guess that you only changed that, but did not change your getInterestRates() method.



Do you know what the void means there? Do you know how to return a value from a method?


Also, I will post my code as long as you don't give me a hard time for all of the bad formatting... ;)



That may not be necessary now.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd still like you to tell me what's on the lines of code indicated in this stack trace (if you haven't changed things so that those line numbers no longer apply):



 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:In this case, though, I'm going to guess that you only changed that, but did not change your getInterestRates() method.


OK, I made that change as well and here are the errors that I get:

Jeff Verdegan wrote:Do you know what the void means there? Do you know how to return a value from a method?


To be honest, I don't FULLY understand it.
reply
    Bookmark Topic Watch Topic
  • New Topic