Question

I am trying to read a text file, and then display the output in another file. I can only read using Scanner. input.txt

3005045 7
3245436 0
7543536 3
8684383 -1

output.txt should be like

ID    Number of Bags   Total Cost
**    **************   **********

customer pays 20.50 per bag if the bag is 4 or less. and pays 15.50 per bag if the bag greater than 4. but if it's 0 or negative number this message should appeared "Error : Wrong Number of Bags" I did this program, but it works only once(reads one line only)

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

public class Bags {
public static void main(String []args) throws IOException {
    FileInputStream fileinput = new FileInputStream("input.txt");
    FileOutputStream fileoutput = new FileOutputStream("output.txt");
    Scanner infile = new Scanner(fileinput);
    PrintWriter pw = new PrintWriter(fileoutput);
    double total = 0, line = 0;
    int bags = 0, ID = 0, count = 0;
    pw.println("ID\t\tNumber of Bags\t\t\tTotal Cost");
    for(int i = bags; i >= 0; i++, count++){
        ID = infile.nextInt();
        i = infile.nextInt();
        if (i <= 0){
            pw.println(ID + "\tError: Wrong Number of Bags\t\t\t");
            break;
        }
        else if (i <= 4){
            total = (80.50)*i;
            pw.printf("%d\t\t%d\t\t\t\t%.2f", ID, i, total);
            break;
        }
        else {
            total = ((80.50)*4)+((75.50)*(i-4));
            pw.printf("%d\t\t%d\t\t\t\t%.2f", ID, i, total);
            break;
        }
    }
    infile.close();
    pw.close();
}
}
Was it helpful?

Solution

You don't need that for loop over there. Also, you want to read line by line. Here is quick fix of your code:

public class Bags {

public static void main(String[] args) throws IOException {
    FileInputStream fileinput = new FileInputStream("input.txt");
    FileOutputStream fileoutput = new FileOutputStream("output.txt");
    Scanner infile = new Scanner(fileinput);
    PrintWriter pw = new PrintWriter(fileoutput);
    double total = 0, line = 0;
    int bags = 0, ID = 0, count = 0;
    pw.println("ID\t\tNumber of Bags\t\t\tTotal Cost");
    while(infile.hasNext()){

        ID = infile.nextInt();
        int i = infile.nextInt();
        if (i <= 0) {
            pw.println(ID + "\n\t\tError: Wrong Number of Bags\t\t\t");

        } else if (i <= 4) {
            total = (80.50) * i;
            pw.printf("%d\t\t%d\t\t\t\t%.2f", ID, i, total);

        } else {
            total = ((80.50) * 4) + ((75.50) * (i - 4));
            pw.printf("%d\t\t%d\t\t\t\t%.2f", ID, i, total);

        }
    }
    infile.close();
    pw.close();
}
}

Output.txt

ID      Number of Bags                  Total Cost
3005045 7                               548.503245436
        Error: Wrong Number of Bags         
7543536 3                               241.508684383
        Error: Wrong Number of Bags         

OTHER TIPS

You should not use i to save "number of bags". See the line i = infile.nextInt();. Use another variable, then you should be fine. Also, you should keep reading until end of file, so you probably wouldn't be able to write a for (int i = 0; i < n; i++)-style of loop.

There is no surprise that loop can iterates only one time. In each case you have break.

Also in this case you shouldn't be using for loop, and especially not the way you are using it now. Just take a look at it, your loop would end only when this condition i >= 0 would be false, which means i would have to be negative, but even when i would become -1 like last number from your input it would still be incremented at the end of iteration thanks to i++ so you would end up with 0 >= 0 condition which is true, so loop would try to iterate again)

Instead use

while(scanner.hasNextInt())

This way you will make sure that you will read int from file only when there will be next one to read. Just use your predefined bugs variable instead of i.

Another thing is that you are not including line separators in your printf formats. Add %n at the end of each one of them, and don't use \t but specify space you want each number to hold like

pw.printf("%d %9d %40.2f%n",...);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top