1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Need some help with basic Java

Discussion in 'Mac Programming' started by Karpfish, Jan 22, 2012.

  1. macrumors 6502a


    I'm just learning Java and am tasked with reading in from a file, then reversing each word in that file (so "this file" would output as "siht elif"). I have some code here but I am getting a FileNotFoundException on compile however the file is in the proper location. Because it can't find the file, I can't see what the output is at this point, although I know it is not correct right now. Can someone provide some insight given what I have? The methods used (and parameters passed in) are required however the code within the methods is my own and obviously subject to change. Any help would be greatly appreciated.

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    public class Prog1 {
        public static void main(String[] args) throws FileNotFoundException, IOException {
        BufferedReader in = new BufferedReader (new FileReader("testfile.txt"));    
        String line = in.readLine();       
        private static String[] getWords(String line){
        String[] wordList = line.split("\\s+");
        return wordList;
        private static void reverseWords(String[] wordList){
        char[] words = new char []
        char[] words = wordList.toCharArray();
        String word = String.valueOf(words); 
        private static String reverse (String word){
          if (word.length()==0)
              return "";
        else return (reverse(word)+ word.charAt(0));
        private static void printWords(String[] wordList){
        String reversedList = String.valueOf(wordList);
  2. Guest

    I assume you are on a Mac? Check to make sure that the permissions are set on the file correctly and make sure that it is in the same folder as the src file. Lastly, make sure spellings are correct. ;)
  3. macrumors 6502a


    My main machine is a MBA but I have been doing this on a library PC desktop. The file is in the same folder as src. I don't see any spelling errors but if someone could point them out that would be awesome.
  4. macrumors 603

    Simply putting your file in the same directory as the source does not guarantee anything. Not even putting it in the same location as the class file guarantees anything.

    1. Make sure you understand what the current working directory means.

    2. Learn how to get the current working directory in Java. Google search terms: java working directory
    (Hint, it's a system property).

    3. Write a program that prints the current working directory. You could even just add it as the first line of your current program.
    It will be quite a short piece of code, but make sure you understand what it does, and most important, what its output means.

    4. If the current working directory output from step 3 isn't what you think it is, maybe there's something about how you're running your Java program that you haven't told us. Like what tools or IDE you're using.

    If you're using Xcode (and I'm guessing), see this thread:

    If you're using something else, then the way to set the working directory is different, and you'll need to tell us what you're using.
  5. macrumors 6502a


    I am using NetBeans. I was told to put the file (testfile.txt) in the top directory of the NetBeans project. That is where it is right now.
  6. Guest

    With Netbeans that does sound correct. It should be in the top of the root folder.

    Another thing to check is to make sure that you didn't accidentally type the extension twice. If you have extensions hidden in Windows, you create folder called file.txt, it is possible that you created a file called file.txt.txt. I have seen a few people do this before.

    If Netbeans is having issues with it then you might want to try and compile it manually to see if that works.
  7. macrumors 603

    Then you need to answer this question: Is that where the current working directory is located when your program runs?

    If you don't have the answer, or even if you do, I suggest actually performing steps 1-4 as outlined earlier. In short, write code that shows you what your program is receiving as its current working directory.

    If the working directory is correct, I suggest using the File.list() method to get a list of every file in that directory. Print that list.

    Or on the Terminal command-line:
    cd where/your/project/dir/is
    ls -l

    Also, please clarify the "I am getting a FileNotFoundException on compile" statement in your first post. In particular, post the actual text of the error message.

    Because seeing that error on compile means something else. That would indicate a compiler error (the compiler tries to compile your program and fails), which means something completely different than a runtime error (your program was compiled successfully, yet when it runs it can't find its input file).
  8. macrumors 6502a

    This isn't what you asked, but I did notice a problem with your code. The above code is simply going to create a StackOverflowException, you have to remove a character from the word before recursing or all you are going to do is infinitely call the function with the same input.
  9. macrumors 603

    A more fundamental problem: reverse() is never called:
    public static void main(String[] args) throws FileNotFoundException, IOException {
        BufferedReader in = new BufferedReader (new FileReader("testfile.txt"));    
        String line = in.readLine();       
    That's it. Open a file, read a line, close, done. No reversing, no printing, nothing. So even if the file is being opened and read, nothing else happens.

    There are also multiple problems in reverseWords() that will cause the compiler to fail. So I find it hard to believe the posted code was actually compiled. Or the code that was compiled doesn't match what was posted.

Share This Page