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

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

Related

Can't round decimals to 4 decimal points in java for gravity program

So, I'm trying to get the decimals in a table I have for a gravity program that I created to round after 4 decimals. However, whenever I've tried to round it, it either will print the table without rounding, or it will start to print the table, and then throw an error at me.
I'm sure what I doing wrong, and I've tried to look online for help, but it hasn't helped.
I have a few lines commented out below to show where I have tried to round to 4 decimals places, but hasn't worked.
I'll post my code below to see if you guys could please help me figure out what I'm doing wrong.
public class Gravity
{
public static void main(String[] args)
{
Gravity g = new Gravity();
System.out.printf("%5s%20s%20s\n", "Time", "Distance Earth", "Distance Moon");
// Gravity.format()
// DecimalFormat df = new DecimalFormat("###.####");
// System.out.println(df.format(g));
for (int i = 1; i <= 10; i++)
{
System.out.printf("%5d%20fm%20fm\n", + i,g.distanceFallen(i, 9.8), g.distanceFallen(i, 1.625));
// System.out.format("%.4f", i);
}
}
/* private static Gravity format(String string, int i)
{
//
return i;
}*/
public double distanceFallen (double time, double gAcc)
{
// System.out.format("%.4f");
// System.out.format("%.4f", time);
return (0.5)*gAcc*Math.pow(time, 4);
}
}
EDIT: Also, here's what the table looks like, just to make clear up any potential confusion.
Time Distance Earth Distance Moon
1 4.900000m 0.812500m
2 78.400000m 13.000000m
3 396.900000m 65.812500m
4 1254.400000m 208.000000m
5 3062.500000m 507.812500m
6 6350.400000m 1053.000000m
7 11764.900000m 1950.812500m
8 20070.400000m 3328.000000m
9 32148.900000m 5330.812500m
10 49000.000000m 8125.000000m
Change your formatting to:
"%5d%20.4fm%20.4fm\n"
You'll want to change your header formatting to 24 instead of 20 too.

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

Give a checkbox an integer value and store in JavaDB

I am currently working on a small program that allows the ordering of items and the total cost to be stored in a JavaDB.
I have managed to successfully store integer values into the database but the cost amount for the items are not correct. It seems like when the check box is selected it is counting that as a value on top of the set value. Would anyone be able to take a look and see where I am going wrong?
Any help would be greatly appreciated!
Screenshot:
http://i.imgur.com/ad41ooG.png
OK - I think I can see your problem. Reducing the code a bit further, you have:
int fanta = 1;
int crisps = 1;
int beer = 2;
int wine = 3;
int water = 0;
...
if(btnFanta.isSelected()) fanta++;
if(btnCrisps.isSelected()) crisps++;
if(btnBeer.isSelected()) beer++;
if(btnWine.isSelected()) wine++;
if(btnWater.isSelected()) water++;
rs.updateInt("TotalCost", fanta + crisps + beer + wine + water);
So, if nothing is selected, you're adding up all the values: 1 + 1 + 2 + 3 + 0 = 7, and inserting that. If Fanta is selected, you first increment the value of fanta, making it 2, then add it to the values of all the other snacks: 2 + 1 + 2 + 3 + 0 = 8, which is what you're seeing in the database when you select Fanta.
Hopefully that makes sense, and I'm guessing that's not what you want. I'd suggest that one solution would be to add a new variable, say, totalCost, and add the values to that if the relevant checkbox is selected, so, you'd end up with something like:
int fanta = 1;
int crisps = 1;
...
int totalCost = 0;
...
if(btnFanta.isSelected()) totalCost += fanta;
if(btnCrisps.isSelected()) totalCost += crisps;
...
rs.updateInt("TotalCost", totalCost);
(In case you're not familiar with the += syntax, it's the same as saying totalCost = totalCost + fanta, i.e. you're adding fanta to whatever totalCost was previously.)

multiply user input through JTextField with an array of double values

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");
}

pathfinding search crash if distance is longer than 2

The assignment is to create a game of reversi. I have it working except for moves that involve changing more than one chip
x o
o o
o o o
# <-- the # is x's move that crashes the game.
, in which case the program crashes. The crash takes place somewhere in isPlayable().
Where am I going wrong?
//there are 7 more such methods for different directions
// (down, left, right, diagonals).
public void searchN(int x, int y, int increment){
if(x-increment>=0){
if(this.isPlayed(x-increment,y)){
if(increment>1 && chips[x-increment][y]==turnColor){
//turnColor is an int value 1 or 2
// (0 represents unplayed space in chips[][])
// 1 corresponding to white, 2 corresponding to black.
playable[0] = true;
leads[0] = false;
} else {
if(chips[x-increment][y]!=turnColor){
leads[0]=true;
}
}
}else
leads[0]=false;
}else
leads[0]=false;
}
public boolean isPlayable(int x, int y){
this.searchN(x,y,1); //7 other directions are searched
while(leads[0]||leads[1]||leads[2]||leads[3]||leads[4]
||leads[5]||leads[6]||leads[7]){
int i = 2;
if(leads[0]) // 7 other directions are searched given that their marker is true.
this.searchN(x,y,i);
}
if(playable[0]||playable[1]||playable[2]||playable[3]||playable[4]
||playable[5]||playable[6]||playable[7])
return true;
else
return false;
}
Per your comment, it sounds like you're experiencing a hang, not a crash. When a program hangs, you should look for places where the program can get indefinitely "stuck". The prime suspect in isPlayable is your while loop. As long as any of the eight booleans are true it will never complete.
I would add some logging so you can see what's happening:
while(leads[0]||leads[1]||leads[2]||leads[3]||leads[4]
||leads[5]||leads[6]||leads[7]){
System.out.println("leads[0]: " + leads[0]);
System.out.println("leads[1]: " + leads[1]);
// etc.
int i = 2;
if(leads[0]) // 7 other directions are searched given that their marker is true.
this.searchN(x,y,i);
}
Once you've verified that this is the problem, start looking into your search methods to figure out why its happening.

Categories