Java help


by Nothing000
Tags: java
Nothing000
Nothing000 is offline
#1
Oct16-06, 10:43 PM
P: 442
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 ammount 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".
Phys.Org News Partner Science news on Phys.org
Going nuts? Turkey looks to pistachios to heat new eco-city
Space-tested fluid flow concept advances infectious disease diagnoses
SpaceX launches supplies to space station (Update)
Nothing000
Nothing000 is offline
#2
Oct16-06, 10:46 PM
P: 442
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?
0rthodontist
0rthodontist is offline
#3
Oct16-06, 10:50 PM
Sci Advisor
P: 1,253
It means that there will be a text file that you will read your data from instead of typing stuff in at the keyboard. Some simple examples of this can be found at
http://www.javacoffeebreak.com/java103/java103.html

Nothing000
Nothing000 is offline
#4
Oct16-06, 11:51 PM
P: 442

Java help


I don't get it.
0rthodontist
0rthodontist is offline
#5
Oct16-06, 11:54 PM
Sci Advisor
P: 1,253
What exactly don't you get? Do you know what a text file is?
Nothing000
Nothing000 is offline
#6
Oct16-06, 11:56 PM
P: 442
By text file do you mean like a file of the format .txt? Like a file created with notepad?
Nothing000
Nothing000 is offline
#7
Oct16-06, 11:57 PM
P: 442
Do I just create an input file like normal?
Nothing000
Nothing000 is offline
#8
Oct16-06, 11:57 PM
P: 442
And then somehow call on that file from the main program when it needs the input?
Nothing000
Nothing000 is offline
#9
Oct17-06, 12:00 AM
P: 442
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?
0rthodontist
0rthodontist is offline
#10
Oct17-06, 12:07 AM
Sci Advisor
P: 1,253
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.
Nothing000
Nothing000 is offline
#11
Oct17-06, 12:10 AM
P: 442
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.
0rthodontist
0rthodontist is offline
#12
Oct17-06, 12:12 AM
Sci Advisor
P: 1,253
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.
Nothing000
Nothing000 is offline
#13
Oct17-06, 12:13 AM
P: 442
Quote Quote by 0rthodontist
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.
0rthodontist
0rthodontist is offline
#14
Oct17-06, 12:19 AM
Sci Advisor
P: 1,253
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.
Nothing000
Nothing000 is offline
#15
Oct17-06, 12:22 AM
P: 442
Quote Quote by 0rthodontist
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.
Nothing000
Nothing000 is offline
#16
Oct17-06, 12:25 AM
P: 442
So I would write the program like this normal?
Nothing000
Nothing000 is offline
#17
Oct17-06, 12:30 AM
P: 442
Quote Quote by 0rthodontist
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?
0rthodontist
0rthodontist is offline
#18
Oct17-06, 12:33 AM
Sci Advisor
P: 1,253
Quote Quote by Nothing000
Quote Quote by 0rthodontist
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
This is code-style
{
    text
}


Register to reply

Related Discussions
java q Computing & Technology 3
new to java Programming & Computer Science 7
Java help Computing & Technology 15
Help in Java 3D Computing & Technology 0
Java Introductory Physics Homework 13