Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java-Arrays & Files Help

    How would i get to do a two deminsional array to work and be defined in at least one line. Much like normal arrays such as

    String[] zero={"Hi"};

    but i want it to be String[][] and so forth. Also i need help with understanding how to read and write files along with creating to. I want to make a saved file for a game and the data is stored in like a .txt each variable on a certain line and such. Thanks for you help.

  • #2
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey,, mm I don't think you need to use an Array for that
    You can use Java's Serializable for saving variables to a file
    Let me know if you need examples

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey,

    Creating it is easy...

    String[][] whatev=new String[length1][length2];

    but to set each one with a value, you need to use a 2 dimentional for loop:

    Code:
    for(int i=0;i<whatev.length;i++){
    for(int j=0;j<whatev[0].length;j++){
    
    whatev[i][j]="newvalue";
    
    }
    }
    As for reading and writing files... Let me consult my game book...
    ahhh: (by the way, the method you make for opening files
    throws an IOException)

    Code:
    public void readFile(String filename) throws IOException{
    
    BufferedReader reader=new BufferedReader(new FileReader(filename));
    
    //so then you have some methods:
    
    String line=reader.readLine();
    reader.close();
    
    }
    I suppose replace FileReader with FileWriter and the method would change
    to writeLine("blah"); if you wanted to write to files... Creating files
    i cant help you there, but if you look up BufferedReader objects on google
    youll find it...

    hope that helped
    UNDER CONSTRUCTION

  • #4
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I suppose that is helpfull but how would i get lets say save.txt lines into int variables.

    if save.txt was like

    4
    5
    3
    2

    how would i get the first line 4 into a int variable named a or something also getting different lines into different variables. Thanks

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    like so...

    Code:
    String thenum = reader.readLine();
    if(thenum!=null){
    
    int num1=Integer.parseInt(thenum);
    
    }
    parseInt turnes a string in to an integer... and thenum will equal null if
    there are no more lines to read
    UNDER CONSTRUCTION

  • #6
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think that helps. Here is a program i am working on to test some information out
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Contacts implements ActionListener {
    	JFrame contactsFrame;
        JPanel contactsPanel;
        JTextField name, lname, phonenum;
        JLabel names, lnames, phonenums;
        JButton next, back, add, edit;
        String[] data={"","",""}, data2=new String[6];
        int a=0;
        
            public Contacts() {
            //Create and set up the window.
            contactsFrame = new JFrame("Contacts Manager Program");
            contactsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            contactsFrame.setSize(3020, 40);
    
            //Create and set up the panel.
            contactsPanel = new JPanel(new GridLayout(5, 2));
    
            //Add the widgets.
            addWidgets();
    
            //Add the panel to the window.
            contactsFrame.getContentPane().add(contactsPanel, BorderLayout.CENTER);
    
            //Display the window.
            contactsFrame.pack();
            contactsFrame.setVisible(true);
        }
        
        private void addWidgets() {
            //Create widgets.
            name = new JTextField(data[a]);
            lname = new JTextField(data[a+1]);
            phonenum = new JTextField(data[a+2]);
            names = new JLabel("Name:");
            lnames = new JLabel("Last Name:");
            phonenums = new JLabel("Phone Number:");
            next = new JButton("Next");
            back = new JButton("Back");
            add = new JButton("Add new Contact");
            edit = new JButton("Save Changes");
    
            //Listen to events from the Convert button.
            next.addActionListener(this);
            back.addActionListener(this);
            add.addActionListener(this);
    
            //Add the widgets to the container.
            contactsPanel.add(names);
            contactsPanel.add(name);
            contactsPanel.add(lnames);
            contactsPanel.add(lname);
            contactsPanel.add(phonenums);
            contactsPanel.add(phonenum);
            contactsPanel.add(back);
            contactsPanel.add(next);
            contactsPanel.add(add);
            contactsPanel.add(edit);
        }
        
        
    	public void actionPerformed(ActionEvent event) {
    		if(event.getSource()==add) {
        	data2[data2.length-3]=name.getText();
    		data2[data2.length-2]=lname.getText();
    		data2[data2.length-1]=phonenum.getText();
    		data=data2;
    		data2=new String[data.length+3];
    		for(int i=0;i<data.length-1;i++)
    		{
    			data2[i]=data[i];
    			
    		}
        	}
        	if(event.getSource()==next) {
        		if (a!=data.length-1) {
        			a=a+3;
        	}
        	name.setText(data[a]);
        	lname.setText(data[a+1]);
        	phonenum.setText(data[a+2]);
        	}
        	if(event.getSource()==back) {
        		if(a!=0){
        			a=a-3;
        		}
        	name.setText(data[a]);
        	lname.setText(data[a+1]);
        	phonenum.setText(data[a+2]);
        	}
        	if(event.getSource()==edit) {
        	data[a]=name.getText();
    		data[a+1]=lname.getText();
    		data[a+2]=phonenum.getText();
    		data2[a]=name.getText();
    		data2[a+1]=lname.getText();
    		data2[a+2]=phonenum.getText();
        	}
    		}
    	
    	
        private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
    
            Contacts converter = new Contacts();
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    All that is technically needed for it to write to a file needs to be in the one if statment for the button edit all it needs to do is save the changes to the file and to the data. Data 2 is used for the add part so that needs to be changed. The edit button just saves the changes made in the current information displayed to the two lists but puts the new list data into the file. it should be written so that it has one string information per line like

    Bob
    Bob
    9-999-999-9999

    Also at the begining it needs to be able to open the file and read the information into data and data2 but data2 with 3 open spaces at the end. Any help with this? I know this is asking alot for that but i will be very thankfull to the person who helps me. Also if you are going to edit the 2 small parts please use comments so i can understand what it is doing and i don't have to do this again. Also please remeber this is going to be inside a jar file with the document outside of it so that is can be easily transfered from person to person if need be.
    [*edit*]One more thing there is a problem with i believe the next and back button in finding out if it should add or not because it keeps going over the limit. Thanks

  • #7
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any help?


  •  

    Posting Permissions

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