Java program that asks a customer of a store to type in their name

  • Context: Java 
  • Thread starter Thread starter Nothing000
  • Start date Start date
  • Tags Tags
    Java Program Type
Click For Summary

Discussion Overview

The discussion revolves around a programming assignment that requires creating a Java program to read customer data from a file instead of the keyboard. Participants explore the implications of reading input from a file, how to structure the program, and the command line instructions necessary for execution.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant seeks clarification on how to read data from a file rather than from keyboard input.
  • Another participant suggests creating a separate method for gathering input, but it remains unclear how this integrates with file reading.
  • Several participants explain that a text file, such as one with a .txt extension, is needed for input, and that the program should be run with specific command line instructions.
  • There is confusion about the command line syntax and whether it should be included in the code itself.
  • One participant emphasizes that the program should be written as if it were taking input from the keyboard, with the file redirection occurring at runtime.
  • Another participant expresses a desire for further clarification on how to implement the file reading in the program.
  • There are suggestions for additional resources to understand standard input and output in Java.

Areas of Agreement / Disagreement

Participants generally agree on the need to read from a file and the command line syntax required for execution, but there is confusion and lack of consensus on how to implement this in the program itself and the specifics of handling input.

Contextual Notes

Some participants express uncertainty about the command line interface and the concept of standard input, indicating a potential gap in foundational knowledge that may affect their understanding of the assignment.

Nothing000
Messages
403
Reaction score
0
Hello. I have an assignment that asks me to create a program that asks a customer of a store to type in their name, and an amount of money they spent. Then the program calculates a dicount they get according to the amount of money they spend. And print the discount to the screen.

But it says that the data should be read from a file, not from the keyboard.

I don't understand what it means about being "read from a file, not the keyboard".
 
Technology news on Phys.org
Does this mean that I create another method that asks for the user to input the information. And then in the main method I call on that method that gathers the input info?
 
I don't get it.
 
What exactly don't you get? Do you know what a text file is?
 
By text file do you mean like a file of the format .txt? Like a file created with notepad?
 
Do I just create an input file like normal?
 
Last edited:
And then somehow call on that file from the main program when it needs the input?
 
My assignment says that "the input should be redirected to come from a file named Discount2.dat using the UNIX command:
javac Discount2.java
java Discount2 < Discount2.dat

I don't understand what this means. Do I actually put this:

javac Discount2.java
java Discount2 < Discount2.dat

into my code to call on the input file?
 
  • #10
No. A text file is a file containing text. Anything with a .txt extension probably is a text file. Anything with a .java extension--that means your java programs--probably is a text file. Anything you typically open with Notepad (or whatever plain text editing program you use) is a text file. Text files contain unadorned text--that means just 1 font, usually of constant character width. Nothing is formatted in any way. You just have a bunch of ordinary characters, probably ASCII, that you can read with a text editor.

Edit: That changes things. You just use the usual System.in in that case. You just type that in on the command line: that is, you write your program just as if it were accepting input from the keyboard. Then at the command line you type those two lines, and if discount2.dat is a properly formatted text file, your discount2 program will run just as if you had typed the contents of discount2.dat from the keyboard.
 
Last edited:
  • #11
Ok, I get that, but how do I use that knowledge to make a program that has it's data read from a file, not from the keyboard.
 
  • #12
You write the program as if nobody had told you anything about taking input from text files. It's only when you go to run it, at the command line, that you do something different.
 
  • #13
0rthodontist said:
You write the program as if nobody had told you anything about taking input from text files. It's only when you go to run it, at the command line, that you do something different.

Could you please elaborate on that.

I really want to understand this, but I am finding it very confusing. By the way, the assistance is GREATLY appreciated.
 
  • #14
I can't be any more clear. You write your program the usual way, and run it with java Discount2 < Discount2.dat instead of only java Discount2. The < means that the contents of Discount2.dat is sent to the standard input stream of your program Discount2. Every program has a standard input and a standard output stream (and standard error). The standard input defaults to the keyboard--when you call readLine() you are reading from standard input, which is normally what you type at the keyboard. But if you make the standard input for the program to be Discount2.dat, then readLine() will read lines from Discount2.dat instead of the keyboard.
 
Last edited:
  • #15
0rthodontist said:
You write your program the usual way, and run it with java Discount2 < Discount2.dat instead of only java Discount2

I don't understand what you mean.
 
  • #16
So I would write the program like this normal?
 
Last edited:
  • #17
0rthodontist said:
You write your program the usual way, and run it with java Discount2 < Discount2.dat instead of only java Discount2. The < means that the contents of Discount2.dat is sent to the standard input stream of your program Discount2. The standard input defaults to the keyboard--when you call readLine() you are reading from standard input, which is normally what you type at the keyboard. But if you make the standard input for the program to be Discount2.dat, then readLine() will read lines from Discount2.dat instead of the keyboard.

So I would write the program as above, but leave out all of the scanner inputs?
 
  • #18
Nothing000 said:
0rthodontist said:
You write your program the usual way, and run it with java Discount2 < Discount2.dat instead of only java Discount2
I don't understand what you mean.
How can you not understand that? Do you know what the command line is?

I haven't done a careful read of your code as I about to go to sleep, but I think you have the right idea (edit: NO, do not leave out the scanner inputs). By the way, for future postings of your code you can use
Code:
 tags, like
[code]
This is code-style
{
    text
}
 
  • #19
Oh, cool. Thanks bro.
 
  • #20
It sounds like you need to do some legwork on your own. Make sure you have a good reference or just google around for anything and everything pertaining to compiling a java program, running a java program, and standard input/standard output. Below is a link to an article that talks about standard input/standard output...I found it in about 10 seconds. Google is your friend.

http://www.javaworld.com/javaworld/jw-03-2001/jw-0302-java101.html?page=1
 

Similar threads

Replies
65
Views
5K
  • · Replies 41 ·
2
Replies
41
Views
5K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K