Requesting Multiple Numbers performing operation on each and outputtin

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 885 Views - Last Post: 12 February 2011 - 01:56 PM Rate Topic: -----

Topic Sponsor:

#1 atvman29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 11

Requesting Multiple Numbers performing operation on each and outputtin

Posted 09 February 2011 - 06:06 PM

Hey guys,

This forum has provided a great deal of help already but I'm still stumped. I've been searching for quite a while now, but nothing has reflected quite what I need to accomplish.

First of all, I'm NOT a good programmer by any stretch, thus my plea for help. So please don't make any simple assumptions! :rolleyes2:

Ok, so I am wanting to develop a program that accepts a vast amount of numbers, around 100 or so, categorizes them based on their value (between 0 and 3, 3 and 6, etc), performs a mathematical operation on each value that is dependent upon which category they are placed in, then outputs the results of each value with the original value in a format that can be placed in Excel to be graphed.

I'd like to use JOptionPane for all operations. Is there anyway to use the Scanner with a JOptionPane? That is one of the problems I have been having. I've been unable to figure out how to make things like this happen with JOptionPane.

Any ideas??

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Requesting Multiple Numbers performing operation on each and outputtin

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 997
  • View blog
  • Posts: 3,023
  • Joined: 20-August 09

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 09 February 2011 - 06:44 PM

Quote

Is there anyway to use the Scanner with a JOptionPane?

Why? If you get user input using JOptionPane, why use Scanner for this same purpuse as well?
For more info about JoptionPane:
http://download.orac...nts/dialog.html

As for the file you want to create to be read by Excel, check for a CSV file:
http://en.wikipedia....eparated_values
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6381
  • View blog
  • Posts: 25,918
  • Joined: 06-March 08

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 09 February 2011 - 06:46 PM

You are seriously thinking of using a JOptionPane to input 100 numbers from the user ???
You have very patient users :)
Was This Post Helpful? 1
  • +
  • -

#4 atvman29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 11

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 05:44 PM

Do you guys have any other suggestions? Is there a way for me to take a single input from a JOptionPane, store it in an array, request another input, store it in the next row, etc.?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7497
  • View blog
  • Posts: 28,838
  • Joined: 27-December 08

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 05:46 PM

You can have the user enter the multiple inputs separated by spaces, then use the String split() method on whitespace, and parse the returned array accordingly.
Was This Post Helpful? 0
  • +
  • -

#6 atvman29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 11

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:10 PM

Would I then be able to perform a mathematical operation on each and then output the result to a CSV text file?

Currently, I am able to perform the full operation I desire for a single input...I just need to find a way to store that completed value so as to be exported into a CSV text file for graphing on Excel.

I think I uploaded what I currently have correctly...

Don't laugh too hard at my extrememly amateur coding! :blush:

Attached File(s)


This post has been edited by atvman29: 10 February 2011 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7497
  • View blog
  • Posts: 28,838
  • Joined: 27-December 08

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:11 PM

Please post your code in the body of your post using code tags, like so: :code:. Also, please post a small sample of your data in the body of your post.
Was This Post Helpful? 0
  • +
  • -

#8 atvman29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 11

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:20 PM

Here's part of what I have....

The user will input a global coordinate (double), and two other double values for the nodes.

if (oneDChoice == "Generate Shape Functions/Find Values"){
				
				double oneDGlobalCoord, oneDShapei, oneDShapej, oneDValue,
				oneDNodeValue1, oneDNodeValue2, oneDNodeValue3, oneDNodeValue4, oneDNodeValue5, oneDNodeValue6, oneDNodeValue7, oneDNodeValue8, oneDNodeValue9, oneDNodeValue10, oneDNodeValue11;
				
				oneDGlobalCoord = Double.parseDouble(JOptionPane.showInputDialog("Enter the global coordinate."));
				
				//Generate Functions
				
				if (oneDGlobalCoord <= oneDLength1){
					
					oneDShapei = (oneDLength1 - oneDGlobalCoord) / oneDLength1;
					oneDShapej = (oneDGlobalCoord - 0) / oneDLength1;
					
					JOptionPane.showMessageDialog(null, "The shape functions for this value are\n" + "Si = " + oneDShapei + "\n" + "Sj = " + oneDShapej);
					
					oneDNodeValue1 = Double.parseDouble(JOptionPane.showInputDialog("Enter the value for node 1."));
					oneDNodeValue2 = Double.parseDouble(JOptionPane.showInputDialog("Enter the value for node 2."));
					
					oneDValue = (oneDShapei * oneDNodeValue1) + (oneDShapej * oneDNodeValue2);
					
					JOptionPane.showMessageDialog(null, "The value for the selected coordinate is " +oneDValue);
					
				}	
				
					else if (oneDGlobalCoord <= (oneDLength1 + oneDLength2)){
					
						oneDShapei = ((oneDLength1 + oneDLength2) - oneDGlobalCoord) / oneDLength2;
						oneDShapej = (oneDGlobalCoord - oneDLength1) / oneDLength2;
						
						JOptionPane.showMessageDialog(null, "The shape functions for this value are\n" + "Si = " + oneDShapei + "\n" + "Sj = " + oneDShapej);

						oneDNodeValue2 = Double.parseDouble(JOptionPane.showInputDialog("Enter the value for node 2."));
						oneDNodeValue3 = Double.parseDouble(JOptionPane.showInputDialog("Enter the value for node 3."));
						
						oneDValue = (oneDShapei * oneDNodeValue2) + (oneDShapej * oneDNodeValue3);
						
						JOptionPane.showMessageDialog(null, "The value for the selected coordinate is " +oneDValue);
						
					}
				
						else if (oneDGlobalCoord <= (oneDLength1 + oneDLength2 + oneDLength3)){
						
							oneDShapei = ((oneDLength1 + oneDLength2 + oneDLength3) - oneDGlobalCoord) / oneDLength3;
							oneDShapej = (oneDGlobalCoord - (oneDLength1 + oneDLength2)) / oneDLength3;
							
							JOptionPane.showMessageDialog(null, "The shape functions for this value are\n" + "Si = " + oneDShapei + "\n" + "Sj = " + oneDShapej);

							oneDNodeValue3 = Double.parseDouble(JOptionPane.showInputDialog("Enter the value for node 3."));
							oneDNodeValue4 = Double.parseDouble(JOptionPane.showInputDialog("Enter the value for node 4."));
							
							oneDValue = (oneDShapei * oneDNodeValue3) + (oneDShapej * oneDNodeValue4);
							
							JOptionPane.showMessageDialog(null, "The value for the selected coordinate is " +oneDValue);
							
						}

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7497
  • View blog
  • Posts: 28,838
  • Joined: 27-December 08

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:22 PM

First thing I see. Don't compare Strings using the == operator, as it tests their locations in memory, not the values in the Strings. Use the String equals() method instead.
Was This Post Helpful? 0
  • +
  • -

#10 atvman29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 11

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:38 PM

View Postmacosxnerd101, on 10 February 2011 - 06:22 PM, said:

First thing I see. Don't compare Strings using the == operator, as it tests their locations in memory, not the values in the Strings. Use the String equals() method instead.


This is what that is for:

Object[] possibilities = {"Convert/Find Coordinates", "Generate Shape Functions/Find Values", "Create Plottable Array"};
			
			String oneDChoice = (String)JOptionPane.showInputDialog(null, "Please select one of the following tasks.",
                    "Task Inquiry",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    possibilities,
                    "Convert/Find Coordinates");

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7497
  • View blog
  • Posts: 28,838
  • Joined: 27-December 08

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:42 PM

Still, not good practice to use the == operator to compare Strings or other types of Objects unless you care where they are located in memory. Regardless of the String pool, which is there to conserve resources not allow you to compare Strings with the == operator and get away with poor practice.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6381
  • View blog
  • Posts: 25,918
  • Joined: 06-March 08

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:43 PM

What macosxnerd101 said is that

if (oneDChoice == "Generate Shape Functions/Find Values"){ 


will never be true. The String returned from the OptionPane is not in same place in memory than your String constant "Generate Shape Functions/Find Values"

You have to
if (oneDChoice.equals("Generate Shape Functions/Find Values")){ 


which compare the content of the Strings not their addresses
Was This Post Helpful? 1
  • +
  • -

#13 atvman29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 11

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:46 PM

hmm this is weird because that part of my program is working perfect...
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7497
  • View blog
  • Posts: 28,838
  • Joined: 27-December 08

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:47 PM

The String pool determines if a new String object needs to be created or if an existing one can be reused. It's great for saving memory. Not good practice to rely on it to use the == operator though, which may work 10% of the time. Use the equals() method which works 100% of the time. :^:
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6381
  • View blog
  • Posts: 25,918
  • Joined: 06-March 08

Re: Requesting Multiple Numbers performing operation on each and outputtin

Posted 10 February 2011 - 06:56 PM

View Postmacosxnerd101, on 10 February 2011 - 08:47 PM, said:

The String pool determines if a new String object needs to be created or if an existing one can be reused. It's great for saving memory. Not good practice to rely on it to use the == operator though, which may work 10% of the time. Use the equals() method which works 100% of the time. :^:

The String pool is used for String constants in the program:

String a = "xyz";

in another class:

Strinb b = "xyz";

a and b will be == because the constant "xyz" is retreived from the pool

When the String is built from user input (Scanner or JOptionPane or InputBuffer) that will never be the case. The String is built byte per byte using a StringBuffer from user input and is not compared with the pool.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2