Results 1 to 14 of 14
  1. #1
    sarapeace is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default Problem reading from a file

    Hi,

    The program should read commands from a text file. Example:
    save name Sara Smith; phone 0344444; address unit23 pt street
    then save it in an object person so I can have
    name=Sara Smith
    phone= 0344444
    address= unit23 pt street

    the problem is I can see that it is splitted to 3 parts after using the Delimiter, when I scan the first one I get the whole sentence "name Sara Smith", so I made another scanner that read each word of a sentence but it reads only the first sentence.
    I'm trying for 2 days to solve it but no progress I don't know what is the poblem.



    Java Code:
    public static ArrayList readFile(String instructionFile) 
    	{
    		ArrayList<Person> records = new ArrayList<Person>();
    		
    		try {
    			
    			File file = new File(instructionFile + ".txt");
    			
    			Scanner read = new Scanner(file);
    			String str;
    			Person person = null;
    			
    
    			while (read.hasNext()) 
    			{
    				str = read.nextLine();
    
    				Scanner line = new Scanner(str);
    				String command;
    				Date birthday = null;
    				if (line.hasNext()) 
    				{
    					// store the first word (command) of the command line
    					command = line.next();
    					if (command.equalsIgnoreCase("save")) 
    					{
    						index++;
    						person = new Person();
    
    						String tokens = line.nextLine();
    						Scanner s = new Scanner(tokens).useDelimiter(";");
    						String st;
    						//System.out.println(s.next());
    						Scanner token;
    						while (s.hasNext()) 
    						{
    							
    							st = s.next();
    							token= new Scanner(st);
    							//System.out.println(st);
    							if (token.next().equalsIgnoreCase("name")) 
    							{
    								//person.setName(token.nextLine());
    								System.out.println(token.nextLine());
    							}
    							else if (token.next().equalsIgnoreCase("phone")) 
    							{
    								person.setPhone(token.nextInt());
    								//System.out.println(token.nextInt());
    							}
    
    						}
    
    						records.add(index, person);
    					
    				    } // end save
    
    				}
    				}
    				read.close();
    				
    				return records;
    			
    		} catch (Exception e) 
    		{
    			System.out.println("Error:- " + e.getMessage());
    			return null;
    		}
    	}
    that is only part of the code.
    thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    it reads only the first sentence
    Does the loop end?
    What happens after it reads the first sentence?
    Try debugging your code by adding print outs to show how variables change and execution flows. For example print out every line that is read from the file.
    Your print outs are commented out. What did they show when executed?
    The print outs would be more useful if they had an id string with the variable:
    println("s.next()=" + s.next());

    It looks like the print outs could "eat" data that the following code would not see.
    Better to put the data into a variable and print that variable than call a method in the print method.




    Where is the ending } of the while() loop? Your formatting makes it hard to see.
    Putting a comment on the ending } can make code easier to scan:
    } // end of while()

  3. #3
    sarapeace is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Thanks a lot, it works fine now.
    Instead of using the token directly each time - else if (token.next().equalsIgnoreCase("phone")) - I put it in a variable.
    Using the print out the way you said was very helpful


    I have another problem in reading date. I used SimpleDateFormat
    example:
    Java Code:
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    Date birth = df.parse("22-02-1982");
    System.out.println("birth= " + birth);
    when I print it I see the day and time as well? I don't know why?
    Java Code:
    birth= Mon Feb 22 00:00:00 EST 1982
    also I don't know how to set the date to the instance variable in the person object? I've read the java documintation and searched the net but still can't make it works.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    when I print it I see the day and time as well
    That must be what the toString() method for Date returns.

    how to set the date to the instance variable in the person object
    Can you show the code your are using for this?

  5. #5
    sarapeace is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Can you show the code your are using for this?
    I tried many things the last thing i used is this in the person class.
    import java.util.Date;
    birthday is type of Date
    Java Code:
    		public void setBirthday(Date birth){
    			birthday.setDate(birth);
    		}
    That must be what the toString() method for Date returns.
    when I used toString() it was the same problem
    String strDate = birth.toString();

    then I used :
    System.out.println("birth= " + df.format(birth));
    It worked but I'm not sure if it's a correct way.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    I'm not sure if it's a correct way
    Does it compile ok?
    Do you get the results you want?

  7. #7
    sarapeace is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Does it compile ok?
    Do you get the results you want?
    yes.. now it's ok, I was confused before.

    I still can't make this work:
    [QUOTE=sarapeace;146398]I tried many things the last thing i used is this in the person class.
    import java.util.Date;
    birthday is type of Date
    Java Code:
    		public void setBirthday(Date birth){
    			birthday.setDate(birth);
    		}

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    I still can't make this work:
    please explain. (if you'd include the explanation with the post it'd make it faster)

  9. #9
    sarapeace is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    the problem is in setting the birthday in Person

    import java.util.Date;

    public class Person {

    private Date birhday;
    }
    public void setBirthday(Date birthday){
    //birthday.setDate(birthday);
    }
    Java Code:
    		else if (t.equalsIgnoreCase("birthday")) 
    							{
    								
    								DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    								String p=token.next();
    								System.out.println("p="+p);
    								Date birth = df.parse(p);
    								System.out.println("birth= " + df.format(birth));
    
    person.setBirthday(birth);						
    	}

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    You have not explained what the problem is.
    Does it compile?
    Does it execute?
    Do you get the desired results?

  11. #11
    sarapeace is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    No I can't run the program, I attached a picture of the problem.
    Attached Thumbnails Attached Thumbnails Problem reading from a file-error.jpg  

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    What you are showing is a compile time problem. I guess your IDE doesn't actually let you compile the program.
    Have you read the API doc for the Date setDate method? What values does it take?
    Where can you get the value that is required?
    Read all the methods for the Date class.
    Especially the parts that talk about this: standard base time known as "the epoch"

  13. #13
    harinder_romy is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi Sara,
    Its seem You have very similar problem like mine. I am working on my assignment EMR. Still not getting how to store birthday in Date variable..... it can be in any format in text file
    such as 12/12/1978 or 12-12-1978 or 12-12/1978 or 12/12-1978
    I really have no clue how to deal with all 4 formats
    Have you figure it out yet or not???????? or any1 please help us

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Look at the SimpleDateFormat class. It has a parse method that will help.

Similar Threads

  1. Replies: 26
    Last Post: 05-31-2010, 09:05 PM
  2. Reg: File Reading Problem
    By balaji csc in forum New To Java
    Replies: 0
    Last Post: 11-06-2009, 04:22 PM
  3. Weird path problem when reading properties file
    By jerry_popperq in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 03:32 PM
  4. Problem with reading text from a .txt file
    By Gigi in forum New To Java
    Replies: 40
    Last Post: 01-22-2009, 03:22 AM
  5. Problem reading an xml file with AJAX
    By Fiona80 in forum XML
    Replies: 0
    Last Post: 12-17-2007, 08:02 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
  •