This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

reading from one file, calculating, writing to another...  RSS feed

 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hey guys/gals-- I'm back for another adventure...

I have to disclose that I'm, not only a Java newbie, but also a OOP newbie as well. I also have to mention that this is part of my homework for an IS class. Please don't run away when you hear that it's part of my homework. I'm really trying to learn this for myself and not just trying to get others to do it for me. I mean this genuinely!

With that being said, I'm trying to write a program that reads from "customer.txt" extracts the data, performs calculations, and then outputs the results to another file "surcharge.txt". I'm having a hard time with the whole I/O part of it. I can output it to the console, no prob, but I am not able to write it to another file. Here's what pseudocode I have so far, double check my logic:

it has to come from two different classes (per requirements):

"customer.txt"
235678,20,true,false,-1
698090,24,true,true,3.12
// customerID, age, hadAccident, hasCollegeEd, gradePointAverage


public class Customer {
// instable class
private instance variables (i.e. see above)
public double calculateSurcharge(parameters)
// tests for various conditions
// return surcharge;
public void write()
// writes (customerID and surcharge) to "surcharge.txt"
}

public class Test {
// Here's where I have trouble..
main() {
try
String line = read file("customer.txt");
while file != null
String[] parts = line.split(",");

String customerID = parts[0];
int age = Integer.parseInt(parts[1]);
boolean hadAccident = Boolean.parseBoolean(parts[2]);
boolean hasCollegeEd = Boolean.parseBoolean(parts[3]);
double gradePointAverage = Double.parseDouble(parts[4]);

/********************
*
* Here's where I'm stuck. Once I've read the file
* and split the text into the different elements in
* the array, how do I pass them to another class so
* I can run calculateSurcharge() on them?
*
**********************/

Customer c = new Customer();
c.calculateSurcharge(arguments);
c.write()

// I'm guessing this is how you'd do it, right?

catch (Exception e)
Display I/O error message
}

Am I on the right track? Thanks in advance for any help...
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks for the refreshing honesty, Brian. We're glad to help with homework, we just don't like fools who try to get us to do it for them.
Have you looked at the Java Tutorial? It covers much of the Java Language, including Reading and Writing. Basically you want to create some sort of Reader or Stream to do output and write values to that. Which particular Reader or Stream you use depends on what your requirements are. In general terms, a Reader is used to write text and a Stream is used to write binary data.
 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Report post to moderator
Yes, I'm vaguely familiar with the tutorial on Sun's website. However it's mostly done when all in the same class. The problem I'm having requires one class to read, another to calculate, and then the original class to write the calculations. Am I wrong?
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have it now. You're asking a design question, not an IO question.
My rule of thumb is: "does it work"
In your case, Test invokes write() on Customer and Customer would write himself to file. I don't see any reason why it would not work.
 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Report post to moderator
Well it's sort of both I guess, a design question and an I/O question. I wanted someone to tell me that my logic was on the right track and then point me in the right direction on how to implement the I/O part of it. I sorta figured it out I can get it to read/calculate/write now. However, I'm having difficulty with the output now so I've moved it to the Java General (beginner) thread now.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!