1

I have tried many different things trying to get this to work. I am sorry for just a lame question I am a total newbie. When I request information from my inFile does it need to be in the order the data is in?

This is the inData.txt :

10.20  \\\length
5.35   \\\width
15.6   \\\radius
Randy Gill   \\\first last name
31 \\\age
18500 \\\bank account balance
3.5 \\\ interest rate
A \\char 'a'

I am supposed to print dimensions of a rectangle, circle. Name with bank account info. Thanks for the help!

import java.util.*;
import java.io.*;


public class ProgrammingExercise3_1   
{
   public static void main(String[] args) throws FileNotFoundException
   {

      double rectWidth;
      double rectLength;
      double radius;
      int age;
      double begBal;
      char A;
      String name;
      double rate;

      Scanner inFile = new Scanner(new FileReader("C:\\Users\\sierr_000\\Desktop\\Sam School\\IT-145\\Exercises\\Ch 3\\inData.txt"));

      PrintWriter outFile = new PrintWriter("C:\\Users\\sierr_000\\Desktop\\Sam School\\IT-145\\Exercises\\Ch 3\\outData.out");

      rectWidth = inFile.nextDouble();
      rectLength = inFile.nextDouble();

      outFile.println("Rectangle: ");
      outFile.println("Length = " + rectLength + ", width = " + rectWidth + ", area = "
                     + (rectWidth*rectLength) + ", perimeter = " + (2 * (rectWidth + rectLength)));

      radius = inFile.nextDouble();

      outFile.println("Circle: ");
      outFile.println("Radius = " + radius + ", area = " + (radius*3.1416) + "Circumfrence = " + (2*3.1416*radius));

      name = inFile.next();
      age = inFile.nextInt();

      outFile.println("Name: " + name + ", age: " + age);

      begBal = inFile.nextDouble();
      rate = inFile.nextDouble();

      outFile.printf("Beginning Balance: %.2f %n" , begBal + "interest rate: %.2f" , rate);
      outFile.println("The character that comes after A in the ASCII is B");      




      inFile.close();
      outFile.close();

   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • `"When I request information from my inFile does it need to be in the order the data is in?"` -- yes it does. The Scanner object scans through your file linearly, token by token. – Hovercraft Full Of Eels Apr 05 '16 at 23:29

1 Answers1

0

You were off to a good start! I found 2 issues with your code which were causing issues.

The name variable should be assigned this way:

name = inFile.next()+" "+inFile.next();

name = inFile.next() only gets "Randy" and messing up the age variable.

Next, This line was causing issues:

outFile.printf("Beginning Balance: %.2f %n" , begBal + "interest rate: %.2f" , rate);

I split it into a series of outFile.print and outFile.printf statements.

  outFile.print("Beginning Balance: ");
  outFile.printf("%.2f %n", begBal);
  outFile.print("interest rate: ");
  outFile.printf("%.2f", rate);

After those 2 issues, everything seems to work. I hope this helps!

runningviolent
  • 317
  • 3
  • 13