multiply user input through JTextField with an array of double values - java

I'm very new to Java (and programming in general). I'm working on a program that should accept a double value from user through a JTextField and multiply it with quite a few double values in an array, and display the result in a JTextArea. A kind of calculator you could say.
Right now, I only see the result of the input multiplied with the last value in the array(0.50) and im sure there's something with my loop, array or something missing, I just cant figure out what.
double[] percRM = {0.65, 0.75, 0.85, 0.70, 0.80, 0.90, 0.30, 0.40, 0.50};
double dDouble;
double pFinal;
if(ae.getSource() == pressbutton){
pDouble = Double.parseDouble(presstext.getText());
for (int j=0;j<percRM.length;j++){
pFinal = percRM[j] * pDouble;
}
resulttext.setText("The Sum is:" + "\n" + pFinal+ "\t");
}
I also have a JFrame with quite a few buttons. Please tell me if you want me to show my entire code.
And while im here, I wonder how I can add some text next to each value printed, lets say user input is 100, the result will be(example):
The sum is:
Week1:
Set1 = 65
Set2 = 75
Set3 = 85
etc.
appreciate any help, thanks

Your problem is that you're calling setText(...) on the JTextArea and doing so after the loop is over. setText(..) will erase everything that the JTextArea is currently showing and replace it with your new text -- now what you want. The append(...) method just adds new text tot he bottom.
Instead call append(...) and do so inside of the loop.
for (int i = 0; i < someLength; i++) {
double value = doSomeCalculation():
myTextField.append("result: " + value + "\n");
}

Related

Graphing with JLabel for looks [null layout] Java Swing *ERROR

I am trying to build a student graph with JLabels
my app does a sql query and returns an int value for each month's number of entries and if doesnt find any it returns 0.
//scale variable is an int
//ene is a return value from another method that runs before
//in case there are no students in january I dont have to graph anything
//so this 'if' doesnt run
Scale = 50;
if (Ene != 0) {
System.out.println(Ene + " ene stdds");
// this prints out 11 ene stdds
double EneBH = 449 * (Ene / Scale);
int EneBHeight = (int) Math.round(EneBH);
int EneBYLocal = 612 - EneBHeight;
EneP.setBounds(76, EneBYLocal, 7, EneBHeight);
EneP.setVisible(true);
} else {
//if the last if didnt run I want to know if it hidd the label
System.out.println("HIDDEN ENE");
EneP.setVisible(false);
}
*Ene P is the very first jlabel for graphing, it looks kida gray and is at the enero zone.
*EneP prints out 11 students but never shows up, doesnt print hidden ene, it just doesnt show up
*EneP will have the same code than the other jlabels if I solve it or you solve it, please
I have found a Solution for My Own Problem
The solution was easy turns out that If you divide Ene (the return variable from a query that saves as an integer value) by the Scale variable which is also an integer it causes java to return 0
So I have discovered this on my own by placing a system.out.print() after every line and printing the values of every variable.
I noticed 11/50 shouldn't return 0, so I changed the scale variable to be a double and still returned 0; but I changed both and now it works just right.
Scale = 50;
if (Ene != 0) {
System.out.println(Ene + " ene stdds");
// this prints out 11 ene stdds
double EneBH = 449 * (Ene / Scale);
int EneBHeight = (int) Math.round(EneBH);
int EneBYLocal = 612 - EneBHeight;
EneP.setBounds(76, EneBYLocal, 7, EneBHeight);
EneP.setVisible(true);
} else {
//if the last if didnt run I want to know if it hidd the label
System.out.println("HIDDEN ENE");
EneP.setVisible(false);
}
enter image description here

How to convert double values to string in a text field

I want to do the average of 9 textfields and also the sum of them and place them in 2 other textfields by using a button, currently this code doesnt displays anything in the other textfiels. If i put anything, for example "A" instead of "%.Of" it would display the "A" in the textfield but not the average or the sum. Please i need help with a code that would work, dont mind if i need to change a lot.
This is what im working with:
private void jButton_RankingActionPerformed(java.awt.event.ActionEvent evt) {
double R[] = new double [14];
R[0] = Double.parseDouble(jTextField_Math.getText());
R[1]= Double.parseDouble(jTextField_English.getText());
R[2] = Double.parseDouble(jTextField_Spanish.getText());
R[3] = Double.parseDouble(jTextField_Biology.getText());
R[4] = Double.parseDouble(jTextField_Physics.getText());
R[5] = Double.parseDouble(jTextField_Chemestry.getText());
R[6] = Double.parseDouble(jTextField_PE.getText());
R[7] = Double.parseDouble(jTextField_Humanities.getText());
R[8] = Double.parseDouble(jTextField_Technology.getText());
R[9] = (R[0]+R[1]+R[2]+R[3]+R[4]+R[5]+R[6]+R[7]+R[8])/ 9;
R[10] = R[0]+R[1]+R[2]+R[3]+R[4]+R[5]+R[6]+R[7]+R[8];
String Average = String.format("%.Of",R[9]);
jTextField_Average.setText(Average);
String TotalScore = String.format("%.Of",R[10]);
jTextField_TotalScore.setText(TotalScore);
if(R[10]>=50)
{
jTextField_Ranking.setText("Superior");
}
else if (R[10]>=41){
jTextField_Ranking.setText("Alto");
}
else if (R[10]>=34){
jTextField_Ranking.setText("Basico");
}
else if (R[10]<=33){
jTextField_Ranking.setText("Bajo");
Since you mentioned that an A would print, it follows that jButton_RankingActionPerformed is being called. The issue you have is the format string you are using to print the total and average. You have mistakenly chosen the capital letter O rather than the number zero.
Replace this (which contains a capital letter O):
String.format("%.Of",R[9]);
With
1) No decimal will be printed: i.e. 50.2 would be 50
String.format("%.0f",R[9]);
2) Or perhaps you want to see one decimal place like 50.2
String.format("%.1f",R[9]);
Also a very small optimization is:
R[9] = (R[0]+R[1]+R[2]+R[3]+R[4]+R[5]+R[6]+R[7]+R[8])/ 9;
R[10] = R[0]+R[1]+R[2]+R[3]+R[4]+R[5]+R[6]+R[7]+R[8];
Could be replaced with:
R[10] = R[0]+R[1]+R[2]+R[3]+R[4]+R[5]+R[6]+R[7]+R[8];
R[9] = R[10] / 9;
or use a loop to calculate R[10]. (to add R[0] to R[8])

How to generate a varying number of textboxes and labels in Window Builder

I am using Eclipse Window Builder, and in my program, I would like to ask the user to enter the highest degree of the polynomial and based on his answer, I would like my program to display n text boxes and n label for him to enter the coefficient of each x
Example:
Enter Highest Degree: 3
-- X^3
-- X^2
-- X^1
-- X^0
Anyone knows how this can be done?
If you know the number of boxes you need, simply pass that number and a parent Composite (with the Layout you need) to the method below:
private void addBoxes(Composite parent, int number)
{
for(int i = 0; i < number; i++)
{
Text text = new Text(parent, SWT.BORDER);
// Maybe add them to a List here so you can use them again later.
}
}
If you want to call this method more than once, remember to dispose() of the old Texts before you do so.

How can I store a bunch of double in a single double?

I edited my post.
:: New logic problem, everytime I input only 1 integer the += only prints 0.
System.out.print("\nEnter the property code: ");
sPropertyCode = input.next();
bError = false; //set to false
dTotalCommission += dCommissionRate;
dTotalSales += dSellPrice;
if (sPropertyCode.equalsIgnoreCase("R"))//if r or R dRate will store 7,...perform calculation for dCommissionRate
{
dRate = 7;
dCommissionRate = dSellPrice * (dRate/100);
System.out.print("Total commission on this property is $" +dCommissionRate);
} //this works and prints the calculated amount of rate but when it is going to the last line....
if (sYesOrNo.equalsIgnoreCase("n"))
{
System.out.println(sApplicationReport);//prints the Summary Report
System.out.println ("----------------------------------------------------------");
System.out.println ("Total property sales: $" + dTotalSales);//all the stored values for dSellPrice will be added and printed
System.out.println("Total Commissions: $"+ dTotalCommission);//This part only prints 0.00 instead of the calcuated dCommissionRate
break;
}
dTotalPrice += dSellPrice
means : dTotalPrice = dTotalPrice + dSellPrice
But if you want to store 10000 and 20000 in a single variable , you can use an arrayList :
Example :
ArrayList<Double> myValues = new ArrayList<Double>();
myValues.add(10000 );
myValues.add(200O00 );
// etc.
If you want to show them :
for(int i = 0 ; i < myValues.size(); i++){
Double mySingleValue = myValues.get(i);
System.out.println(mySingleValue.toString());
}
Hm. It's kind of hard to follow your thinking, but here is my best shot.
Your code here (dTotalPrice += dSellPrice) Will add the value of dSellPrice to dTotalPrice.
aside from a semicolon you aren't missing anything.
Instead of trying to store multiple doubles in a single double variable, why not try and store your multiple doubles in an array? Therefore you could store your multiple number values in this array and then pick out the ones you want.
You'd just reassign the variable. No problem.
Try it!
double dada = 10.7;
/* run jump play */
dada = 3.141592653589;
However, what makes more sense is to use an array.
declare a double array -
double[] myNumbers = {28.3, 21.2};

Create A Method In Java

Hello I am trying to create a method in Java that Accepts an integer from the user. Calculate and display how many occurences of the integer are in the array(i'm Creating a random array) as well as what percentage of the array values is the entered integer.
This is how i create my Array:
public void fillVector ( )
{
int myarray[] = new int [10];
for (int i = 0 ; i < 10 ; i++)
{
myarray [i] = (int) (Math.random () * 10);
}
}
Any sugestions how can i do to accomplish this ?
This seems like a homework to you so I am not gonna give you the full solution but I will break down the steps of what you need to do in order to solve your problem. You have to find out how to code those steps yourself, or at least provide some code and your specific problem because your question is too vague right now.
Ask the user to input the number.
Store that number somewhere.
Check each cell of the array for that number. If you find one appearance
increase the counter and continue until the end of your index.
Print out the appearances of the given number.
Print out the percentage of the cells containing the given value to the total amount of cells.
As I can see from your code (if it's yours) you are capable to pull this off on your own. It shouldn't be too hard.

Categories