I keep getting an error on the lines:     private static String pad (String str, int n) {         if (str.length() > n)             return str.substring (0, n);         while (str.length() < n)             str += " ";         return str;     } The program says that there is an exception but I cannot figure out where it is occuring within these lines. Can you please help me with this? Thanks so much.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I have provided the full code for my issue; I am getting an exception error that I do not understand. 

import java.util.Scanner;

class Part {

    private int number;
    private String name;
    private int quantity;

///////////////////

    public Part(int number, String name, int quantity) {
        this.number = number;
        this.name = name;
        this.quantity = quantity;
    }

//////////////////////

    public Part(int number, String name){}
        public Part() {
    }

    public void decreaseQuantity (int amount) {
        quantity -= amount;
    }

/////////////////////////

    public String getName(){
        return name;
    }

////////////////////

    public int getNumber () {
        return number;
    }

/////////////////////

    public int getQuantity () {
        return quantity;
    }

/////////////////////

    public void increaseQuantity(int amount) {
        quantity += amount;
    }

/////////////////////////

    public String toString() {
        return pad (number + " ", 5) + " " + pad(name, 20) + " " +
            quantity;
    }

///////////////////////////

    private static String pad (String str, int n) {
        if (str.length() > n)
            return str.substring (0, n);
        while (str.length() < n)
            str += " ";
        return str;
    }

// Provided for testing purposes only:

    public static void main (String [] args) {
        Scanner scan = new Scanner (System.in);

// Prompt user to enter a part:

    System.out.println("Enter part number: ");
    int number = scan.nextInt();
    System.out.println("Enter part name: ");
    String name = scan.next();
    System.out.println("Enter quantity: ");
    int quantity = scan.nextInt();

// Test constructors:

    Part part = new Part (number, name, quantity);
    System.out.println("\nPart using first constructor: " + part);
    System.out.println("Part using second constructor: " + new Part(number, name));
    System.out.println("Part using no-arg constructor: " + new Part());

// Test getters:

    System.out.println("\nNumber: " + part.getNumber());
    System.out.println("Name: " + part.getName());
    System.out.println("Quantity: " + part.getQuantity());
    System.out.println("\nEnter amount of increase: ");
    int amount = scan.nextInt();
    part.increaseQuantity(amount);
    System.out.println("Part is now: " + part);
    System.out.println("\nEnter amount of increase: ");
    amount = scan.nextInt();                                                                                         

    part.decreaseQuantity(amount);
    System.out.println("Part is now: " + part);
    } // End of main

} // End of class

I keep getting an error on the lines:

    private static String pad (String str, int n) {
        if (str.length() > n)
            return str.substring (0, n);
        while (str.length() < n)
            str += " ";
        return str;
    }

The program says that there is an exception but I cannot figure out where it is occuring within these lines. Can you please help me with this? Thanks so much.

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
InputStream
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education