Java Question

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Java Question

Post by Shadow Ozera on Sun Jul 25, 2010 12:43 am
([msg=42656]see Java Question[/msg])

Hello all, lately iv been learning scanners all over again because iv sorta forgotten what i leaned over the school year. All i'm trying to do is get the tokens from my txt file be displayed. When i compile it, it says it worked but, it doesnt write anything out. I'v saved the txt file in the same folder thats in "My Documents"...did i save it to the wrong place?
Here's my code
Code: Select all
import java.io.*;
import java.util.*;

public class hope{
   
   public static void main(String[] args) throws IOException{
      
      Scanner sc = null;
      
      try{
          sc = new Scanner(new BufferedReader(new FileReader("cat.txt")));
         
         while (sc.hasNext()) {
            System.out.println(sc.next());
            
         }
         
         
      }finally {
      if (sc !=null){
         sc.close();
      }
      
   }
   }      
      
}
Shadow Ozera
New User
New User
 
Posts: 28
Joined: Wed Jun 16, 2010 8:52 pm
Blog: View Blog (0)


Re: Java Question

Post by TheMindRapist on Sun Jul 25, 2010 12:50 am
([msg=42657]see Re: Java Question[/msg])

For that code to work cat.txt has to be in the same folder as hope.java, which likely isn't the My Documents folder.
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: Java Question

Post by Shadow Ozera on Sun Jul 25, 2010 1:13 am
([msg=42660]see Re: Java Question[/msg])

Well, i went to my documents, JCreater LE, projects, hope and then i just made a txt file in the hope folder.

Do i have to save it in the src section?
Shadow Ozera
New User
New User
 
Posts: 28
Joined: Wed Jun 16, 2010 8:52 pm
Blog: View Blog (0)


Re: Java Question

Post by TheMindRapist on Sun Jul 25, 2010 1:31 am
([msg=42661]see Re: Java Question[/msg])

Shadow Ozera wrote:Well, i went to my documents, JCreater LE, projects, hope and then i just made a txt file in the hope folder.

Do i have to save it in the src section?

You could try that, that might work. Alternatively, you can use the absolute path to the file, i.e:

Code: Select all
new FileReader("C:\\Documents and Settings\\My Documents\\JCreater LE\\projects\\hop\\cat.txt")

or whatever the path would be.
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: Java Question

Post by Shadow Ozera on Sun Jul 25, 2010 3:14 am
([msg=42663]see Re: Java Question[/msg])

How would i use that one?
Shadow Ozera
New User
New User
 
Posts: 28
Joined: Wed Jun 16, 2010 8:52 pm
Blog: View Blog (0)


Re: Java Question

Post by tremor77 on Sun Jul 25, 2010 11:02 am
([msg=42679]see Re: Java Question[/msg])

On a completely semi off-topic note.. I really need to start learning JAVA. I just hate how kludgy and bulky the SDK and eclipse are.. everytime I go back to start trying my hand at JAVA the whole concept just pisses me off lolz
User avatar
tremor77
Addict
Addict
 
Posts: 1098
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)


Re: Java Question

Post by TheMindRapist on Sun Jul 25, 2010 2:20 pm
([msg=42686]see Re: Java Question[/msg])

Shadow Ozera wrote:How would i use that one?

What?

You're just specifying where the file, in the case cat.txt is. You can do so using a relative path, meaning a path relative to the folder that you are in. This is what you've attempted in your OP - you're telling your program to look for cat.txt in the same folder that the program itself is in. If you're not sure where cat.txt is relative to the program you can use an absolute path, which is the path irregardless of the folder. For example, say that you are on the page www.site.com/folder/subfolder/page.php and you want to get to anotherPage.php, which is in subfolder. You can use the relative path, which would be ./anotherPage.php, or you can use the absolute path, which would be www.site.com/folder/subfolder/anotherPage.php The . character means current folder, .. means move up one folder. If you want to make things very simple just put cat.txt in you C: drive and then use the path C:\\cat.txt
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: Java Question

Post by Shadow Ozera on Mon Jul 26, 2010 5:51 am
([msg=42736]see Re: Java Question[/msg])

huh, well i replaced cat.txt with C:\\cat.txt..still nothing. I think il just move on to my next Java project -_-
Shadow Ozera
New User
New User
 
Posts: 28
Joined: Wed Jun 16, 2010 8:52 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests