• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing GUI to add a list of numbers entered by the user

 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am feeling fairly stupid for not being able to figure out this seemingly simple code. I have a GUI with a textArea for the user to input numbers, a button which should "listen" for those numbers, and then a textField to display the sum. I have my code working for user input of one number; but I'm at a loss as to what sort of loop I need to create to get it to read each line of input. I wasn't sure whether to put this in the beginner forum or here, because I am definitely a beginner.

So my code thus far that works with one input:

 
Claude Moore
Ranch Hand
Posts: 832
7
IBM DB2 Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just an hint:you're trying to parse the whole content of your text area... what's that content when you press the calculate button? Try first to print it with System.out..
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, when I change it to:


and then enter some numbers in the textArea I don't get any output in my console, however when I put in just one number it prints that number out. What this is telling me is that I need a way to recognize each line input into the textArea. I tried using split but that didn't seem to do anything, my console remains blank with multiple numbers input but will still display one number.

 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now I can get a list of numbers to display but how to add them together is my next question. Do I need a sum variable? Also, now my catch exception isn't working but I think I can figure that out. Any hints are appreciated. My code as it is now...
 
Claude Moore
Ranch Hand
Posts: 832
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you need a variable to sum over inputed values.. and, to be honest, i think that you should review your computer programming basis, no matter which language you're going to use...
Update:
Betty, I'm afraid of having been to hard with you. Sorry if my answer hurted you somehow, but asking if you need a variable where to store the sum of values seemed to me more
a programmer beginner's question than a Java programmer one....
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are absolutely correct, I am a beginner all around. No offense taken. I should also add I am struggling to keep up in a course I'm taking that was listed as beginning programming with no pre-reqs, so there's that.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figured it out...I wasn't too far off to begin with but your hints helped me along. Thank you.

Final code:
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not look a good solution I am afaid. You need to step a long way back.

Don't try to calculate anything in a GUI. GUIs are not for calculation or any other logic or operations. They are for displaying things. You ought to have classes which do that addition., and it should be possible to pass the text to an addition object and get it to do the addition. Get an app which does additions when you pass it the lines via the command line. The put your GUI atop that, with controls which sent the data to be calculated, and get the result back.
By the way: you can add action listeners to some text components, and they are activated when you push the enter key. So you might get away without a button.
Using \n to split text might not be reliable, because \n is not necessarily the correct line end. You might be better using "\\s+" to split, which will allow you several numbers per line, and also multiple spaces between successive numbers. You can also pass the entire text to a Scanner and use its hasNextInt method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!