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.

