I wasn't too sure what to title this question, apologies in advance. I currently have a value of say 50 stored in the BidderArray. I want to be able to increase that 50 by any given number entered into a text field.
So say I want to add 10 to the existing 50, it will return 60. Currently when I add the 10 the 50 is replaced by 10 instead of adding the two together. I understand why my code is doing this but haven't been able to find any tutorials or hints on what I should be doing instead.
Here is the code:
package abc;
import java.awt.*;
public class Funds extends javax.swing.JFrame {
int i = 0;
Bidder bidbal = new Bidder();
/** Creates new form Funds */
public Funds() {
initComponents();
}
private void addActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int f = 0;
boolean validEntries = true;
try{
f = Integer.parseInt(amount.getText());
Bidder.BidderArray.get(i).setRegFee(f);
} catch (Exception error) {
validEntries = false;
amount.setBackground(Color.red);
}
if (validEntries) {
Bidder.exportBidder();
Home home = new Home();
home.setVisible(true);
this.dispose();
}
}
}
You aren't actually adding anything
Bidder.BidderArray.get(i).setRegFee(f);
is apparently just setting something to f, you have to get the current value, add to it, and then put it back. But this is just a guess as we don't have enough actual code to know what you are doing wrong.
You have to get the current fee, add the value, and then set the fee:
f = Integer.parseInt(amount.getText());
Bidder.BidderArray.get(i).setRegFee( f + Bidder.BidderArray.get(i).getRegFee() );
Or you could add a new method the Bidder class that adds to the fee:
class Bidder
{
//...
public void addToRegFee( int amount )
{
this.regFee += amount;
}
}
f = Integer.parseInt(amount.getText());
Bidder.BidderArray.get(i).addToRegFee( f );
f = Integer.parseInt(amount.getText());
Bidder.BidderArray.get(i).setRegFee(f);
Here, it seems like you're getting the user's input (f), and just set the array's element value to it. What it sounds like you want to do is to take that input (f), and the array's element existing value, combine (read: add) them, before setting the element's value to that new combined value.
In pseudo-code, this is what you're doing:
f := get user's input
setValue(f)
What you need to do:
f := get user's input
g := get current value
setValue(f + g)
You must add it to your old value:
//Add old to new (Unless you have a different method set for get like
//getRegFee (Not sure how bidder is implemented))
Bidder.BidderArray.get(i).setRegFee(Bidder.BidderArray.get(i) + f);
Related
so this is the main code for my text-based game.
import java.util.Scanner;
public class D_M_RPG {
public static void main(String[] args) {
//Creating the class to call on my toolbox
D_M_RPGtoolbox toolbox = new D_M_RPGtoolbox();
//Creating the scanner class for user input
Scanner input = new Scanner(System.in);
//Initiating variables and final variables aswell as arrays
//token variable to validate open spots in an array
int slotCounter = 0;
int inventoryExpander = 11;
//First initiated will be the character creation variables
String hairColor = "";
String eyeColor = "";
String skinColor = "";
String gender = "";
//Initiating the arrays for character inventory slots
String[] weaponSlots = new String[10];
//initiating the arrays for the character creation
String[] hairColorARR = {"black","Green","Yellow","Brown","Blue","Blonde","Grey","White"};
String[] eyeColorARR = {"Green","Brown","Blue","Grey",};
String[] skinColorARR = {"White","brown","Black",};
String[] genderARR = {"Male","Female"};
//Creating the introduction title and introduction
System.out.println("Welcome to, COLD OMEN.");
System.out.println("\nNOVEMBER 12th, 2150: ONTARIO, CANADA");
System.out.println("\nYou hear loud shouts and gun fire all around you but can't pinpoint the location of anything, you feel a bit dazed until someone grabs you and you open your eyes and snap out of it.");
System.out.println("\nUnknown: 'Get up, its time to move out. Take this.'");
System.out.println("\nUnknown hands you a 'M4-A4 RIFLE'");
System.out.println("\nyou manage to catch a small glimpse of him before you get up.");
//Character creation screen
System.out.println();
//ONLY WORKS ONCE WONT INCREMEMENT THE SLOTCOUNTER
toolbox.insert(weaponSlots, slotCounter, inventoryExpander, "M4-A4 RIFLE");
System.out.println("\n" + weaponSlots[0]);
toolbox.insert(weaponSlots, slotCounter, inventoryExpander, "ak47");
System.out.println(weaponSlots[0]);
}
}
so I have this method I made to basically add an "item" to the weaponSlots array (the inventory) but whenever I run it it will add to the first element in the array [0] but it wont incremement the slotcounter which should go up by one every time the method is used so that I dont replace any items in the array It should just add items until its full which is checked using the inventoryExpander variable. at the moment I have it printing the element at 0 and 0 for the array but i have checked 1 aswell and 1 is just null no item added it only just replaces the element at 0. heres the code for the method to increment etc:
public class D_M_RPGtoolbox {
//method for random number generating to be used for crit hits, turns, loot generation etc
public int randomGen(){
int x = (int) (Math.random()*((20-0)+1)+0);
return x;
}
//method for inserting into an array ONLY WORKS ONCE WONT INCREMEMENT THE SLOTCOUNTER FIX
public void insert(String[] a, int b, int d , String c) {
if(b < d) {
a[b] = c;
b++;
}//end of if statement
}//end of method
}
What you are actually performing the ++ operation on in b is a copy of the value in slotCounter.
The variable slotCounter is passed into insert "by-value".
This unlike what you probably imagine, that it is passed "by-reference".
One solution would be to do the slotCounter++ from the call row instead; and another would be to let the toolbox own the slotCounter variable completely.
This question uses the image of passing a copy of document content (by value) where changes to the document would not be seen by the sender; or as a link to a shared document (by reference), where changes could be made to the same page that the sender sees.
Its always going to be zero since you are passing zero and incrementing the local variable b.
Try calling the method as below with post increment ++ to slotCounter and see if it works for you,
toolbox.insert(weaponSlots, slotCounter++, inventoryExpander, "M4-A4 RIFLE");
This question already has answers here:
Can you recommend a Java library for reading (and possibly writing) CSV files? [closed]
(8 answers)
Closed 6 years ago.
So I have received quite a few tips and acquired some resources for learning Java since joining this community. I have now reached week 6 in my class and am working through my third project. I feel like I'm learning a lot but I also have a long road ahead if I want to master Java.
My question this time is how do I get my code to save more than one output to file?
Part of my current project is to do the following:
"When the window is closed, the efficiency values should be computed with >values of n from 0 to 10 and written to a file. Each line of the file > >should contain the value of n, the efficiency of the iterative method for >that value of n and the efficiency of the recursive method. The values >should be separated by commas so the file can be opened with Excel."
I have managed to get the program to write a single entry into the output file. However,I either made an error in the code or missing something critical. Can someone point me to the correct solution? I think I may have to create an array, store the outputs there, then output the array to csv. I have looked at roseindia and viralpatel but those didn't reveal what I was hoping.
Sequence (part I'm screwing up)
package cmisproject3;
public class Sequence {
private static int efficiency = 0;
// method to compute iterative
public static int computeIterative(int n) {
int result = 0;
efficiency = 0;
if (n == 0) {
result = 0;
} else if (n == 1) {
result = 1;
} else {
int secondPrevious = 0;
int previous = 1;
for (int i = 2; i <= n; i++) {
efficiency++;
result = 2 * previous + secondPrevious;
secondPrevious = previous;
previous = result;
}
}
return result;
}
// method to comopute recursive
public static int computeRecursive(int n) {
efficiency = 0;
return computeRecursiveHelper(n);
}
private static int computeRecursiveHelper(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
efficiency++;
return 1;
} else {
efficiency++;
return 2 * computeIterative(n - 1) + computeIterative(n - 2);
}
}
public static int getEfficiency() {
return efficiency;
}
}
GUI (nailed it?)
package cmisproject3;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class CMISProject3 extends JFrame implements ActionListener {
private final int TWICE = 2;
private JLabel jLabel1 = new JLabel(), jLabel2 = new JLabel(), jLabel3 = new JLabel(), jLabel4 = new JLabel(), jLabel5 = new JLabel(), jLabel6 = new JLabel();
private ButtonGroup radioButtons = new ButtonGroup();
private JRadioButton iterativeBtn = new JRadioButton(), recursiveBtn = new JRadioButton();
private JTextField enterN = new JTextField(16), textResult = new JTextField(16), textEfficiency = new JTextField(16);
private JButton computeBtn = new JButton();
private int efficiency;
private Sequence sequence;
private static FileWriter fileWriter;
private File file = new File("output.txt");
// Beginning of the constructor for the GUI
public CMISProject3() throws IOException {
sequence = new Sequence();
setSize(300, 200); // define size of GUI
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(6, 2));
getContentPane().add(jLabel4);
radioButtons.add(iterativeBtn);
iterativeBtn.setSelected(true); // sets Iterative as default GUI selection
iterativeBtn.setText("Iterative");
getContentPane().add(iterativeBtn);
getContentPane().add(jLabel5);
radioButtons.add(recursiveBtn);
recursiveBtn.setText("Recursive");
getContentPane().add(recursiveBtn);
jLabel1.setText("Enter n: ");
getContentPane().add(jLabel1);
getContentPane().add(enterN);
getContentPane().add(jLabel6);
computeBtn.setText("Compute");
computeBtn.addActionListener(this);
getContentPane().add(computeBtn);
jLabel2.setText("Result: ");
getContentPane().add(jLabel2);
getContentPane().add(textResult);
textResult.setEditable(false);
jLabel3.setText("Efficiency: ");
getContentPane().add(jLabel3);
getContentPane().add(textEfficiency);
textEfficiency.setEditable(false);
pack();
}
public void actionPerformed(ActionEvent event) {
int result;
efficiency = 0;
try {
fileWriter = new FileWriter(file);
} catch (IOException e1) {
e1.printStackTrace();
}
if (iterativeBtn.isSelected()) {
result = sequence.computeIterative(Integer.parseInt(enterN.getText()));
} else {
result = sequence.computeRecursive(Integer.parseInt(enterN.getText()));
}
try {
System.out.println(result);
fileWriter.write(result + ", " + sequence.getEfficiency());
} catch (IOException e) {
e.printStackTrace();
}
textResult.setText(Integer.toString(result));
textEfficiency.setText(Integer.toString(sequence.getEfficiency()));
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
CMISProject3 CMISProject3 = new CMISProject3();
CMISProject3.setVisible(true);
}
}
For those interested, here are the parameters I'm working within.
Instructions
You are reopening the file each time an action was performed without telling FileWriter to append instead of overwrite.
See:
https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.File,%20boolean)
I think you have a good start on your project. However, I see other problems apart from your question.
First I'll address your question then move on to other items. I assume when you say:
write a single entry into the output file
that you're saying you are able to write a single line to the file. So that would mean your question is: How can I write multiple lines to a file?
In that case you have at least two options. One is to setup your FileWriter to append rather than the default behavior of overwriting the existing file content.
Another option would be to avoid closing the FileWriter until you're finished writing. You could, for example, do this by moving the construction of your fileWriter to the constructor of your GUI and moving the call to the close method into an event handler that fires when the GUI closes.
Whatever you chose to do, you need to remember to write the newline character at the end of each line or else your file will be one very long line. So, modifying what you have now it would look like this:
fileWriter.write(result + ", " + sequence.getEfficiency()+"\n");
Other Issues:
Your Sequence.computeRecursiveHelper method is not recursive. A recursive method calls itself, yours does not do this.
I don't think you're following the instructions correctly. Perhaps you're just not finished yet and you intend to modify your code. If that is the case you can ignore what I'm about to point out. The instructions state:
When the window is closed, the efficiency values should be computed with values of n from 0 to 10 and written to a file.
You are currently writing to the file every time the user clicks the "Compute" button rather than doing the above. Also, you're not writing the correct data - you're writing the value you got based on the user's input not values obtained using n from 0 to 10.
i am trying to create such calculation within a texArea which i calculate in one class, and i pass this onto a different class to perform the next calculation with only clicking buttons. So if i click on 'm10py' , whatever i have in the TextArea will be decreased by 0.10. I tried everything but it just doesnt seem to work. It doesnt throw any errors but the total in the TextArea stays still.
My main class where it performs the basic calculation.
public void actionPerformed(ActionEvent e) {
double frontM = 9.50;
double middleM = 7.30;
double backM = 6.70;
double vipM = 12.90;
//double total1 = Double.parseDouble(output.getText());
if (frontR.isSelected() && e.getSource() == calculate) {
double total = Integer.parseInt(frontT.getText()) * frontM;
output.setText(pounds.format(total));
} else if (middleR.isSelected() && e.getSource() == calculate) {
double total = Integer.parseInt(middleT.getText()) * middleM;
String total2 = String.valueOf(total);
output.setText(total2);
} else if (backR.isSelected() && e.getSource() == calculate) {
double total = Integer.parseInt(backT.getText()) * backM;
output.setText(pounds.format(total));
} else if (vipR.isSelected() && e.getSource() == calculate) {
double total = Integer.parseInt(vipT.getText()) * vipM;
output.setText(pounds.format(total));
} else if (e.getSource() == cancel) {
frontT.setText("1");
middleT.setText("1");
backT.setText("1");
vipT.setText("1");
output.setText("");
}
if (e.getSource() == payment) {
Payment paymentJ = new Payment();
paymentJ.output.setText(output.getText());
}
}
Second class which the calculation is passed on to a different textArea. I didnt do it for every button because i couldnt manage to the calculation.. ;
public void actionPerformed(ActionEvent e) {
Main main = new Main();
// double totalR = Double.parseDouble(output.getText());
String cost = main.output.getText();
double cost2 = Double.parseDouble(cost);
double total;
total = cost2;
double m10py = 0.10;
double m20py = 0.20;
double m50py = 0.50;
double m1p = 1.00;
double m2p = 2.00;
double m5p = 5.00;
double m10po = 10.00;
double m20po = 20.00;
if (e.getSource() == m10p) {
total = total - m10py;
String total2 = String.valueOf(total);
output.setText(total2);
}
}
I'm fairly new to this so please dont go have a go at me. I just need to know what is wrong with this. Thanks
One thing I do know is that you've got a serious reference problem going on. For example in your first posted actionPerformed method:
if (e.getSource() == payment) {
Payment paymentJ = new Payment(); // ***** line 1 ****
paymentJ.output.setText(output.getText()); // ***** line 2 ****
}
On line 1 above you create a new Payment object, called paymentJ, and on line 2 you change its state, by calling output.setText(...). I'm guessing that output is some text component, and you're trying to change the text that it displays, but here's the problem -- while paymentJ refers to a Payment object, it's not the Payment object that is being displayed, which is a completely distinct separate object, and changing the state of the non-displayed one created here by trying to change the text it displays, will have no effect on the output text component in the actualy displayed Payment object.
Similarly in your second posted actionPerformed method:
Main main = new Main();
// double totalR = Double.parseDouble(output.getText());
String cost = main.output.getText(); // ***** line 1 ****
double cost2 = Double.parseDouble(cost); // ***** line 2 ****
On line 1 above you create a new Main object, called cost, and on line 2 you query its state, by calling output.getText(). But again the Main instance created here is not the same Main object that is being displayed, and again this means that you have at least two (or more) Main objects, only one of which is being displayed, and the data that your extracting from the one created locally here will not reflect the changes made to the one that's displayed. You can test this by placing a println after you extract the text, for example:
Main main = new Main();
// double totalR = Double.parseDouble(output.getText());
String cost = main.output.getText();
System.out.println("cost is currently: " + cost); // ***** add this ****
double cost2 = Double.parseDouble(cost);
I will bet that you'll see a default value that is held by the text component returned, and not a value that was entered by the user or was displaying in the currently visualized Main GUI.
What to do?
Well for one, you could make the output fields static. That would be a quick and easy solution, but unfortunately it would be quick, easy and very very wrong, since this would break OOPs principles, making your code very difficult to test, enhance and inherit.
Better would be to pass references in where needed, for instance pass a reference to the displayed Payment object into the object that has that first actionPerformed method, and then call the appropriate methods on that object, and likewise pass a valid reference to the displayed Main object into the object whose code is displayed in your lower code snippet. This will allow you to query and modify the states of valid displayed objects. How to do this? I can't tell you specifically how to do this without a better and working code example from you (as per my comments). Generally, you could pass references around using constructor and setter method parameters.
Best would be to make your code more M-V-C or Model-View-Controller like, but this may be overkill for this program and may be beyond your current level of coding at this time.
For more help, for better help, please improve your question.
Based on your new code,
your Payment class should extend JDialog, not JFrame since a GUI should only have one main window
You will want to pass Main into Payment via Payment paymenetJ = new Payment(this);
You will need to change the Payment constructor to accept this: public Payment(Main main)
And inside the constructor use the parameter to set a field: this.main = main;
Then use this main field instead of creating a new Main object.
I need to create a method which checks each element in my array to see if it is true or false, each element holds several values such as mass, formula, area etc for one compound, and in total there are 30 compounds (so the array has 30 elements). I need an algorithm to ask if mass < 50 and area > 5 = true .
My properties class looks like:
public void addProperty (Properties pro )
{
if (listSize >=listlength)
{
listlength = 2 * listlength;
TheProperties [] newList = new TheProperties [listlength];
System.arraycopy (proList, 0, newList, 0, proList.length);
proList = newList;
}
//add new property object in the next position
proList[listSize] = pro;
listSize++;
}
public int getSize()
{
return listSize;
}
//returns properties at a paticular position in list numbered from 0
public TheProperties getProperties (int pos)
{
return proList[pos];
}
}
and after using my getters/setters from TheProperties I put all the information in the array using the following;
TheProperties tp = new properties();
string i = tp.getMass();
String y = tp.getArea();
//etc
theList.addProperty(tp);
I then used the following to save an output of the file;
StringBuilder builder = new StringBuilder();
for (int i=0; i<theList.getSize(); i++)
{
if(theList.getProperties(i).getFormatted() != null)
{
builder.append(theList.getProperties(i).getFormatted());
builder.append("\n");
}
}
SaveFile sf = new SaveFile(this, builder.toString());
I just cant work out how to interrogate each compound individually for whether they reach the value or not, reading a file in and having a value for each one which then gets saved has worked, and I can write an if statement for the requirements to check against, but how to actually check the elements for each compound match the requirements? I am trying to word this best I can, I am still working on my fairly poor java skills.
Not entirely sure what you are after, I found your description quite hard to understand, but if you want to see if the mass is less than 50 and the area is greater than 5, a simple if statement, like so, will do.
if (tp.getMass() < 50 && tp.getArea() > 5) {}
Although, you will again, have to instantiate tp and ensure it has been given its attributes through some sort of constructor.
Lots of ways to do this, which makes it hard to answer.
You could check at creation time, and just not even add the invalid ones to the list. That would mean you only have to loop once.
If you just want to save the output to the file, and not do anything else, I suggest you combine the reading and writing into one function.
Open up the read and the write file
while(read from file){
check value is ok
write to file
}
close both files
The advantage of doing it this way are:
You only loop through once, not three times, so it is faster
You never have to store the whole list in memory, so you can handle really large files, with thousands of elements.
In case the requirements changes, you can write method that uses Predicate<T>, which is a FunctionalInterface designed for such cases (functionalInterfaces was introduced in Java 8):
// check each element of the list by custom condition (predicate)
public static void checkProperties(TheList list, Predicate<TheProperties> criteria) {
for (int i=0; i < list.getSize(); i++) {
TheProperties tp = list.get(i);
if (!criteria.apply(tp)) {
throw new IllegalArgumentException(
"TheProperty at index " + i + " does not meet the specified criteria");
}
}
}
If you want to check if mass < 50 and area > 5, you would write:
checkProperties(theList, new Predicate<TheProperties> () {
#Override
public boolean apply(TheProperties tp) {
return tp.getMass() < 50 && tp.getArea() > 5;
}
}
This can be shortened by using lambda expression:
checkProperties(theList, (TheProperties tp) -> {
return tp.getMass() < 50 && tp.getArea() > 5;
});
At the moment , when I hit F or f :
private static final char FILL_POLYGON_LOWERCASE = 'f';
private static final char FILL_POLYGON = 'F';
#Override
public void keyTyped(KeyEvent keyEvent)
{
PolygonFiller polyFiller = new PolygonFiller();
char key = keyEvent.getKeyChar();
switch(key)
{
/**
* Fill the polygons
*/
case FILL_POLYGON:
{
if (greenLightForFilling == true)
{
fillPolygon(polyFiller);
System.out.print("called");
}
break;
} // end FILL_POLYGON
case FILL_POLYGON_LOWERCASE:
{
if (greenLightForFilling == true)
{
fillPolygon(polyFiller);
}
break;
}
...
}
The program goes into fillPolygon(polyFiller); .
Meaning , when I hit for the first time f , I go into fillPolygon() .
How can I go into some other method , for example other() , when I hit f or F again ?
Thanks
So the thing is, if you click f/F you goto fill polygon, and pressing f/F again will call other().
This can be a classic case of Stateful Class.
Have an attribute in this at class level.
And on entering f/F check the value and increment it by one.
And on entering f/F again, check the value and increment it by one.
Before each increment you should check whether,
//Am assuming that there are more than two functions, else could use boolean
if (value == 1) {
fillpolygon();
}
else if (value == 2) {
other();
}
else if (value == 2) {
some_other();
}
Remember the entry point will be a single function, from there the flow is delegated based on checks similar to this.
Hope this helps.
Store the previously used button in a variable like 'currentCommand' and 'previousCommand'. Everytime you detect a new input you put current to previous and store the new one in the current member.
Or maybe if you want more than the last two key pressed use a stack.
How can I go into some other method, for example other(), when I hit f or F again ?
You need to introduce a boolean flag.
This is a very simple example of a state machine.