I have a file with some info of a store and I want to sort it in another file like a table(in Excel).
the file :
001 Tablets 5 3
002 pens 4 1
005 Computeres 3 0
003 Bages 2 1
004 USB 4 0
I write this code :
import java.util.*;
import java.io.*;
public class Sort {
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);
int id, quantity, soldQuantity;
String title;
pw.println("ID\tTitle\t\t\tQuantity\tSoldQuantity");
pw.println("");
while(infile.hasNext()){
id = infile.nextInt();
title = infile.next();
quantity = infile.nextInt();
soldQuantity = infile.nextInt();
pw.printf("%6d\t%s\t\t%d\t%d%n", id , title, quantity, soldQuantity);
}
infile.close();
pw.close();
}
}
and I want it to look like this :
Code(ID) Name Quantity SoldQuantity
001 Tablets 5 3
002 pens 4 1
005 Computeres 3 0
003 Bages 2 1
004 USB 4 0
my problem is the number of the quantity and sold quantity , it don't fit well . looks like this :
Code(ID) Name Quantity SoldQuantity
001 Tablets 5 3
002 pens 4 1
005 Computers 3 0
003 Bags 2 1
004 USB 4 0
The problem happens when the size of the name is different if there is a name bigger than "computers" , how can I handle it ?
Thanks