Results 1 to 20 of 24
- 10-25-2010, 12:32 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Simple question Hopefully, Scanner/file
Hi there, im new to java. Have to study it so just covering the basics. I have read from a file and used Scanner to read user input but the problem i have got now is using the scanner to choose the file to read from. I need the user to type in example.txt and then use that input to get File to read it and print it. Im stumped. This is all i have right now:
import java.util.Scanner;
import java.io.File;
class Choose
{
public static void main (String[] args) throws Exception
{
Scanner scan = new Scanner(System.in);
File myFile = scan.nextLine();
String textfile;
System.out.println("Please Enter File to Read Including .txt=>");
textfile = scan.nextLine();
scan.close();
}
}
Thanks alot in advance
- 10-25-2010, 12:37 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you compile and run your code, if so what happen?
- 10-25-2010, 12:42 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Choose.java
Found: java.lang.string
Required. java.io.file
File myFile = scan.nextLine(); Points to the open bracket of "()"
- 10-25-2010, 12:44 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Have a read of this
Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)
- 10-25-2010, 12:48 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Thanx for the replies. It still has the error. The "textfile" scan doesnt even come up. Its because im not using the right thing with File - Usually its File myFile = new File ("example.txt") <-- i need to make that "example.txt" an imput for the user to specify
- 10-25-2010, 12:52 PM #6
So just get a String from the user and then declare it just like you said above. Deleting the one you have now.
Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 12:55 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Ok thanx i am reading through that now.
Im new to this so forgive my ignorence. I can't seem to do that though, something must come after the File myFile = new File? No matter what i type to try and set is as the inputted string just doesnt work and throws compile errors my way
- 10-25-2010, 12:56 PM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
1. Use a String variable say called fileName;
2. Ask the user for the name of the file
2. Use scanner to get the input (as a String);
3. use that String as the file name inside File myFile=new File(fileName)
4. open file for reading
5. print out the contents per read
- 10-25-2010, 12:56 PM #9
There's no rule that says you can't get the input from the user before declaring a file though right?
Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 01:06 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Ok thanks thats great, something seemed to click as that worked alot better that time.
Reading the linked page got me worried though, their program seems to be written in a much more complicated set up just to read a file?
Thus far i have only printed files i have made on to the screen using println. is there a way to print the whole page? Sorry i know this seems elementary stuff and im looking through the API its all just abit mental
- 10-25-2010, 01:11 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-25-2010, 01:14 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Yea my variable (textfile) will be what the next line scanned in from the keyboard?
Its all just abit insane at the moment, so much to learn haha. All my scanning before has just had a variable per line to read it.
- 10-25-2010, 01:16 PM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Okay, if you read a line, what you've to do? Check any other lines are available or not? In API you can find the required things. Then, in a loop check that condition and work on with it.
- 10-25-2010, 01:22 PM #14
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Thats the thing we havent been taught yet. Im only a few weeks in. We have been shown the scanner and this one that im stuck is a task to try. Havent even glimpsed at loops yet!
All i have been shown to do it print per line, so if my crerated text file has 5 lines just do 5 variables and print out the line. Im working my way through this one slowly, best way to learn i suppose!
- 10-25-2010, 01:25 PM #15
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
But on brighter news i have managed to make it work (With the exception that it only prints the first line) But the main bit is done, Big thanx to you guys!
Java Code:import java.util.Scanner; import java.io.File; class Choose { public static void main (String[] args) throws Exception { Scanner scan = new Scanner(System.in); String filename; System.out.println("Please Enter File to Read Including .txt=>"); filename = scan.next(); File myFile = new File(filename); Scanner scantwo = new Scanner (myFile); String readfile; readfile = scantwo.next(); System.out.print(readfile); scan.close(); } }Last edited by Eranga; 10-25-2010 at 01:27 PM. Reason: code tags added
- 10-25-2010, 01:27 PM #16
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
I'd learn loops before going any further:
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
- 10-25-2010, 01:28 PM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What's the exception you comes with?
- 10-25-2010, 01:33 PM #18
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Ok i will have a look into that, should prob but some hard hours into Java, should set me up for the year.
The exception? I dunno what you mean exactly - What i kmeans was it prints the file, but only the first line.
Anyway i have to run into uni now for un programming related lectures i will come back on here later.
Once again thanks so much for the input though. Much appreciated.
- 10-25-2010, 01:38 PM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-25-2010, 01:40 PM #20
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
Simple string add or subtract using scanner
By weezer562 in forum New To JavaReplies: 12Last Post: 10-21-2010, 08:23 PM -
scanner question
By kira137 in forum New To JavaReplies: 8Last Post: 10-12-2009, 03:09 PM -
Scanner Issues (User Input: Very Simple)
By carlodelmundo in forum New To JavaReplies: 8Last Post: 10-31-2008, 02:44 AM -
[SOLVED] Simple Scanner Method - Plus Sign throwing me off...
By Josh.Hoekstra in forum New To JavaReplies: 2Last Post: 06-02-2008, 10:24 PM -
Scanner class question
By Rgfirefly24 in forum New To JavaReplies: 5Last Post: 04-25-2008, 12:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks