Results 1 to 4 of 4

Thread: [RESOLVED] Student needs help with Scanner

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Resolved [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!
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width