Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 22, 2012, 04:05 PM   #1
Karpfish
macrumors 6502a
 
Karpfish's Avatar
 
Join Date: Sep 2006
Need some help with basic Java

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.

Code:
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();       
    in.close();
    }
    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);
        System.out.print(reversedList);
    }
}
__________________
MBA (Mid-2011) i7 Ultimate
Custom Built Windows 8 Workstation
A lot more external HD space than I need
Karpfish is offline   0 Reply With Quote
Old Jan 22, 2012, 04:11 PM   #2
wpotere
Guest
 
Join Date: Oct 2010
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.
wpotere is offline   0 Reply With Quote
Old Jan 22, 2012, 04:23 PM   #3
Karpfish
Thread Starter
macrumors 6502a
 
Karpfish's Avatar
 
Join Date: Sep 2006
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.
__________________
MBA (Mid-2011) i7 Ultimate
Custom Built Windows 8 Workstation
A lot more external HD space than I need
Karpfish is offline   0 Reply With Quote
Old Jan 22, 2012, 04:45 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
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:
http://forums.macrumors.com/showthread.php?t=1285568

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.
chown33 is offline   0 Reply With Quote
Old Jan 22, 2012, 04:53 PM   #5
Karpfish
Thread Starter
macrumors 6502a
 
Karpfish's Avatar
 
Join Date: Sep 2006
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.
__________________
MBA (Mid-2011) i7 Ultimate
Custom Built Windows 8 Workstation
A lot more external HD space than I need
Karpfish is offline   0 Reply With Quote
Old Jan 22, 2012, 05:00 PM   #6
wpotere
Guest
 
Join Date: Oct 2010
Quote:
Originally Posted by Karpfish View Post
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.
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.
wpotere is offline   0 Reply With Quote
Old Jan 22, 2012, 05:03 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Karpfish View Post
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.
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:
Code:
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).
chown33 is offline   0 Reply With Quote
Old Jan 22, 2012, 07:13 PM   #8
foidulus
macrumors 6502a
 
Join Date: Jan 2007
Quote:
Originally Posted by Karpfish View Post
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.

Code:
    private static String reverse (String word){
      if (word.length()==0)
          return "";
    else return (reverse(word)+ word.charAt(0));
        
    }
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.
foidulus is offline   0 Reply With Quote
Old Jan 22, 2012, 07:27 PM   #9
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by foidulus View Post
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.
A more fundamental problem: reverse() is never called:
Code:
public static void main(String[] args) throws FileNotFoundException, IOException {
    BufferedReader in = new BufferedReader (new FileReader("testfile.txt"));    
    String line = in.readLine();       
    in.close();
    }
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.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 08:56 AM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 04:48 PM
Run Basic Command-line Java Programs in Sandvox jblagden Mac Applications and Mac App Store 0 Oct 2, 2013 03:23 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 10:13 AM
Free note app with basic formatting basic HTML Subu iPhone and iPod touch Apps 0 Mar 1, 2013 04:44 AM

Forum Jump

All times are GMT -5. The time now is 11:18 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC