Multiple lines

Hi guys, I'm extremely new to java, and have my first assignment on it at university.

My problem is reading in the following .dat file:

1 1.0 Super Cautious

2 2.5 Cautious

3 3.0 Balanced

4 3.5 Growth

5 4.5 Gang Busters

However, I can only read in the first line, and don't know how to read in the other lines. Furthermore, is there a way of using the same varabies AND not having them as arrays (as we haven't learnt about them yet).

Here is my code.

import java.io.*;// Imports java input/output files, allowing to input/output to external files.

import java.util.*;// Imports the appropiate java utilities.

import java.text.DecimalFormat;// Imports text to allow decimal format.

publicclass Assignment1

{

static Scanner console =new Scanner(System.in);

publicstaticvoid main(String[] args)throws FileNotFoundException

{

Scanner inFile =new Scanner(new FileReader("inFile.dat"));// Inputs the .dat file of the necessary data.

DecimalFormat twoDecimal =new DecimalFormat ("0.00");

// Declare the following variables as doubles.

double balance;// The current balance of the superannuation inputted by the employee..

double contrib = 0;// The total amount contributed thus far by the employer AND employee.

double grossPay;// The yearly gross pay earned by the employee.

double interest = 0;// The interest returned from the balance.

double lowLimit = 0;// The lower limit of the median.

double median = 0;// The median amount (before balance is added on).

double upLimit = 0;// The upper limit of the median.

double yearReturn;// The yearly return of the balance.

// Declare the following variables as int's.

int age;// The age of the employee.

int employeeContrib;// The employee contribution of the gross pay.

int employerContrib;// The employer contribution of the gross pay.

int i = 0;// The counter used in the while loop.

int option, option2, option3, option4, option5;// The number of the different stratgies.

int retire;// How many years until the employee retires.

// Declare the following varaibles as strings.

String firstName;// The first word of the strategy name.

String secondName;// The second word of the strategy name (if applicable).

// Following variables are imported from inFile.dat.

option = inFile.nextInt();

yearReturn = inFile.nextDouble();

firstName = inFile.next();

secondName = inFile.next();

System.out.println("Please enter your gross pay: ");

grossPay = console.nextInt();// Allows the user to input gross pay.

System.out.println("Please enter your current age: ");

age = console.nextInt();// Allows the user to input age.

while (age < 18 || age > 70)// While the age is less than 18 or over 70.

{

if (age < 18)// If the age is less than 18.

{

System.out.println("Age is less than 18. Age must be 18 or over and 70 or under.");

System.out.println(" ");

System.out.println("Please enter your current age: ");

age = console.nextInt();// Allows the user to input correct age.

}

elseif (age > 70)// Else if the age is over 70.

{

System.out.println("Age is over 70. Age must be 18 or over and 70 or under.");

System.out.println(" ");

System.out.println("Please enter your current age: ");

age = console.nextInt();// Allows the user to input correct age.

}

}

System.out.println("Please enter how many years to retirement: ");

retire = console.nextInt();// Allows the user to input years to retirement.

System.out.println("Please enter your current balance: ");

balance = console.nextInt();// Allows the user to input their current balance.

System.out.println("Please enter employer contribution (in %): ");

employerContrib = console.nextInt();// Allows the user to input employer contribution.

System.out.println("Please enter employee contribution (in %): ");

employeeContrib = console.nextInt();// Allows the user to input employee contribution.

while (retire > i)// While the inputted retire age is higher than the counter.

{

interest = balance * (yearReturn / 100);// Interest is the balance multiplied by the yearly return (divide 100 to make percentage).

contrib = grossPay * ((employerContrib + employeeContrib) * 0.01);// Contribution is the gross

median = interest + contrib;// Median (before balance) is interest plus total contributions.

balance = balance + median;// The median when the balance is added on.

i++;// The counter goes up by one.

}

// Calculate the lower and upper limits.

lowLimit = balance - (balance * 0.10);// Minus 10% of the median from the median to result in lower limit.

upLimit = balance + (balance * 0.10);// Add 10% of the median from the median to result in upper limit.

// Output the results.

System.out.println("StrategyNameLower LimitMedianUpper Limit");

System.out.print(option +"\t" + firstName +" " + secondName +"\t" + twoDecimal.format (lowLimit) +"\t" + twoDecimal.format (balance) +"\t" + twoDecimal.format (upLimit));

inFile.close();// Closes inFile.dat.

}

}

Cheers guys.

[8804 byte] By [Geovicshaa] at [2007-11-27 1:21:18]
# 1

> Hi guys, I'm extremely new to java, and have my first

> assignment on it at university.

>

> My problem is reading in the following .dat file:

>

> 1 1.0 Super Cautious

> 2 2.5 Cautious

> 3 3.0 Balanced

> 4 3.5 Growth

> 5 4.5 Gang Busters

>

> However, I can only read in the first line, and don't

> know how to read in the other lines. Furthermore, is

> there a way of using the same varabies AND not having

> them as arrays (as we haven't learnt about them

> yet).

>

I don't see in your code that you actually are reading in the first line correctly, since all you ever read from your Scanner is integers, and each line of your data file ends with a string that is clearly not an integer.

However, if you could read in a single line, then you could read in multiple lines by doing the reading inside of a loop.

I would start by trying to just read the file and echo it, which will allow you to visually inspect that you are reading the file correctly, before you worry about whatever else your program is supposed to do. Something like the following psuedocode:

while (not at end of file)

read integer into field1

read float into field2

read string into field3

print field1, field2, field3

If you don't know how to use arrays yet, I would come up with a strategy to re-use the variables you have. You would probably have to loop over the data once you had it stored in order to do what you want with it, so instead, do what you need to in the same loop as you read it. For example, instead of this:

while (not at end of file)

read int into list_of_ints

for (each int in list_of_ints)

add int to total

Try this:

while (not at end of file)

read int into myIntegerVariable

add myIntegerVariable to total

- Adam

guitar_man_Fa at 2007-7-11 23:58:59 > top of Java-index,Java Essentials,New To Java...
# 2

Your program structure is the problem.

One solution is to create a loop the encloses the read of the file and the subsequent console i/o and printing (for the current file line), The intent is to process all information relatice to a file line before reading the next file line, like this:

loop:

read a file line

process console i/o for this file line

print data relative to this file line

loopend

Message was edited by:

ChuckBing

Too late. . .

ChuckBinga at 2007-7-11 23:58:59 > top of Java-index,Java Essentials,New To Java...
# 3
> Message was edited by: > ChuckBing> Too late. . .Sorry, didn't mean to steal your fire... ;)
guitar_man_Fa at 2007-7-11 23:58:59 > top of Java-index,Java Essentials,New To Java...
# 4
That's what I get for being old and slow!
ChuckBinga at 2007-7-11 23:58:59 > top of Java-index,Java Essentials,New To Java...