Tab and/or Table Question?

Is there a way to make the output print under headings like columns on

a simple Java app?

My code is fine, it complies and works and even has an illusion of

printing the way I want it too. But what I want is for the output to actually

print under headings.

//Inventory Program Part 1

//Inventory enters and displays Part information

/*

*Parts used for testing include Glass Break: part # FG730,

*Motion Detector: part# Aurora, and W/L Contact: part # 5816.

*any quantity for units on hand and unit price is appropriate

*for testing purposes.

*/

import java.util.Scanner;// program uses class Scanner

import java.text.*;

publicclass Inventory

{

privatestaticvoid Quit()

{

System.out.println("Goodbye");

System.exit (0);

}

// main method begins execution of Java application

publicstaticvoid main(String args[])

{

Part part =new Part();

// create Scanner to obtain input from command window

Scanner input =new Scanner(System.in);

while(true)// starts loop to test name

{

System.out.print("\nEnter Part Name (stop to quit): ");// prompt for name

part.setName(input.nextLine());// get name

if(part.getName().compareToIgnoreCase("stop") == 0)

{

System.out.println("Stop entered, Thank you");

Quit();

}//end if

System.out.print("Enter part number for " + part.getName() +": ");// prompt

part.setitemNum(input.nextLine());// read part number from user

System.out.print("Enter Units on hand: ");// prompt

part.setunits(input.nextDouble());// read second number from user

if(part.getunits() <= 0)

{

System.out.println ("Invalid amount, Units on hand worked must be positive");

System.out.print("Please enter actual units on hand: ");

part.setunits(input.nextDouble());

}//end if

System.out.print("Enter Unit Price: $ ");// prompt

part.setprice(input.nextDouble());// read third number from user

if(part.getprice() <= 0)

{

System.out.println ("Invalid amount, Unit Price must be positive");

System.out.print("Please enter actual unit price: $ ");

part.setprice(input.nextDouble());

}//end if

System.out.println("Part #\tPart Name\tUnits On Hand\tUnit Cost\tTotal Cost" );

System.out.printf("%s\t%s\t%.2f\t%.2f\t$%.2f\n",

part.getitemNum(), part.getName(), part.getunits(), part.getprice(), part.getvalue());

input.nextLine();

}//end while

}// end method main

}// end class Inventory

// Class Part holds Part information

class Part

{

private String name;

private String itemNum;

privatedouble units;

privatedouble price;

//default constructor

public Part()

{

name ="";

itemNum ="";

units = 0;

price = 0;

}//end default constructor

//Parameterized Constructor

public Part(String name, String itemNum,double units,double price)

{

this.name = name;

this.itemNum = itemNum;

this.units = units;

this.price = price;

}//end constructor

publicvoid setName(String name){

this.name = name;

}

String getName()

{

return name;

}

publicvoid setitemNum ( String itemNum )

{

this.itemNum = itemNum;

}

public String getitemNum()

{

return itemNum;

}

publicvoid setunits (double units )

{

this.units = units;

}

publicdouble getunits()

{

return units;

}

publicvoid setprice (double price )

{

this.price = price;

}

publicdouble getprice()

{

return price;

}

publicdouble getvalue()

{

return (units * price);

}

}//end Class Part

[7846 byte] By [Kelelaa] at [2007-11-27 11:00:24]
# 1

Use system.out.printf() method with an appropriate format string(*). Give your half-day or a full-day to try and learn the method if it is your first time to use the C legacy.

(*) Don't use TAB in the format string. Beware that creation of the format string can be dynamic.

Message was edited by:

hiwa

hiwaa at 2007-7-29 12:29:26 > top of Java-index,Java Essentials,New To Java...
# 2

My printf() skill is rather rusty but this might help:

import java.util.*;

public class InventoryX{

private static void Quit(){

System.out.println("Goodbye");

System.exit (0);

}

public static void main(String args[]){

Part part = new Part();

Scanner input = new Scanner(System.in);

while (true){

System.out.print("\nEnter Part Name (stop to quit): ");

part.setName(input.nextLine());

if (part.getName().equalsIgnoreCase("stop")){

System.out.println("Stop entered, Thank you");

Quit();

}

System.out.print("Enter part number for " + part.getName() + ": ");

part.setItemNum(input.nextLine());

System.out.print("Enter Units on hand: ");

part.setUnits(input.nextDouble());

while (part.getUnits() <= 0){

System.out.println

("Invalid amount, Units on hand worked must be positive");

System.out.print("Please enter actual units on hand: ");

part.setUnits(input.nextDouble());

}

System.out.print("Enter Unit Price: $ ");

part.setPrice(input.nextDouble());

while (part.getPrice() <= 0){

System.out.println ("Invalid amount, Unit Price must be positive");

System.out.print("Please enter actual unit price: $ ");

part.setPrice(input.nextDouble());

}

String fmtStr = "%-7s %-10s %14s %14s %14s\n";

System.out.printf(fmtStr, "Part #", "Part Name",

"Units On Hand", "Unit Cost", "Total Cost");

String inum = part.getItemNum();

String name = part.getName();

String units = String.format("%.2f", part.getUnits());

String price = String.format("%.2f", part.getPrice());

String value = String.format("%.2f", part.getValue());

System.out.printf(fmtStr, inum, name, units, price, value);

input.nextLine();

}

}

}

class Part{

private String name;

private String itemNum;

private double units;

private double price;

public Part(){

name = "";

itemNum = "";

units = 0;

price = 0;

}

public Part(String n, String m, double u, double p){

name = n;

itemNum = m;

units = u;

price = p;

}

public void setName(String n) {

name = n;

}

String getName(){

return name;

}

public void setItemNum (String m){

itemNum = m;

}

public String getItemNum(){

return itemNum;

}

public void setUnits (double u){

units = u;

}

public double getUnits(){

return units;

}

public void setPrice (double p){

price = p;

}

public double getPrice(){

return price;

}

public double getValue(){

return (units * price);

}

}

hiwaa at 2007-7-29 12:29:26 > top of Java-index,Java Essentials,New To Java...