Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By Tolls

Thread: Write to File trouble

  1. #1
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Write to File trouble

    Hi guys!
    Yeah… It’s me again. I have more troubles with my employee program. This time I would like to save data that variable “ansatte” contains, in own file. Explanation: “ansatte” is an arraylist of employee objects. Then I would like to read/open this file and create employee object some “tilsvarer” objects that we created before saving file.

    Program window:

    Write to File trouble-programwindow.png

    Here’s my “save to file” – code:
    Java Code:
     static void skrivUtTilFil() {
    		if (filvelger == null)
    			filvelger = new JFileChooser();
    
    		int resultat;
    		File filpeker;
    
    		resultat = filvelger.showSaveDialog(null);
    		if (resultat == JFileChooser.APPROVE_OPTION) {
    			filpeker = filvelger.getSelectedFile();
    			try {
    				
          FileOutputStream filStrom = new FileOutputStream(filpeker);
    
    				PrintWriter filSkriver = new PrintWriter(filStrom);
    
    				// It’s here I send ansatte arraylist to PrintWriter.
    					filSkriver.println(ansatte);
    
    				filSkriver.close();
    			} catch (FileNotFoundException e) {
    				JOptionPane.showMessageDialog(null,
    						"Du har ikke lov til ĺ skrive her!");
    			}
    		}
    	}
    and my «read from file» – code:
    Java Code:
     static void lesInnFraFil() {
    
    		if (filvelger == null)
    			filvelger = new JFileChooser();
    
    		int resultat;
    		File filpeker;
    
    		resultat = filvelger.showOpenDialog(null);
    
    		if (resultat == JFileChooser.APPROVE_OPTION) {
    
    			
    			filpeker = filvelger.getSelectedFile();
    			try {
    		FileInputStream filStrom = new FileInputStream(filpeker);				Scanner filleser = new Scanner(filStrom);
    
    
    				// Deletes eventually old data
    				tekstFelt.setText("");
    				tekstFelt2.setText("");
    				tekstFelt3.setText("");
    				tekstFelt4.setText("");
    				tekstFelt5.setText("");
    				ansatte.clear();
    
    // And HERE’s something WRONG! Error comes at line 226, which is colored red.
    				Employee nyAnsatt;
    				while (filleser.hasNext()) {
    			[COLOR="#FF0000"]nyAnsatt = new Employee(filleser.nextLine(),filleser.nextLine(),filleser.nextLine(),Integer.valueOf(filleser.nextLine()),new Room(filleser.nextLine()));[/COLOR]
    					ansatte.add(nyAnsatt);
    				}
    
    				filleser.close();
    
    			} catch (FileNotFoundException e) {
    				JOptionPane.showMessageDialog(null, "Finner ikke fila "
    						+ filpeker.getName());
    				e.printStackTrace();
    			}
    		}
    	}
    When I try to open my saved file it shows me multiple errors (I just copied all of crap):
    Java Code:
     
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "E-mail: damian.bratz@hotmail.com "
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:492)
    	at java.lang.Integer.valueOf(Integer.java:582)
    	at practice9.AnsatteLayout.lesInnFraFil(AnsatteLayout.java:226)
    	at practice9.LesinnLytter.actionPerformed(LesinnLytter.java:17)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:682)
    	at java.awt.EventQueue$3.run(EventQueue.java:680)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:696)
    	at java.awt.EventQueue$4.run(EventQueue.java:694)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    To deeper understanding, here’s my Employee-class:
    Java Code:
    public class Employee {
    String name;
    String email;
    int tlf;
    Room r_number;
    String lastname;
    
    public String getName() {
    	return name;
    }
    
    public String getLastname() {
    	return lastname;
    }
    
    public void setLastname(String lastname) {
    	this.lastname = lastname;
    }
    
    public void setName(String name) {
    	this.name = name;
    }
    
    public String getEmail() {
    	return email;
    }
    
    public void setEmail(String email) {
    	this.email = email;
    }
    
    public int getTlf() {
    	return tlf;
    }
    
    public void setTlf(int tlf) {
    	this.tlf = tlf;
    }
    
    public Room getR_number() {
    	return r_number;
    }
    
    public void setR_number(Room r_number) {
    	this.r_number = r_number;
    }
    
    
    	public Employee(String n, String ett, String em, int t, Room room) {
    		name = n;
    		lastname = ett;
    		email = em;
    		tlf = t;
    		r_number = room;
    	}
    	
    	public Employee(String n, String ett, String em) {
    		name = n;
    		lastname = ett;
    		email = em;
    	}
    
    	public Employee(String n, String ett, String em, int t) {
    		name = n;
    		lastname = ett;
    		email = em;
    		tlf = t;
    	}
    	
    	public String toString() {
    		return String.format("\nName: %s \nEtternavn: %s \nE-mail: %s \nTelefon Number: %s \nRoom: %s\n", name, lastname, email, tlf, r_number);
    	}
    }
    I would appreciate any help here, because I literally STUCK. Thanks for YOUR reply!

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

    Default Re: Write to File trouble

    1. get the GUI out of the equation. Try to simplify the problem down to its constituent issues -- that of getting information from a File and creating an object with it.
    2. You've got a lot of code packed on one line. Separate this into several lines where you get info from the file and then try to parse it or place in simple variables, one line at a time.
    3. Don't try to color code lines in the forum. That doesn't work. Use comments to highlight your problem.
    4. You're using Scanner in a way I'm not happy with. I like to get each line one at a time after first checking if the line has data and that the data is appropriate. In other words, it's a good idea to pair each scanner.nextXXX() with an appropriate preceding scanner.hasNextXXX().
    OPTYMISTA likes this.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Write to File trouble

    Have you looked in the file you output this to?
    What is the format?
    Because I suspect you are not taking into account how ArrayList outputs it's contents.
    OPTYMISTA likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Write to File trouble

    At first thanks for Your response! I've figured it out by printing out "element-by-element" from my arraylist, using simple "scanner.next()". I noticed that data saved in arraylist are not variables that we input in program, but whole (toString) from Employee class. Also prefixes as: "Name:", "Etternavn:" were includeed. What I did was just initialize new variables for name, lastname and e-mail adress and put some "empty" lines between. I'm really bad in explanations, so I'll just demonstrate it in form of java-code:

    Java Code:
    			while(filleser.hasNext()) {
    					String name,lastname, email;
    					int nr;
    						filleser.next(); // sign "[" which is opening arraylist
    						filleser.next(); // "Name:"
    						name = filleser.next();
    						filleser.next(); // "Lastname:"
    						lastname = filleser.next();
    						filleser.next(); // "E-mail:"
    						email = filleser.next();
    						filleser.next(); // "Telephone"
    						filleser.next(); // "Number:"
    						nr = Integer.valueOf(filleser.next());
    						filleser.next(); // "Room"
    						filleser.next(); // "room"
    						nyAnsatt = new Employee(name,lastname,email,nr);
    						ansatte.add(nyAnsatt);
    						filleser.next(); // sign "]", which is closing arraylist
    						System.out.println(nyAnsatt.getName() + " " + nyAnsatt.getLastname() 
    								+ " added to employeelist.");
    					}
    Last edited by OPTYMISTA; 03-29-2013 at 06:47 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Write to File trouble

    The prefixes are included because you included them.
    It uses the toString() method of your class to output as a String.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 8
    Last Post: 07-25-2012, 10:00 AM
  2. Java save to file / open from file - trouble
    By joelBeaton in forum Advanced Java
    Replies: 6
    Last Post: 03-23-2012, 12:47 PM
  3. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  4. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  5. Replies: 5
    Last Post: 02-05-2009, 10:28 AM

Posting Permissions

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