Connecting Tech Pros Worldwide Forums | Help | Site Map

help with I/0 with prompt by user

Member
 
Join Date: Mar 2007
Posts: 122
#1: Apr 27 '07
Hello everyone.
I need some help here.
I can not figure out where to put my .txt info at.
I 'm trying to use my instructure, some what confused. I have also read the info form Java and they really don't give an example on how to use a scanner where the user is prompt to find a text file once the file is found it then will display it
here is my code so far
Expand|Select|Wrap|Line Numbers
  1. public class Reading{ 
  2. public static void main(String[] args) throws Exception {
  3.         Scanner kbd = new Scanner(System.in);
  4.         int choice;
  5.         System.out.println("Make a Section: ");
  6.         System.out.println("1. Read Text File ");
  7.         System.out.println("2. Copy Text File ");
  8.         System.out.println("3. Exit ");
  9.         System.out.print("\nPlease press Enter afer each response");
  10.         System.out.println("Enter your choose please: ");
  11.         choice = kbd.nextInt();
  12.         kbd.nextLine();
  13.         if (choice == 1) { // if 1 is select go to makePerson
  14.             String fname;
  15.  
  16.             FileInputStream fin;
  17.  
  18.             long datalen=0;
  19.  
  20.             Scanner kbd1 = new Scanner(System.in);
  21.  
  22.             System.out.println("Enter file name");
  23.  
  24.             fname = kbd1.next();
  25.  
  26.             kbd1.nextLine();
  27.  
  28.  
  29.             fin = new FileInputStream("c:\\data.in");// not to sure how the .txt is found
  30.  
  31.             while( fin.read()!=-1) {
  32.  
  33.                       datalen++;
  34.             }
  35.  
  36.             System.out.println("File "+fname+" is "+datalen+" bytes long");
  37.   }
  38. }


thanks
sandyR
PS is this code right.
Do I need to use PrintWriter or can I use Printf(fname)

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Apr 29 '07

re: help with I/0 with prompt by user


Quote:

Originally Posted by sandyw

Hello everyone.
I need some help here.
I can not figure out where to put my .txt info at.
I 'm trying to use my instructure, some what confused. I have also read the info form Java and they really don't give an example on how to use a scanner where the user is prompt to find a text file once the file is found it then will display it
here is my code so far

Expand|Select|Wrap|Line Numbers
  1. public class Reading{ 
  2. public static void main(String[] args) throws Exception {
  3.         Scanner kbd = new Scanner(System.in);
  4.         int choice;
  5.         System.out.println("Make a Section: ");
  6.         System.out.println("1. Read Text File ");
  7.         System.out.println("2. Copy Text File ");
  8.         System.out.println("3. Exit ");
  9.         System.out.print("\nPlease press Enter afer each response");
  10.         System.out.println("Enter your choose please: ");
  11.         choice = kbd.nextInt();
  12.         kbd.nextLine();
  13.         if (choice == 1) { // if 1 is select go to makePerson
  14.             String fname;
  15.  
  16.             FileInputStream fin;
  17.  
  18.             long datalen=0;
  19.  
  20.             Scanner kbd1 = new Scanner(System.in);
  21.  
  22.             System.out.println("Enter file name");
  23.  
  24.             fname = kbd1.next();
  25.  
  26.             kbd1.nextLine();
  27.  
  28.  
  29.             fin = new FileInputStream("c:\\data.in");// not to sure how the .txt is found
  30.  
  31.             while( fin.read()!=-1) {
  32.  
  33.                       datalen++;
  34.             }
  35.  
  36.             System.out.println("File "+fname+" is "+datalen+" bytes long");
  37.   }
  38. }


thanks
sandyR
PS is this code right.
Do I need to use PrintWriter or can I use Printf(fname)

A few remarks:

- I don't understand the comment "if 1 is select go to makePerson"

- why are you using a separate Scanner for reading the name of the file? You
already had a Scanner instantiated for the System.in input stream.

- You read the filename "fname" supplied by the user but you don't use it.
You use a String literal for the file name instead.

- If you merely want to know the size of the file there's no need to read it all,
byte by byte. Have a look at what the File class can do for you.

kind regards,

Jos
Reply