Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: No such file or directory exist (help!)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default No such file or directory exist (help!)

    So I'm making this simple zombie simulation game wherein a player is given a situation and different options to choose. Whenever they make a decision I have an object that will write a letter within a file which is a number. This number indicates their score on depending on the decision.

    System.out.println("\n\t\t==================================================");
    		System.out.println("\t\t|                                                |");
    		System.out.println("\t\t| You have been hiding inside your house for a   |");
    		System.out.println("\t\t| few days now. Outside there are plenty of      |");
    		System.out.println("\t\t| Zombies outside. What will you do?             |");
    		System.out.println("\t\t|                                                |");
    		System.out.println("\t\txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    		System.out.println("\t\txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    		System.out.println("\t\t|| [1] Barricade the house                      ||");
    		System.out.println("\t\t|| [2] Go outside and fight the zombies         ||");
    		System.out.println("\t\t|| [3] Search for supply                        ||");
    		System.out.println("\t\t|| [4] Escape through the back                  ||");
    		System.out.println("\t\t==================================================");
    		System.out.print("\t\tWhat would it be?: ");
    		int ans1 = keyIn.nextInt();
     
    		switch(ans1)	{
     
    		case 1: HomeGame.home1a(); loop = false; break;
    		case 2: HomeGame.home1b(); loop = false; break;
    		case 3: HomeGame.home1c(); loop = false; break;
    		case 4: HomeGame.home1d(); loop = false; Score.addScore(); break;

    This is the code for the object (This is still just a testing so everything is short)

    	public static void addScore() throws Exception	{
     
    		try{
     
    		FileWriter outFile = new FileWriter("test.txt", true);
     
    		outFile.write("a"+"\r\n");
     
    		outFile.close();
    		}
     
    		catch(Exception e)
    		{
    		System.out.println("Something went wrong: "+e.getMessage());
    		}
     
    	}
     
    	public static void ShowScore() {
     
    	try {
    		int count = 0;
     
    		BufferedReader fileIn = new BufferedReader (new FileReader("text.txt"));
    		String S = fileIn.readLine();
     
    		while(S!=null)
    		count++;
    		System.out.println("line "+count+": "+S);
    		S = fileIn.readLine();	
    	}
     
    	catch(Exception e)
    	{
    	System.out.println("Something went wrong: "+e.getMessage());
    	}
     
    	}
     
    }

    The output I always get is: No such file or directory exist


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: No such file or directory exist (help!)

    Quote Originally Posted by hawkeye10 View Post
    This is ... a test...everything is short
    I like your style! Get something simple to work. Make sure you understand what's going on, then fill in the Good Stuff. That's Great! (And I don't tell folks that unless I mean it.)

    Quote Originally Posted by hawkeye10 View Post
    The output I always get is: No such file or directory exist
    First of all, make sure that your error catching messages are distinguishable. Does the error come from addScore() or from showScore()?

    Maybe augment them to something like:

        public static void addScore() throws Exception  {
     
            try{
                FileWriter outFile = new FileWriter("test.txt", true);
                outFile.write("a"+"\r\n");
                outFile.close();
            }
     
            catch(Exception e)
            {
                System.out.println("addScore(): Something went wrong: "+e.getMessage());
            }
     
        }
     
        public static void showScore() {
     
          try {
            int count = 0;
     
            BufferedReader fileIn = new BufferedReader (new FileReader("text.txt"));
            String S = fileIn.readLine();
     
            while(S!=null)
            count++;
            System.out.println("line "+count+": "+S);
            S = fileIn.readLine();
          }
     
          catch(Exception e)
          {
            System.out.println("showScore(): Something went wrong: "+e.getMessage());
          }
     
        }

    Then, remove any files named "*.txt" in your directory and recompile and run the program.

    After running the program:

    Does the directory have a file whose name is the name of the file that you tried to write? If so, what are the contents of that file?

    Does the directory have a file whose name is the name of the file that you tried to read? If so, what are the contents of that file?

    Look at the error message. Does the directory have a file whose name is reported in the error message? If so, what are the contents of that file?


    Cheers!

    Z
    Last edited by Zaphod_b; October 14th, 2012 at 02:14 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: No such file or directory exist (help!)

    I have changed the code of the object that does the file writing and reading. But I still get this error at the end when I try reading the file.

     
    ShowScore(): Something went wrong: text.txt (No such file or directory)

    I have seen the file on my workspace area named "text.txt" and within that file is the letter 'a' since it's only going to write once.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: No such file or directory exist (help!)

    Quote Originally Posted by hawkeye10 View Post
    I have changed the code
    Well, how the heck can I know what is going wrong if I can't see your changed code.
    Quote Originally Posted by hawkeye10 View Post
    ShowScore(): Something went wrong: text.txt (No such file or directory)

    I have seen the file on my workspace area named "text.txt" and within that file is the letter 'a' since it's only going to write once.
    Did you do what I suggested?

    With your original code, after erasing "*.txt" files and running the code, I found a code named "test.txt" which is what addScore() writes. The ShowScore() function, on the other hand was looking for "text.txt" which is what the error message said was missing. Notice "test.txt" is not the same as "text.txt"

    If you have changed your code, how about showing us? Otherwise I really can see no way of helping.


    Cheers!

    Z

Similar Threads

  1. Replies: 4
    Last Post: June 6th, 2012, 03:31 PM
  2. Checkin if a file exist within a directory
    By tash876 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 23rd, 2011, 02:16 PM
  3. PROBLEM CHECKING IF RECORDS EXIST OR DO NOT EXIST IN DATABASE
    By jimmyb0206 in forum JDBC & Databases
    Replies: 1
    Last Post: April 10th, 2011, 09:18 AM
  4. Urgent - File exist nor working
    By Bagzli in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2011, 04:09 AM
  5. Help Remove file from pad directory
    By georgybaja in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 8th, 2011, 05:29 PM