Ask your own question, for FREE!
Computer Science
OpenStudy (anonymous):

Please, I need help. In java, how can I get input from a text file, specifically in a payroll program? I really don't have any idea. Please help.

OpenStudy (anonymous):

is the problem in how to get the input from the text file , or what data do u need to get from the text file ?

OpenStudy (e.mccormick):

Is it a formatted text file or are you going to have to seach for the information? If formatted, you can read it in a line at a time and deal with it as it comes. If it is not, then you have to seach through for key terms that signal the data you need.

OpenStudy (e.mccormick):

Basics of I/O in Java: http://www.tutorialspoint.com/java/java_files_io.htm

OpenStudy (anonymous):

The data in the text file is not formatted and I have format it in the program, I don't know how to do it.

OpenStudy (e.mccormick):

You generally will read in the file one line at a time or the whole thing if it is not huge. As it is read in, you make it into a string or string array. Then you search through the strings for key markers. For example, if you want a name and in the file it is always preceded by the word name and a colon, then you search for "Name:" and then find what follows it until there is a space. Sometimes it is easiest to turn the string into an array then loop through the array. Whenever you find a key, you know the next value is a data element to link with that key. This shows some examples: http://www.javapractices.com/topic/TopicAction.do?Id=87

OpenStudy (anonymous):

Okay thanks, I'll try to figure it out.

OpenStudy (espex):

If you can read the file, you can then tokenize the input. Once the token is read you can evaluate it and do with it what you will.

OpenStudy (anonymous):

How? Can you give me an example? about token

OpenStudy (espex):

Take a look at this sample. http://www.tutorialspoint.com/java/io/streamtokenizer_nexttoken.htm

OpenStudy (anonymous):

Thanks for the link, I'll study about it

OpenStudy (e.mccormick):

And with tokens you can do the same basic thing I said. Look at each token in order. When one is the keyword you need that denotes the next item is useful data, you trigger reading the next into some data holder. You said it is a payroll file of some sort. So lets say one of the fields is "Hours: 36" When you get the token of "Hours:" you know the next item is the number of hours worked. Now, that depends on a certain sequence to the file. There may be a better choice. Only looking at the file will let you know.

OpenStudy (anonymous):

I still don't understand. Would you mind if I post here the problem? Please help me with the next step. I really don't know. Thanks in advance.

OpenStudy (e.mccormick):

Ah, so it is a formatted text file. You just need to read in line by line and process them into an object. They want the object version of the variables so that it becomes pass by reference. Were you able to understand any of the file read in methods? Here is the fourth video in a set on dealing with files: http://thenewboston.org/watch.php?cat=31&number=81 It covers the open and read from files part and handles data that is formatted.

OpenStudy (e.mccormick):

A lot of what we have been telling you was based on it being an unformated file. However, it is formatted. An unformatted file is like these messages. Searching through something like that fora name or numbers. A formatted file has some structure or organization. The structure of yours is: Name # # # The names and the three numbers repeat in that way. That means you just need to be able to do a basic read in of the file. You can use the tokenizer or do some more basic file reads and formatting. I would work with a separate program just to learn the file i/o. Use your payroll file and work with it. Just see if you can read in the names and numbers as individual items and print them. Try and get them into the proper data types for use in the payroll program. Once you have that working, you can use what you figure out as the file retrieval part of the payroll program.

OpenStudy (anonymous):

Thanks, I've done it already. But, I have a new problem. The compiler says " operator + cannot be applied to double, DoubleClass " DoubleClass is a class I created, and I want to add a variable of type DoubleClass to a variable of type double. What should I do to fix this?

OpenStudy (e.mccormick):

It depends on what you want to do with a + operator. You can overload it or you might just want to build a method to get a value that you can use the existing + operator with.

OpenStudy (anonymous):

I use the Scanner class to read the text file. hourlyRate = sc.nextDouble(); but hourlyRate is declared as a reference variable of class DoubleClass so nextDouble() cannot be used. What should I use instead?

OpenStudy (e.mccormick):

Did you put getters and setters in your class?

OpenStudy (anonymous):

Here's the class' content: public class DoubleClass { private double x; public DoubleClass() { x = 0.0; } public void setNum(double num) { x = num; } public double getNum() { return x; } } I'm not sure if that's correct

OpenStudy (e.mccormick):

Then you need to use setNum(double num) to set values.

OpenStudy (e.mccormick):

hourlyRate.setNum(sc.nextDouble());

OpenStudy (anonymous):

How about this: totalGrossAmount = totalGrossAmount + grossAmount; totalGrossAmount is of type double and grossAmount is also a reference variable of class Double. I want to add them.

OpenStudy (e.mccormick):

Because you have them in that class, you need to use the get and set methods.

OpenStudy (anonymous):

I find it hard, I don't understand. Anyways, thank you so much.

OpenStudy (e.mccormick):

I also kind of wondered why you made a new class object for doubles rather than use the builtin one. ``` public class Test { public static void main(String[] args) { Double d1, d2; // Objects double d3, d4; // Basic types d1=1.1; d2=2.2; d3=d1; d4=d2+d3; System.out.println("d1:" + d1 + " d2:" + d2 + " d3:" + d3 + " d4:" + d4); } } ``` When run it makes: d1:1.1 d2:2.2 d3:1.1 d4:3.3000000000000003 So even though one is an object, you can use = and + because they are built in with the operator overloading.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!