Results 1 to 6 of 6
  1. #1
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default Can't read from my file

    I can't figure why my program isn't reading input from a text file. I'm using the file name as the String parameter for the constructor, but keep getting the exception file is not found even though they are in the same folder. Classy.txt is listed right below Tinker.java in the same file. What's the problem?
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class Tinker {
    	
    	public Tinker(String x) {
    			   try {
    				   Course temp = new Course();
    				   FileReader from = new FileReader(x);
    				   BufferedReader input = new BufferedReader(from);
    				   String courseinfo = input.readLine();
    				   StringTokenizer cl;
    				   int count = 0;
    				    while (courseinfo != null) {
    				    	cl = new StringTokenizer(courseinfo);
    				    	count += cl.countTokens();
    				    	if(count == 1){
    				    		temp.setName(courseinfo);
    				    	}
    				    	if(count == 2) {
    				    		temp.setSection(courseinfo);
    				    	}
    				    	if(count == 3) {
    				    		String startT = courseinfo;
    				    		startT = startT.replaceAll(":","");		
    				    		startT.trim();
    				    		int starter = temp.toMilitaryTime(startT);
    				    		if(starter >= 1200){
    				    			startT = "" + startT + "PM";
    				    		}
    				    		else {
    				    			startT = "" + startT + "AM";
    				    		}
    				    		temp.setStartTime(starter);
    				    	}
    				    	if(count == 4){
    				    		String endT = courseinfo;
    				    		endT = endT.replaceAll(":","");
    				    		endT.trim();
    				    		int ender =temp.toMilitaryTime(endT);
    				    		if(ender >= 1200){
    				    			endT = "" + endT + "PM";
    				    		}
    				    		else {
    				    			endT = "" + endT + "AM";
    				    		}
    				    		temp.setEndTime(ender);
    				    	}
    				    	if(count == 5) {
    				    		String dayconvertor = courseinfo;
    				    		dayconvertor = dayconvertor.replace('H','Y');
    				    		dayconvertor = dayconvertor.replace('F','Z');
    				    		temp.setDays(dayconvertor);
    				    	}
    					   courseinfo = input.readLine();
    					  System.out.print(temp);
    				    }
    				
    				}
    				catch (FileNotFoundException e) {
    					System.out.println("File classy.txt not found");
    				}
    				catch(IOException e) {
    					System.err.println("Exception in reading from file");
    				}
    			}	
    	public static void main(String[] args) {
    		Course A = Course.fromAString("CSC200 001 01:30PM 02:30PM MHF");
    		Course B = Course.fromAString("CSC216 100 8:30AM 03:00PM F");
    		Course C = Course.fromAString("MAT350 222 03:30PM 08:45PM MWF");
    		Tinker cmon = new Tinker("classy.txt");
    	}
    }

  2. #2
    Yaridovich is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    That's not quite the right place to put it I think, I tried it myself and found I had to put the text file elsewhere. What is the filepath for Tinker.java?

  3. #3
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    Not totally sure I understand what's meant by filepath. Tinker.java is a dummy class I made to duplicate a class called CourseList, which is a linked list of Course objects. I need to be able to make a CourseList from data in a text file. Tinker.java and classy.txt are both in the same project folder in Eclipse. Does that answer your question?
    Last edited by MetalR0; 07-31-2011 at 11:46 PM.

  4. #4
    Yaridovich is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    I mean, where is it in your computer, something like C:\Users\YourName\...
    The reason I ask is because when I tried it first I put both Tinker.java and classy.txt in C:\Users\MyName\Documents\NetBeansProjects\MainPro jects\src\Tester\.
    That didn't work, but if I copied "classy.txt" into the MainProjects folder then it did work.

    If the file system for eclipse is anything like what it is for Netbeans, then the folder in which Tinker.java and "classy.txt" are currently located isn't in the middle of nowhere, it's in some other eclipse folder. Try putting "classy.txt" in a folder analogous to where I put mine.

  5. #5
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    Hey it worked! Thanks!

  6. #6
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    I ran into similar issues when I was looking at files. I also found that placing the text or data files in the root directory of the project seemed to work.

    Good work.

Similar Threads

  1. Replies: 14
    Last Post: 07-15-2011, 06:22 AM
  2. Replies: 2
    Last Post: 05-11-2009, 11:07 AM
  3. Replies: 0
    Last Post: 02-11-2009, 10:53 AM
  4. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 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
  •