i am trying to enter a book title "hoopa doopa"into my object array. when i try it throws a java.util.InputMismatchException.If i enter a string that has no spaces like"hoopa" the code will run fine all of the way through. What is causing this and how can I fix it? please help thanks
import java.io.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int counter = 0;
int numberOfProducts=0; //variable for when the user is asked to enter input the number of products to be entered
do { //this will validate the user input
System.out.println("How many products would you like to enter");
while (!input.hasNextInt()) {
System.out.println("That's not a number!");
input.next(); // this is important!
}
numberOfProducts = input.nextInt();
} while (numberOfProducts <= 0);
//end of the do while loop
Products[] products;
products = new Products[numberOfProducts+4];//create a array the size of the user input that was gathered
for (int i=0;i<numberOfProducts;i++)
{
products[i+4]= new Products(); // create each actual Person
System.out.println("What is the product #: ");
products[i+4].setItemNumber(input.nextInt());
System.out.println("What is the book name: ");
products[i+4].setNameOfProduct(input.next());
System.out.println("How many are in stock: ");
products[i+4].setUnitsInStock(input.nextInt());
System.out.println("What is the cost of the Item: ");
products[i+4].setPrice(input.nextDouble());
counter++;
}
products[0] = new Products(0001,"The Red Rose",1,29.99);
products[1] = new Products(0002,"The Bible",3,11.99);
products[2] = new Products(0003,"End of the Programm",2,29.99);
products[3] = new Products(0004,"WHAT!!! the....",1,129.99);
//____________________________________________________________4 products that are already made
for (int i=0;i<numberOfProducts+4;i++)
{
System.out.println(products[i].toString());
input.nextLine();
}
}
}
this is the other class
import java.text.NumberFormat;
public class Products
{
private int itemNumber;
private String nameOfProduct;
private int unitsInStock;
private double unitPrice;
public Products()
{
itemNumber = 0;
nameOfProduct = null;
unitsInStock = 0;
unitPrice = 0.0;
}
public Products(int num,String name,int inStock,double price)
{
itemNumber = num;
nameOfProduct = name;
unitsInStock = inStock;
unitPrice = price;
}
public int getItemNumber()
{
return itemNumber;
}
public void setItemNumber(int newValue)
{
itemNumber=newValue;
}
//----------------------------------------------
public String getNameOfProduct()
{
return nameOfProduct;
}
public void setNameOfProduct(String newValue)
{
nameOfProduct=newValue;
}
//----------------------------------------------
public int getUnitsInStock()
{
return unitsInStock;
}
public void setUnitsInStock(int newValue)
{
unitsInStock = newValue;
}
//-----------------------------------------------
public double getPrice()
{
return unitPrice;
}
public void setPrice(double newValue)
{
unitPrice = newValue;
}
//_______________________________________________
public double calculateTotalItemValue() //method that uses quantity on hand and price part3 1.A
{
return getUnitsInStock()* getPrice();
}//end of method
#Override
public String toString()
{
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
return"\nItem Number: "+getItemNumber() +
"\nItem Name: "+getNameOfProduct()+
"\nItem Quantity: " +getUnitsInStock()+
"\nItemPrice:" +currencyFormat.format(getPrice())
+"\nValue of Inventory: " +currencyFormat.format(this.calculateTotalItemValue());//part3 1.B
}
}
The Scanner sees the space in the book name as a delimiter since you are using the next() method. So when you go to read the nextInt() for the stock amount, the Scanner index is after the space in the book name String, and pulls in the remaining String data, which doesn't convert to an int. Instead, try something like this:
System.out.println("What is the book name: ");
input.nextLine();
products[i+4].setNameOfProduct(input.nextLine());
If you do not add the input.nextLine();, then it will appear as though the book name prompt gets skipped.
Scanner input = new Scanner(System.in);
Actually you are using scanner to get input and by default scanner delimiter is space. So you have to change the default delimiter of your code.
I think this is your problem:
products[i+4].setNameOfProduct(input.next());
What input.next() does is it reads the input from the user, until it reaches white space (the space between hoopa doopa). The function then passes hoopa to the setNameOfProduct method, and then passes doopa to the nextInt function, which gives a runtime error.
To fix your problem I would code
products[i+4].setNameOfProduct(input.nextLine());
Edit:
nextLine() function passes all characters up to the carriage return
Problem :
products[i+4].setNameOfProduct(input.next());
Solution 1 :
Just create another Scanner object for reading input with spaces
Scanner sc1=new Scanner(System.in);
products[i+4].setNameOfProduct(sc1.nextLine());
Solution 2 :
Or to use same scanner object
input.nextLine(); //Include this line before getting input string
products[i+4].setNameOfProduct(input.nextLine());
Related
My problem is I want to input two numbers separated by a space and getting those two numbers and compute those two numbers. for example, i typed "10 20", they are separated by a space and then I want to get and compute them as separate: 10 * 20 = 200..
what I have is a getter and setter for this part
these are my code:
MY GETTER AND SETTER CLASS
import java.util.*;
public class LabExer2
{
private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;
public void setItemName(String newItemName)
{
itemName = newItemName;
}
public String getItemName()
{
return itemName;
}
public void setTotalCost(int quantity, double price)
{
itemQuantity = quantity;
itemPrice = price;
amountDue = itemQuantity * itemPrice;
}
public double getTotalCost()
{
return amountDue;
}
public void readInput()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the name of the item you are purchasing.");
itemName = s.nextLine();
System.out.println("Enter the quantity and price separated by a space.");
//itemQuantity = s.nextInt();
//itemPrice = s.nextDouble();
}
public void writeInput()
{
System.out.println("You are purchasing " + itemQuantity + " " + itemName + "(s) at " + itemPrice + " each.");
System.out.println("Amount due is " + getTotalCost());
}
}
MY MAIN CLASS
public class MainLabExer2
{
public static void main(String[]args)
{
LabExer2 le = new LabExer2();
//still not finished because I can't figure out the process in the other class.
}
}
The output I wanted to have:
Enter the name of the item you are purchasing.
pencil
Enter the quantity and price separated by a space.
3 15.50
You are purchasing 3 pencil(s) at 15.50 each.
Amount due is 46.50
I want to get the 3 and 15.50 and compute. I don't know what to use or something, I'm just a beginner in java.
You can create two prompts, one to take in the itemQuantity int and another to take the itemPrice Double.
But if you want the user to input both values at once, you need to write a code in the readInput() method that will extract the itemQuantity value and the itemPrice value.
Try this:
public void readInput() {
Scanner s = new Scanner(System.in);
System.out.println("Enter the name of the item you are purchasing.");
itemName = s.nextLine();
System.out.println("Enter the quantity and price separated by a space.");
String userInput = s.nextLine(); //String to collect user input
//code to loop through String in search for Whitespace
for (int x=0; x<String.length(); x++) {
if (userInput.charAt(x) == ' ') {
//whitespace found, set itemQuantity to a substring
//from the first char in UserInput to the char before whitespace
itemQuantity = Integer.parseInt(userInput.substring(0, x-1));
//convert String to int
itemPrice = Integer.parseInt(userInput.substring(x+1, userInput.length()));
x = String.length();
} else {
itemQuantity = Integer.parseInt(userInput);
}
}
}
Pardon me if my code is not properly formatted, I'm new here.
Only need to convert the string into Integer, and string into Double. Then, to put 2 numbers and separate them, use the one-dimensional array and the split function.
try this:
public void readInput(){
//Enter the product of item name
System.out.print("Enter the name of the item you are purchasing: ");
ITEM_NAME = input.nextLine();
//Enter the quantity and the price for the item
System.out.print("Enter the quantity and price separated by a space: ");
String userInput = input.nextLine();
String[] inputArray = userInput.split(" ");
ITEM_QUANTITY = Integer.parseInt(inputArray[0]);
ITEM_PRICE = Double.parseDouble(inputArray[1]);
}
Everything works so far in my program but I'm having trouble with this section of my code:
else if(input.equals("2")) {
System.out.println("Enter the stock symbol:");
symbol2 = in.next();
System.out.println("Enter the number of shares you wish to sell:");
sellshares = in.nextInt();
String tempsymbol = "";
for(int i=0; i<array1.size(); i++) {
tempsymbol = (array1.get(i)).getSymbol();
if(symbol2.equals(tempsymbol)) {
System.out.println("The dollar cost averaged price per share (LIFO): " + (array1.get(i)).averageCost(sellshares));
System.out.println("The dollar cost averaged price per share (FIFO): " + (array2.get(i)).averageCost(sellshares));
}
}
}
It'll go through the loop but tempsymbol will always = "". Why doesn't array1 return anything?
Here's all my code. Apologies ahead of time if any parts are redundant or messy.
import java.util.*;
public class Whoop {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = "";
String symbol = "";
String name = "";
int shares = 0;
double price = 0;
String symbol2 = "";
int sellshares = 0;
int rolling = 0;
stack theStack = null;
queue theQ = null;
String loopcheck = "1";
ArrayList<stack> array1 = new ArrayList<stack>();
ArrayList<queue> array2 = new ArrayList<queue>();
while(loopcheck.equals("1")) {
System.out.println("Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold");
input = in.next();
if(input.equals("1")) {
System.out.println("Enter the stock symbol:");
symbol = in.nextLine();
in.nextLine();
System.out.println("Enter the stock name:");
name = in.nextLine();
System.out.println("Enter the number of shares bought:");
shares = in.nextInt();
System.out.println("Enter the price per share when purchased");
price = in.nextDouble();
theStack = new stack(symbol,name,shares,price);
theQ = new queue(symbol,name,shares,price);
System.out.println("Press 1 to continue entering new shares or press 2 to finish input for " + theStack.getName());
rolling = in.nextInt();
while(rolling == 1) {
System.out.println("Enter the number of shares bought:");
shares = in.nextInt();
System.out.println("Enter the price per share when purchased");
price = in.nextDouble();
theStack.bigPush(shares, price);
theQ.bigAdd(shares, price);
System.out.println("Press 1 to continue entering new shares or press 2 to finish input for " + theStack.getName());
rolling = in.nextInt();
}
array1.add(theStack); //I added the objects after all the values were finalized
array2.add(theQ);
}
else if(input.equals("2")) {
System.out.println("Enter the stock symbol:");
symbol2 = in.next();
System.out.println("Enter the number of shares you wish to sell:");
sellshares = in.nextInt();
String tempsymbol = "";
for(int i=0; i<array1.size(); i++) {
tempsymbol = (array1.get(i)).getSymbol();
if(symbol2.equals(tempsymbol)) {
System.out.println("The dollar cost averaged price per share (LIFO): " + (array1.get(i)).averageCost(sellshares));
System.out.println("The dollar cost averaged price per share (FIFO): " + (array2.get(i)).averageCost(sellshares));
}
}
}
else {
System.out.println("Input invalid ):");
System.exit(0);
}
System.out.println("Press 1 to continue working with your stocks or press anything else to finish up");
loopcheck = in.next();
}
System.out.println("END");
}
}
This is my queue class which works perfectly fine.
import java.util.LinkedList;
public class queue<E> {
private LinkedList<Double> linklist;
private String symbol;
private String name;
private int shares;
private Double price;
public queue(String symbol2, String name2, int shares2, Double price2) {
linklist = new LinkedList<Double>();
shares = shares2;
price = price2;
symbol = symbol2;
name = name2;
bigAdd(shares, price);
}
public String getName() {
return name;
}
public String getSymbol() {
return symbol;
}
public void add(Double e) {
linklist.add(e);
}
public Double take() {
return linklist.poll();
}
public void bigAdd (int shares2, Double price2) {
while(shares2>0) {
linklist.add(price2);
shares2--;
}
}
public double averageCost(int shares2) {
double average = 0;
int sizer = 0;
while(sizer < shares2) {
average = average + linklist.poll();
sizer++;
}
average = average/shares2;
return average;
}
And this is my stack class which also works fine.
import java.util.*;
public class stack {
private ArrayList<Double> stackArray = new ArrayList<Double>();
private int top;
private String symbol;
private String name;
private int shares;
private Double price;
public stack(String symbol2, String name2, int shares2, Double price2) {
symbol = symbol2;
name = name2;
shares=shares2;
price=price2;
top = -1;
bigPush(shares, price);
}
public double averageCost(int shares2) {
double average = 0;
int sizer = shares2;
while(sizer > 0) {
average = average + stackArray.get(top--);
sizer--;
}
average = average/shares2;
return average;
}
public void push(Double value) {
stackArray.add(++top, value);
}
public Double pop() {
return stackArray.get(top--);
}
public String getName() {
return name;
}
public String getSymbol() {
return symbol;
}
public void bigPush(int shares2, Double price2) {
while(shares2>0) {
stackArray.add(++top, price2);
shares2--;
}
}
public static void main(String[] args) {
stack theStack = new stack("Dave", "Franco", 2,10.0);
theStack.bigPush(2,20.0);
System.out.println(theStack.getSymbol());
}
}
Also heres an example of my output:
Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold
1
Enter the stock symbol:
DAVE
Enter the stock name:
FRANCO
Enter the number of shares bought:
5
Enter the price per share when purchased
5
Press 1 to continue entering new shares or press 2 to finish input for FRANCO
2
Press 1 to continue working with your stocks or press anything else to finish up
1
Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold
2
Enter the stock symbol:
DAVE
Enter the number of shares you wish to sell:
1
//AND THEN NOTHING HERE WHEN IT SHOULD RETURN AVERAGECOST()
Press 1 to continue working with your stocks or press anything else to finish up
Following your long code,
tt looks like it all boils down to a wrong usage of the Scanner class :
while(loopcheck.equals("1")) {
System.out.println("Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold");
input = in.next();
if(input.equals("1")) {
System.out.println("Enter the stock symbol:");
symbol = in.nextLine(); // problem here
in.nextLine();
this assigns an empty String to symbol, because it consumes the end of line of the previous in.next().
If you change it to :
while(loopcheck.equals("1")) {
System.out.println("Press 1 to enter a new stock or press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold");
input = in.next();
in.nextLine();
if(input.equals("1")) {
System.out.println("Enter the stock symbol:");
symbol = in.nextLine();
it will work.
Edit :
It looks like you are aware of the need to sometimes call in.nextLine() without using its returned value, but you put in.nextLine() in the wrong place.
Here is my code. I did my work a bit off. I was supposed to not just do one applicant, I was supposed to do many and I was supposed to populate the applicants into 2 different arrays, one for graduates, and one for undergraduates. I need help figuring out how to make an empty array and fill up applicants in those. A start would help
import java.util.Scanner;
public class CollegeApplicant
{
String applicantName;
String collegeName;
public String getApplicantName()
{
return applicantName;
}
public void setApplicantName(String applicantName)
{
this.applicantName = applicantName;
}
public String getCollegeName()
{
return collegeName;
}
public void setCollegeName(String collegeName)
{
this.collegeName = collegeName;
}
public void checkCollege()
{
int choice;
Graduate g = new Graduate();
Undergraduate ug = new Undergraduate();
Scanner input = new Scanner(System.in);
System.out.println("1. Undergraduate");
System.out.println("2. Graduate");
System.out.println("enter your choice ");
choice = input.nextInt();
if (choice == 1)
{
System.out.println("Enter SAT marks :");
ug.SAT = input.nextDouble();
System.out.println("Enter GPA marks :");
ug.GPA = input.nextDouble();
}
else
{
System.out.println("Enter the origin of college:");
g.collegeOrigin = input.next();
System.out.println("Status :" + g.checkCollege());
}
}
public class Undergraduate
{
double SAT;
double GPA;
}
public class Graduate
{
String collegeOrigin;
String checkCollege()
{
String information = null;
if (collegeName.equals(collegeName)) information = "Applicant is applying from inside";
else information = "Applicant is applying from outside";
return information;
}
}
}
The Client
import java.util.Scanner;
public class CollegeApplicantClient extends CollegeApplicant
{
public static void main(String[] args)
{
TestCollege tc = new TestCollege();
Scanner input = new Scanner(System.in);
System.out.println("Enter the name of applicant ");
tc.setApplicantName(input.next());
System.out.println("enter the college name of applicant ");
tc.setCollegeName(input.next());
tc.checkCollege();
}
}
Here is an example of the possible output
Name: Joe
College Applied to: Harvard
SAT: 5
GPA: 2
Name: Tom
College Applied to: Yale
College of Origin: NYU – from outside
I need to be able to put the graduates in an array and the undergrads in an array. Then display it when it's all done
This is very basic java that i'm struggling with n00b style. it just prints out this
Please enter '.' when you want to calculate
1 2 3
.
Numbers are 1 2 3
The Sum is0The Product is1
when it is supposed to calculate the sum and product of those consecutive numbers. something is wrong id appreciate any help!
main method
import java.util.*;
public class NumberScanned {
public static void main(String[] args) {
System.out.println("Please enter '.' when you want to calculate");
Scanner keyboard = new Scanner(System.in);
String scannedString = keyboard.nextLine();
Scanning scanz= new Scanning(scannedString);
while(!keyboard.nextLine().equals("."))
{
scanz.set(scannedString);
}
keyboard.close();
System.out.println("Numbers are"+scannedString);
scanz.printState();
}
}
Class Scanning
public class Scanning {
int num;
int sum;
int product;
String userInput;
public Scanning(String userInput)
{
num=0;
sum=0;
product=1;
this.userInput=userInput;
}
public void set(String userInput)
{
for(int index=0; index<userInput.length(); index++)
{
if(Character.isDigit(userInput.charAt(index))==true)
{
num=userInput.charAt(index);
sum+=num;
product*=num;
}
else
{
index++;
}
}
}
public void printState()
{
System.out.println("The Sum is"+sum+"The Product is"+product);
}
}
A few things to look at:
We know keyboard.nextLine() gets the input from the console, but where are you checking it's validity (more importantly, when do you check it?). Are you looking at all input or just the last line?
isDigit will return true if the passed in character is a number. Do you want to operate on numbers or characters in your for loop?
(a side note, What happens if I enter "1 10" in the console?)
A for loop will automatically increment its index at the end of a loop, so an additional ++ is unnecessary
You might find this helful in case you just need the sum and product values of a user entered
values.
public class ProductSumCalculator{
private static List<Integer> numbers = new ArrayList<Integer>();
public static void main(String[] args){
getInputs();
calculateSumAndProduct();
}
private static void getInputs() {
Scanner scanner = new Scanner(System.in);
System.out.println("Please Enter numbers or ctrl+z to end inputs");
while(scanner.hasNext()){
numbers.add(scanner.nextInt());
}
}
private static void calculateSumAndProduct() {
Iterator<Integer> iterator = numbers.iterator();
int sum=0;
int product=1;
int nextVal;
while(iterator.hasNext()){
nextVal = iterator.next();
sum+=nextVal;
product*=nextVal;
}
System.out.println("Value entered are: "+numbers+".\nThe sum is "+
sum+".The product is "+product);
}
}
You can also try this. You can calculate the sum and product of all the int from your string line input like this:
import java.util.Scanner;
public class Scanning {
/*
* This method returns the integer. If while
* conversion an Exception is thrown it returns
* null. Otherwise the integer.
*/
public static Integer tryParse(String text) {
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
return null;
}
}
/*
* Next String line is scanned. It is split by space.
* Stores String tokens in an String array from one String variable.
* Then passed to tryParse() class method. null or auto Boxed Integer
* is returned accordingly. It is auto unboxed from Integer
* object to int variable. Then sum and product is calculated and
* the final result is printed on the console Or Integrated
* Development Environment.
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
String strInts = keyboard.nextLine();
String[] splits = strInts.split("\\s+");
int i = 0;
Integer anInteger = null;
int total = 0;
int product = 1;
while((i < splits.length)) {
anInteger = tryParse(splits[i]);
if(anInteger != null) {
total = total + anInteger;
product = product * anInteger;
}
++i;
}
System.out.println("The sum is: " + total);
System.out.println("The product is: " + product);
}
}
I have some trouble regarding my lab assignment:
When my program tries to prompt the user for input, the program outputs two questions on the same line and only takes the input for the second question.
The output of my program:
Please enter the name of the second employee:Please enter the number of the second employee:1
(They appear on the same line instead of separate lines)
Also the output for an array outputs like this:
0.00.00.00.00.00.00.00.00.00.0
instead of like this:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
I'm not quite sure how to fix these two any help would be appreciated!
Here is my code:
Employee.java
//import java.util.*;
public class Employee
{
private String empName;
private int empNumber;
private String empAddress;
private double empSalary;
private double[] empBonus=new double[10];
public Employee(){}
public Employee(String empName_, int empNumber_, String empAddress_, double empSalary_, double[] empBonus_)
{
this.empName=empName_;
this.empNumber=empNumber_;
this.empAddress=empAddress_;
this.empSalary=empSalary_;
this.empBonus=empBonus_;
}
public String getName()
{
return this.empName;
}
public int getEmployeeNumber()
{
return this.empNumber;
}
public String getAddress()
{
return this.empAddress;
}
public double getSalary()
{
return this.empSalary;
}
public String changeAddress(String chAddress)
{
return empAddress=chAddress;
}
public double changeSalary(double chSalary)
{
return empSalary=chSalary;
}
public String addBonus(double[] empBonus)
{
String arrayBonus=new String("");
for(int i=0; i<empBonus.length;i++)
{
arrayBonus+=empBonus[i];
}
return arrayBonus;
}
public String toString()
{
return ("\nEmployee's name: "+empName+"\nEmployee's Number: "+empNumber+"\nEmployee's address: "+empAddress+
"\nEmployee's original salary: "+empSalary+ "\nEmployee's bonuses: "+addBonus(empBonus)+"\n");
}
}
EmployeeTester.java
import java.util.*;
public class EmployeeTester
{
public static void main(String[] args)
{
Scanner in1=new Scanner(System.in);
Scanner in2=new Scanner(System.in);
Scanner in3=new Scanner(System.in);
Employee emp1;
Employee emp2;
emp1=read_input("first", in1, in2, in3);
emp2=read_input("second", in1, in2, in3);
System.out.println(emp1.toString());
System.out.println(emp2.toString());
}
public static Employee read_input(String msg, Scanner scan1, Scanner scan2, Scanner scan3)
{
String name, address;
int num;
double salary;
double[] bonus=new double[10];
System.out.print("\nPlease enter the name of the "+msg+" employee:");
name=scan1.nextLine();
System.out.print("Please enter the number of the "+msg+" employee:");
num=scan2.nextInt();
System.out.print("Please enter the address of the "+msg+" employee:");
address=scan1.nextLine();
System.out.print("Please enter the salary of the "+msg+" employee:");
salary=scan3.nextDouble();
System.out.print("Please add a bonus for the "+msg+" employee:");
bonus[0]=scan3.nextDouble();
System.out.print("Add more bonuses to the "+msg+"employee? (y/n) \nNote: Enter 0.0 if you would like to terminate adding more bonuses: ");
if(scan1.next().startsWith("y"))
{
for(int i=1; i<bonus.length;i++)
{
System.out.print("Continue entering a bonus to "+msg+" employee:");
bonus[i]=scan3.nextDouble();
if(bonus[i]==0.0 || i==bonus.length)
{
break;
}
}
}
return new Employee(name, num, address, salary, bonus);
}
}
For your first problem just shift your Scanners inside your read_input method so they start fresh each time.
public static void main(String[] args) {
Employee emp1;
Employee emp2;
emp1 = read_input("first");
emp2 = read_input("second");
System.out.println(emp1.toString());
System.out.println(emp2.toString());
}
public static Employee read_input(String msg) {
Scanner scan1 = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);
Scanner scan3 = new Scanner(System.in);
...
For your second problem, in your addBonus method where you build the output string you are not adding any spaces or commas. Also it is a lot more efficient if you use a StringBuilder for this type of looped concatenation rather than repeatedly creating new string objects.
public String addBonus(double[] empBonus)
{
StringBuilder arrayBonus = new StringBuilder();
for(int i=0; i<empBonus.length;i++)
{
arrayBonus.append(empBonus[i] + ", ");
}
return arrayBonus.toString();
}
You do not need 3 different scanners, one will do.
For the printing part, you need println() or use '\n' for new lines.
E.g.:
System.out.println("Please enter the name of the "+msg+" employee:");
System.out.print("Please enter the name of the "+msg+" employee:\n");
In the first case, you are calling a function (println() instead of print()) that automatically appends a new-line to the outputes text. In the second case you are making the new-line part of the string (which you already used in the beginning of the first line printed)
The array output uses 0.0 because the values are floting point values (double in your case), which by default prints decimal parts. To only print the integer part, you will need to cast the value to an integer or use formatting for the double.
Casting:
double a = 0.0;
System.out.print("'a' as integer: " + ((int)a));
Formatting:
System.out.format("Here is a number: %.0f", a);
The number between the . and f specifies how many decimal places to output.