Search Results

Search found 6 results on 1 pages for 'hatboysam'.

Page 1/1 | 1 

  • Problem storing text to a String[] and then storing the array to a Vector.

    - by hatboysam
    OK so for background I've only been using Java for a little more than a week and I am making a basic GUI "Recipe Book" application just to test out some of the techniques I've learned. Currently I'm working on the "Add New Recipe" page that allows the user to make a new recipe by adding ingredients one at a time. The ingredients have 3 attributes: Name, amount, and unit (like cups, oz, etc) that come from two text fields and a combo box respectively. The 3 values are stores as strings to a String array that represents the ingredient, then the arrays are stored in a vector that holds all of the ingredients for one recipe. When an ingredient is added, the user first types in the 3 necessary values into empty boxes/combo box dropdown and then clicks "Add Ingredient". Here is my code for when the button is clicked: public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == addIngredientButton) { //make sure 3 ingredient fields are not blank if (!ingredientNameField.getText().equals("") && !ingredientAmountField.getText().equals("") && !ingredientUnitDropdown.getSelectedItem().equals("")) { tempIngredientsArray[0] = ingredientNameField.getText(); //set ingredient name to first position in array tempIngredientsArray[1] = ingredientAmountField.getText(); //set ingredient amount to second position in array tempIngredientsArray[2] = (String) ingredientUnitDropdown.getSelectedItem(); //set ingredient unit to third position in array int ingredientsVectorSize = tempRecipe.ingredientsVector.size(); tempRecipe.ingredientsVector.add(this.tempIngredientsArray); //why would it matter if this and previous statement were switched for (int k = 0; k < ingredientsVectorSize + 1; k++ ) { liveIngredientsListArea.append("\n" + tempRecipe.makeIngredientSentence(k)); System.out.println(Arrays.toString(tempIngredientsArray)); //shows that tempIngredientsArray gets messed up each time } } Now here's the really strange problem I've been having. The first time the user adds an ingredient everything works fine. The second time, the String[] for that ingredient seems to be duplicated, and the third time it's triplicated. Aka the first time it runs the System.out.println might return "{Sugar, 3, Cups}" and the second time it will return "{Flour, 2, Oz.} {Flour, 2, Oz.}" etc. What's causing this strange problem? All help greatly appreciated, let me know if you need any clarification.

    Read the article

  • Java - strange problem storing text to a String[] and then storing the array to a Vector. Help plea

    - by hatboysam
    OK so for background I've only been using Java for a little more than a week and I am making a basic GUI "Recipe Book" application just to test out some of the techniques I've learned. Currently I'm working on the "Add New Recipe" page that allows the user to make a new recipe by adding ingredients one at a time. The ingredients have 3 attributes: Name, amount, and unit (like cups, oz, etc) that come from two text fields and a combo box respectively. The 3 values are stores as strings to a String array that represents the ingredient, then the arrays are stored in a vector that holds all of the ingredients for one recipe. When an ingredient is added, the user first types in the 3 necessary values into empty boxes/combo box dropdown and then clicks "Add Ingredient". Here is my code for when the button is clicked: public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == addIngredientButton) { //make sure 3 ingredient fields are not blank if (!ingredientNameField.getText().equals("") && !ingredientAmountField.getText().equals("") && !ingredientUnitDropdown.getSelectedItem().equals("")) { tempIngredientsArray[0] = ingredientNameField.getText(); //set ingredient name to first position in array tempIngredientsArray[1] = ingredientAmountField.getText(); //set ingredient amount to second position in array tempIngredientsArray[2] = (String) ingredientUnitDropdown.getSelectedItem(); //set ingredient unit to third position in array int ingredientsVectorSize = tempRecipe.ingredientsVector.size(); tempRecipe.ingredientsVector.add(this.tempIngredientsArray); //why would it matter if this and previous statement were switched for (int k = 0; k < ingredientsVectorSize + 1; k++ ) { liveIngredientsListArea.append("\n" + tempRecipe.makeIngredientSentence(k)); System.out.println(Arrays.toString(tempIngredientsArray)); //shows that tempIngredientsArray gets messed up each time } } Now here's the really strange problem I've been having. The first time the user adds an ingredient everything works fine. The second time, the String[] for that ingredient seems to be duplicated, and the third time it's triplicated. Aka the first time it runs the System.out.println might return "{Sugar, 3, Cups}" and the second time it will return "{Flour, 2, Oz.} {Flour, 2, Oz.}" etc. What's causing this strange problem? All help greatly appreciated, let me know if you need any clarification.

    Read the article

  • How do I dynamically name objects in Java?

    - by hatboysam
    Let's say I needed to make a series of String[] objects. I know that if i wanted to make a string array called "test" to hold 3 Strings I could do String[] test = new String[3]; But let's say I needed to make a series of these arrays and I wanted them to be named, 1,2, 3, 4, 5... etc. For however many I needed and I didn't know how many I'd need. How do I achieve a similar effect to this: for (int k=0; k=5; k++){ String[] k = new String[3]; } Which would created 5 string arrays named 1 through 5. Basically I want to be able to create array objects with a name detemined by some other function. Why can't I seem to do this? Am I just being stupid?

    Read the article

  • Reflection for Class of generic parameter in Java?

    - by hatboysam
    Imagine the following scenario: class MyClass extends OtherClass<String>{ String myName; //Whatever } class OtherClass<T> { T myfield; } And I am analyzing MyClass using reflection specifically (MyClass.class).getDeclaredFields(), in this case I will get the following fields (and Types, using getType() of the Field): myName --> String myField --> T I want to get the actual Type for T, which is known at runtime due to the explicit "String" in the extends notation, how do I go about getting the non-genetic type of myField?

    Read the article

  • Rails: Thread won't affect database unless joined to main Thread

    - by hatboysam
    I have a background operation I would like to occur every 20 seconds in Rails given that some condition is true. It kicked off when a certain controller route is hit, and it looks like this def startProcess argId = self.id t = Thread.new do while (Argument.isRunning(argId)) do Argument.update(argId) Argument.markVotes(argId) puts "Thread ran" sleep 20 end end end However, this code does absolutely nothing to my database unless I call "t.join" in which case my whole server is blocked for a long time (but it works). Why can't the read commit ActiveRecords without being joined to the main thread? The thread calls methods that look something like def sample model = Model.new() model.save() end but the models are not saved to the DB unless the thread is joined to the main thread. Why is this? I have been banging my head about this for hours.

    Read the article

  • Java: How to store Vector<String[]> in XML (or save in any other way)

    - by hatboysam
    Basically I have a proof-of-concept application that is a digital recipe book. Each Recipe is an object and each object has, among other fields, a Vector containing arrays. The Vector is the list of all ingredients in the Recipe while each ingredient has an array showing the name of the ingredient, the amount, and the unit for that amount. I want to save each Recipe to XML so that they can be accessed by the user. How can I store a Vector of String arrays in XML or any other sort of file so that it can later be recalled and accessed?

    Read the article

1