|
-
Mar 26th, 2009, 11:47 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Student needs help with Scanner
I am creating a program for my school. The program is supposed to get input from the user and print it out in a formatted way. However, I am having a problem. After my second loop through my methods two of the methods end up on the same line. Take a look at my code and then the ouput it produces. Please tell me what I am doing wrong.
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package invprogram;
import java.util.Scanner;
/**
*
* @author mbutler
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//SOFTWARE programs;
SOFTWARE programs=new SOFTWARE(null,null,null,0,0);
//Display welcome message to user
System.out.println("Welcome to the inventory program.");
System.out.println("This program has been designed to");
System.out.println("track computer software.");
//Display whitespace
System.out.println();
System.out.println();
//numRecrods is a variable that is set to ask the user how many
//records he or she wants to set in that session
int numRecords=0;
//Setup the scanner for use
Scanner input =new Scanner(System.in);
//Ask the user how many records he or she wants to create
//This information will be plugged into the array
System.out.print("How many records would you like to add? ");
numRecords=input.nextInt(); //prompt for how many records to add
if (numRecords<=0)
{ //start if
do //Start do to ensure that a positive number is entered
{ //start do...while
System.out.println("Number of records to add MUST be more than 0.");
System.out.print("How many records would you like to add? ");
numRecords=input.nextInt();
} while (numRecords<=0);//end do...while
} //end if
//Create some whitespace
System.out.println();
System.out.printf("This program is going add %d record(s).\n", numRecords);
//Create a string array to hold the item number
String arrayItemNum[]=new String[numRecords];
//Create a string array to hold the software title
String arraySoftTitle[]=new String[numRecords];
//Create a string array to hold the serial number
String arraySerialNumber[]=new String[numRecords];
//Create an int array to hold the qty on hand
int arrayQty[]=new int[numRecords];
//Create a double array to hold the price
double arrayPrice[]=new double[numRecords];
//Create a double array to hold the total of the software
//Format: total=qty*price per unit
double arrayTotal[]=new double[numRecords];
//End of array creation
int counter=0;
while(counter!=numRecords)
{
System.out.printf("\ncounter=%d",counter);
System.out.printf("\nnumRecords=%d\n", numRecords);
programs.setItemNumber(arrayItemNum[counter]);
programs.setSoftwareTitle(arraySoftTitle[counter]);
programs.setSerialNumber(arraySerialNumber[counter]);
programs.setNumberOfUnits(arrayQty[counter]);
programs.setPricePerUnit(arrayPrice[counter]);
//Output some white space
System.out.println();
System.out.println();
//Output item number. Part 2 may work with hash codes if I can get
//them to work Math.abs does not seem to spit out an absolute number
//thus some hash codes are negatives
System.out.printf("%s: %s\n","Item Number.....",programs.getItemNumber());
//Output the software title
System.out.printf("%s: %s\n","Software Title..",programs.getSoftwareTitle());
//Output the serial number
System.out.printf("%s: %s\n","Serial Number...",programs.getSerialNumber());
//Output the number of units
System.out.printf("%s: %s\n","Qty on hand.....",programs.getNumberOfUnits());
//Output the price per unit
System.out.printf("%s: %2s\n","Price per Unit..",programs.getPricePerUnit());
//Figure out how to calculate total value using the array
//System.out.printf("%s: %s\n","Total Value.....",arrayTotal[counter]);
//Make output look like a spreadsheet for Part 2
counter=counter+1;
//counter=counter+1;
}
} //end public main
} //end class main
class SOFTWARE
{ //start of class SOFTWARE
//Declare variables for the program
//itemNumber=Item Number
//softwareTitle=Software title
//serialNumber=Serial Number
//numberOfUnits=Quantity on hand
//pricePerUnit=Price per unit
//totalOfUnits=Total cost of all units
//totalOfInventory=Total amount of all items in inventory
private String itemNumber=null;
private String softwareTitle=null;
private String serialNumber=null;
private int numberOfUnits=0;
private double pricePerUnit=0.0;
private double totalOfUnits=0.0;
private double totalOfInventory=0.0;
//Import scanner for use
Scanner input =new Scanner(System.in);
//Setup 5 item constructor for use
public SOFTWARE(String itemNum, String title, String sn, int numUnits, double ppUnit)
{ //start of constructor
itemNumber=itemNum;
softwareTitle=title;
serialNumber=sn;
numberOfUnits=numUnits;
pricePerUnit=ppUnit;
} //end constructor
//Start of methods
//Methods to set things
public void setItemNumber(String itemNum)
{ //start setItemNumber
//System.out.println();
System.out.print("Enter the item number you would like to use: ");
itemNum=input.nextLine();
itemNumber=itemNum;
} //end setItemNumber
public void setSoftwareTitle(String title)
{ //start setSoftwareTitle
System.out.print("Enter the software title: ");
title=input.nextLine();
softwareTitle=title;
} //end setSoftwareTitle
public void setSerialNumber(String sn)
{ //start setSerialNumber
System.out.print("Enter the serial number: ");
sn=input.nextLine();
serialNumber=sn;
} //end setSerialNumber
public void setNumberOfUnits(int numUnits)
{ //start setNumberOfUnits
System.out.print("Enter the quantity to inventory: ");
numUnits=input.nextInt();
numberOfUnits=numUnits;
} //end setNumberOfUnits
public void setPricePerUnit(double ppUnit)
{ //start SetPricePerUnit
System.out.print("Enter the price per unit: $");
ppUnit=input.nextDouble();
pricePerUnit=ppUnit;
} //end SetPricePerUnit
//Methods to get and return data
public String getItemNumber()
{ //start of getItemNumber
return itemNumber;
} //end getItemNumber
public String getSoftwareTitle()
{ //start of getSoftwareTitle
return softwareTitle;
} //end of getSoftwareTitle
public String getSerialNumber()
{ //start of getSerialNumber
return serialNumber;
} //end of getSerialNumber
public int getNumberOfUnits()
{ //start of getNumberOfUnits
return numberOfUnits;
} //end of getNumberOfUnits
public double getPricePerUnit()
{ //start of getPricePerUnit
return pricePerUnit;
} //end of getPricePerUnit
//public double calcTotalOfInventory()
//{ //start of calcTotalOfInventory
//
//} //end of calcTotalOfInventory
} //end of class SOFTWARE
Output:
run:
Welcome to the inventory program.
This program has been designed to
track computer software.
How many records would you like to add? 2
This program is going add 2 record(s).
counter=0
numRecords=2
Enter the item number you would like to use: MSOFFICE2007
Enter the software title: Microsoft Office 2007
Enter the serial number: asdfd-qwerd-qwerd-qwetrd-qwetd
Enter the quantity to inventory: 50
Enter the price per unit: $200
Item Number.....: MSOFFICE2007
Software Title..: Microsoft Office 2007
Serial Number...: asdfd-qwerd-qwerd-qwetrd-qwetd
Qty on hand.....: 50
Price per Unit..: 200.0
counter=1
numRecords=2
Enter the item number you would like to use: Enter the software title: here's the problem
Enter the serial number: 346
Enter the quantity to inventory: 3
Enter the price per unit: $3
Item Number.....:
Software Title..: here's the problem
Serial Number...: 346
Qty on hand.....: 3
Price per Unit..: 3.0
BUILD SUCCESSFUL (total time: 1 minute 9 seconds)
Last edited by mbutler755; Mar 29th, 2009 at 09:26 AM.
Reason: Resolved!
-
Mar 26th, 2009, 11:49 AM
#2
Re: Student needs help with Scanner
Thread moved from CodeBank forum (which is for you to post your code examples, not questions)
-
Mar 27th, 2009, 01:27 PM
#3
Re: Student needs help with Scanner
I have no clear idea of what is causing the problem, but I found out that if you reconstruct the input scanner instance the problem is solved.
I guess this has to do with using the printf. Try looking it up in the Java Bugs DB or perhaps the Sun forums. If no one has mentioned this, try reporting it... You'll be student of the Year
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 29th, 2009, 09:25 AM
#4
Thread Starter
Hyperactive Member
Re: Student needs help with Scanner
So, the problem was with the scanner. I should not have been using . I am now using and that is working perfectly.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|