Results 1 to 7 of 7
  1. #1
    fiefa is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default Multiplying specific data within a arraylist

    Hey, I'm currently stuck trying to figure out how to multiply the data in the, this.severity = Double.parseDouble(strArr[4]);, and this.damages = Double.parseDouble(strArr[2]);. The multiplying would take place under the ChangeListener .Any help would be grateful I'm guessing its something simple I'm just over looking.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import javax.swing.plaf.ColorUIResource;
    import java.lang.*;
    import java.util.Collections;
    
    public class guiFinal extends JFrame {
       private JButton    Load  = new JButton("Load");
       private JButton    Reset  = new JButton("Reset");
       private JButton    Change  = new JButton("Change");
       private JButton    quitButton      = new JButton("Quit");
       Scanner reader = new Scanner(System.in);
       String input1 = "";//input from first dialog box
    
    public class Australia {
    	String cityName;
    	String population;
    	double damages;
    	String earlierRelief;
    	double severity;
    	String allottedReliefPersonnel;
    
    
    	public Australia(String[] strArr) {
    		this.cityName = strArr[0];
    		this.population = strArr[1];
    		this.damages = Double.parseDouble(strArr[2]);
    		this.earlierRelief = strArr[3];
    		this.severity = Double.parseDouble(strArr[4]);
    		this.allottedReliefPersonnel = strArr[5];
    	}
    	public String toString() {
    		return "City name: " + cityName + ", Population: " + population + ", Damages: " + damages + ", Earlier Relief: " + earlierRelief + ", Severity: " + severity+ ", Allotted Relief Personnel: " + allottedReliefPersonnel ;
    	}
    }
    
    	public void experiment() {
    
    
    	}
    
       public guiFinal(){
          JPanel centerPanel = new JPanel(new GridLayout(4, 1));
          JPanel northPanel = new JPanel(new GridLayout(1, 3));
          JPanel westPanel = new JPanel(new GridLayout(8, 1));
          JPanel eastPanel = new JPanel(new GridLayout(4, 1));
          JPanel southPanel = new JPanel(new GridLayout(1, 4));
          Container container = getContentPane();
          container.add(westPanel, BorderLayout.WEST);
          container.add(northPanel, BorderLayout.NORTH);
          container.add(southPanel, BorderLayout.SOUTH);
          container.add(eastPanel, BorderLayout.CENTER);
          eastPanel.add(Load);
          eastPanel.add(Reset);
          eastPanel.add(Change);
          eastPanel.add(quitButton);
          Load.addActionListener(new LoadListener());
          Reset.addActionListener(new ResetListener());
          Change.addActionListener(new ChangeListener());
          quitButton.addActionListener(new exitListener());
    
       }
                       public class LoadListener implements ActionListener{
          public void actionPerformed(ActionEvent e){
          	ArrayList<Australia> damageReports = new ArrayList<Australia>();
    		File x = new File(
    				"C:/Users/James/Documents/JCreator LE/MyProjects/Final/src/something.txt");
    		try {
    			BufferedReader in = new BufferedReader(new FileReader(x));
    			String line;
    			while ((line = in.readLine())!= null) {
    				String[] listgrab = line.split(",");
    				Australia p = new Australia(listgrab);
    				damageReports.add(p);
    				System.out.println(p.toString());
    			}
    		in.close();
    		}
    		catch (Exception fe)
    		{
    			System.out.print("Wrong data has been inputed into the program, Please do not do that again");
    		}
    	}
        }
    
    
    
                       private class ChangeListener implements ActionListener{
          public void actionPerformed(ActionEvent e){
          	try{
          	    input1 = JOptionPane.showInputDialog
          ( "What percentage would you like to increase the damage by?" );
            double input2 = Double.parseDouble(input1);// retriving the number to change the percentage by
            
    
    
          }catch(Exception asdf){
    javax.swing.JOptionPane.showMessageDialog(null,"Please use numbers only");
        }
          }
       }
                       private class ResetListener implements ActionListener{
          public void actionPerformed(ActionEvent e){
          	try{
          	    input1 = JOptionPane.showInputDialog
          ( "Lists have been cleared" );
          }catch(Exception asdf){
    javax.swing.JOptionPane.showMessageDialog(null,"Please use numbers only");
        }
          }
       }
    
                    private class exitListener implements ActionListener{
          public void actionPerformed(ActionEvent e){
            System.exit(0);
          }
       }
    
       public static void main (String [] args){
            guiFinal theGUI = new guiFinal();
            theGUI.setTitle("James Tiitson");
            theGUI.setSize(500, 350);
            theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            theGUI.setVisible(true);
        }
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Not sure what you're trying to do or where.
    You've got the damages and severity as doubles, so multiply them.

  3. #3
    fiefa is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    How would you actually multiply them though is my question. Not sure how to multiply them by another double which lets say is equal to 2.

    If i want to go about multiplying damage by another double variable with a value of 2.0 how would i do that?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by fiefa View Post
    How would you actually multiply them though is my question. Not sure how to multiply them by another double which lets say is equal to 2.

    If i want to go about multiplying damage by another double variable with a value of 2.0 how would i do that?

    I'm also not sure what your current problem is. To multiply damages by 2, you'd do something like:

    Java Code:
    damages = damages * 2;

    or more succinctly:

    Java Code:
    damages *= 2;
    Perhaps you can tell us precisely what is not working.

  5. #5
    fiefa is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    The program is suppose to be able to take in values from a txt file for information about Australia's flooding that is occurring. Now my problem is whenever I try to get the users number from the Changelistener it captures it, but i cannot change the data in the array i get the following error:

    Java Code:
    cannot find symbol variable damages
    All that i am trying to do is modify all the data in the
    Java Code:
    this.damages = Double.parseDouble(strArr[2]);
    to being double or triple or etc that the user might input into the box.
    All im trying to do is just simply multiply the values that are contained in that array by the users input

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Furbarable told you what to do. FYI there is no 'triple'.

    If you are trying to modify the original array, there are several steps that need to be taken. Read the value in the array. Convert it to a double. Multiply it by 2. Convert it back to a string. Replace the original value.

    Do you know how to:
    1. Read from an Array
    2. Parse a Double
    3. Multiply and store
    4. Convert a double to a String
    5. Save an item in an array?

    If you know how to do all of those things, do them in that order, and you're done. If you don't, pleas tell use where your confusion lies.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Show us the error.
    Show us the code.
    Point out which bit of code is giving the error.

    Anything else involves us guessing, and that usually ends up with incorrect answers.

Similar Threads

  1. Replies: 2
    Last Post: 05-01-2010, 10:16 PM
  2. Retrieving specific objects from ArrayList
    By soketti in forum New To Java
    Replies: 49
    Last Post: 10-29-2009, 08:10 AM
  3. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 10:07 PM
  4. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:24 AM
  5. finding Operating System specific data
    By FrankyDee in forum New To Java
    Replies: 2
    Last Post: 09-28-2007, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •