Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; import java.io.FileNotFoundException; import java.io.F

ID: 3759885 • Letter: I

Question

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class Greencrud
{
public static void main(String[]args)
{
Scanner inputStream = null;
PrintWriter outputStream = null;
try
{
inputStream = new Scanner (new FileInputStream ("greencrud.txt"));
outputStream = new PrintWriter( new FileOutputStream("crudout.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File not found.");
System.exit(0);
}
while (inputStream.hasNextLine())
{
int a=0, b=1;// assume the previos one is a and the later one is b
int f=0;// f is equal to the sum of a and b
int total=0;
int times=0;
int population = inputStream.nextInt();
int days = inputStream.nextInt();
int next = days/5;
// pattern: 0 1 1 2 3 5 8 13 21 34......
//next is how many numbers after the second 1
while (times<=next-2)//next-2 -> set it back to the first number of the series
{
times++;
f = a+b;
a=Math.max(a,b); // change the value to new a and new b
b=f;
}
total=f*population;
outputStream.println("A green crud population starts out as "+ population+" pounds of crud");
outputStream.println("It grows to "+total+" pounds of crud in "+days+" days");
outputStream.close();
inputStream.close();
}
}
}

Input file: greencrud.txt

12 10

4 30

28 18

7 13

17 10

9 35

10 20

When I call the method, a problem occurs for int population = inputStream.nextInt();. It reminds me that java.lang.IllegalStateException: Scanner closed. Also, I can only output the first line of data in greencrud.txt to crudout.txt. How can I fix the problem so that all set of datas can be printed on crudout.txt?

Explanation / Answer

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class Greencrud
{
public static void main(String[]args)
{
Scanner inputStream = null;
PrintWriter outputStream = null;
try
{
inputStream = new Scanner (new FileInputStream ("greencrud.txt"));
outputStream = new PrintWriter( new FileOutputStream("crudout.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File not found.");
System.exit(0);
}
while (inputStream.hasNextLine())
{
int a=0, b=1;// assume the previos one is a and the later one is b
int f=0;// f is equal to the sum of a and b
int total=0;
int times=0;
int population = inputStream.nextInt();
int days = inputStream.nextInt();
int next = days/5;
// pattern: 0 1 1 2 3 5 8 13 21 34......
//next is how many numbers after the second 1
while (times<=next-2)//next-2 -> set it back to the first number of the series
{
times++;
f = a+b;
a=Math.max(a,b); // change the value to new a and new b
b=f;
}
total=f*population;
outputStream.println("A green crud population starts out as "+ population+" pounds of crud");
outputStream.println("It grows to "+total+" pounds of crud in "+days+" days");
outputStream.close();
inputStream.close();
}
}
}