473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

help with I/0 with prompt by user

122 100+
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)
Apr 27 '07 #1
1 1292
JosAH
11,448 Expert 8TB
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
Apr 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Trvl Orm | last post by:
I am working with 2 frames, Left and Right and the main code is in the left frame, which has been attached. Can someone please help me with this code. I am new to JavaScript and can't figure it...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
8
by: drose0927 | last post by:
Please help! I can't get my program to exit if the user hits the Escape button: When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this error: Parse Error, expecting `'}''...
26
by: Bail | last post by:
I will have a exam on the oncoming friday, my professor told us that it will base upon this program. i am having troubles understanding this program, for example what if i want to add all the...
10
by: ycg0771 | last post by:
I'm trying to modify the following program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
1
by: sanjay1985 | last post by:
1. prompt the use to enter paycode ,valid paycode are 1,2,3 and 4. if invalid paycode entered, print message and ask the user to re enter. newline, tab and blank should be ignored. 2.manager has...
1
by: tikney5 | last post by:
I need to modify my current program so that is uses a class to store and retrieve the employees name, hourly rate, and number of hourse worked. Use a constructor to initialize the employee...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.