Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Choose.java
    Found: java.lang.string
    Required. java.io.file
    File myFile = scan.nextLine(); Points to the open bracket of "()"

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

  5. #5
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    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

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    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 :)

  7. #7
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    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

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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

  9. #9
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    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. #10
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    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

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by drucey View Post
    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
    Do you know what this line does. Try to identify, then you can find the solution yourself. ;)

    Java Code:
    textfile = scan.nextLine();

  12. #12
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    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.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  14. #14
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    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!

  15. #15
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    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

  16. #16
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  18. #18
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    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.

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by drucey View Post
    .... (With the exception that it only prints the first line) ....
    About this.

  20. #20
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Eranga there is no java exception as you are thinking, he is just saying the program works but only prints the first line.

Page 1 of 2 12 LastLast

Similar Threads

  1. Simple string add or subtract using scanner
    By weezer562 in forum New To Java
    Replies: 12
    Last Post: 10-21-2010, 08:23 PM
  2. scanner question
    By kira137 in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 03:09 PM
  3. Scanner Issues (User Input: Very Simple)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 10-31-2008, 02:44 AM
  4. Replies: 2
    Last Post: 06-02-2008, 10:24 PM
  5. Scanner class question
    By Rgfirefly24 in forum New To Java
    Replies: 5
    Last Post: 04-25-2008, 12:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •