2 Replies - 1616 Views - Last Post: 30 January 2011 - 10:45 PM Rate Topic: -----

#1 fiefa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-January 11

variable has private access in filereader

Posted 30 January 2011 - 05:22 PM

Hey, I'm somewhat new to java and I cannot figure out how to solve this proble

damageReports has private access in filereader

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.*;

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

class Australia {
	String cityName;
	String population;
	String damages;
	String earlierRelief;
	String severity;
	String allottedReliefPersonnel;


	public Australia(String[] strArr) {
		this.cityName = strArr[0];
		this.population = strArr[1];
		this.damages = strArr[2];
		this.earlierRelief = strArr[3];
		this.severity = 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() {
		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);
			}
		in.close();
		}
		catch (Exception e)
		{
			System.out.print("Wrong data has been inputed into the program, Please do not do that again");
		}

	}

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

		filereader s = new filereader();
		s.experiment();

		System.out.println("Junk");
		System.out.println("--------------");

		for (Australia p : s.damageReports) {
			System.out.println(p.toString());
		}
	}
    }



                   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);
      }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");
        theGUI.setSize(500, 350);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theGUI.setVisible(true);
    }

}



thank you for your help

This post has been edited by fiefa: 30 January 2011 - 05:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: variable has private access in filereader

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: variable has private access in filereader

Posted 30 January 2011 - 10:33 PM

Make

 ArrayList<Australia> damageReports = new ArrayList<Australia>();



an instance variable rather than a method variable. As a method variable it goes out of scope when the method is exited.
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4405
  • View blog
  • Posts: 12,262
  • Joined: 18-April 07

Re: variable has private access in filereader

Posted 30 January 2011 - 10:45 PM

It would be helpful if you also showed your filereader() class. I assume this is your own custom class because you are attempting to access damageReports a member of the variable "s" which you have defined as filereader(). Keep in mind that this is not FileReader which is the Java IO class because you have not specified the capital letters. Java is case sensitive so filereader() and FileReader() are two different things.

;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1