Adding Raise to Salary - java

I got the program running the way I want it.I am trying to make the program adds 2% raise to every employee's salary. My issue is that the program's output does not display the 2% raise.
Here is the output I get when I run the program..
Enter social security number:12345678
Enter salary2000
Please input next security numbers or -1 to quit:12345677
Enter salary3000
Please input next security numbers or -1 to quit:-1
Social Security Number Salaries Salary After Raise
12345678 2000.000000 0.000000
12345677 3000.000000 0.000000
The code..
import java.util.Scanner;
public class Salaries {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
//Declaring variables
int[] ssNumbers = new int [10];
double[] salaries = new double [10];
double[] nSalaries = new double [10];
int c;
c = inputData (ssNumbers, salaries); //calling input data method
raise (salaries, c); // calling raise method
output (ssNumbers, salaries, nSalaries); //calling output method
}
public static int inputData (int[]ssn, double[]sals){ // input method
int c = 0;
Scanner input = new Scanner (System.in);
int ssNum;
System.out.print("Enter social security number:");
ssNum = input.nextInt();
while (ssNum != -1) //using while loop.
{
ssn[c] = ssNum;
System.out.print("Enter salary");
sals[c] = input.nextDouble(); //prompting user for input
c++;
System.out.print("Please input next security numbers or -1 to quit:");
ssNum = input.nextInt(); //prompting user for input
}
return c;
}
public static void raise (double[] salaries, int c) // raise method
{
double[] salaryraise = new double [10];
for (int i = 0; i < c; i++ )
salaryraise[i] = salaries[i]*.02;
}
public static void output (int[] ssNumbers, double[] salaries, double[] salaryraise ) //output method
{
System.out.printf("%30s %30s %30s\n", "Social Security Number", "Salaries", "Salary After Raise");
for (int i = 0; i < salaries.length; i++)
//output
System.out.printf("%30d %30f %30f\n", ssNumbers[i], salaries[i], salaryraise[i]);
return;
}
}

In your raise method, you create a local array called salaryraise, and you assign the new numbers to it, but you do nothing with it, and it goes out of scope.
The array you do send to output, nSalaries, is declared, but it is never modified before it's sent to output.
Pass nSalaries as a second parameter to raise, and have raise assign values to that array instead of declaring a local array and assigning values to it.

See the comments in the code :
public static void raise (double[] salaries, int c) // raise method {
double[] salaryraise = new double [10];
// Here, you write to a new array and the original array isn't modified at all.
for (int i = 0; i < c; i++ ) {
salaryraise[i] = salaries[i]*.02;
}
// Change it for
for (int i = 0; i < c; i++ ) {
salaries[i] *= 1.02;
}
}

Related

passing an array as an argument; Setting up Array in Java with user input with Scanner Class

I am trying to take user input, place it into my array, display the array and then print all the values larger than the "n" values the user provides. I think I am close, but I can't get the user input to go to the array. I keep getting an error in eclipse when I call the method (main at very bottom) the "arrayValues" cannot be resolved to a variable:
import java.util.Arrays;
import java.util.Scanner;
public class LargerThanN {
//initialize n
static int n;
static int arraySize;
//setup the array
static int [] integerArray = new int [] {};
public static void printGreaterThanN(int[] integerArray, int n) {
for (int i = 0; i < integerArray.length; i++) {
if (integerArray[i]>n) {
System.out.println(integerArray[i]);
}
}
}
public static int[] fillArrayWithUserInt() {
Scanner sc = new Scanner(System.in);
System.out.println("How big will the array be?");
int arraySize = sc.nextInt();
sc.nextLine(); // clears rest of input, including carriage return
int[] integerArray = new int[arraySize];
System.out.println("Enter the " + arraySize + " numbers now.");
for (int i = 0; i < integerArray.length; i++) {
integerArray[i] = sc.nextInt();
}
return integerArray;
}
/**
* This method prints the array to the standard output
* #param array
*/
private static void displayArray( int[] integerArray) {
for (int i = `0; i < integerArray.length; i++) {
System.out.print(integerArray[i] + " ");
}
}
public static void main(String[] args) {
int [] array ;
array = fillArrayWithUserInt();
Scanner sc = new Scanner(System.in);
fillArrayWithUserInt();
displayArray(array);
System.out.println("To which number would you like to compare the rest? Your n value is: ");
n = sc.nextInt();
printGreaterThanN(array, n);
but now my output looks like:
How big will the array be?
4
Enter the 4 numbers now.
1 2 3 4
How big will the array be?
3
Enter the 3 numbers now.
1 2 3
1 2 3 4
To which number would you like to compare the rest? Your n value is:
2
3
4
Heads up, the following code does nothing in java...
public void set(int n, int value) {
n = value;
}
You seem to written code like this in many functions where a value should be returned.
For example, the function definition :
static void fillArrayWithUserInt(int[] integerArray, int arraySize, int arrayValues, int n)
Should really be written as :
static int[] fillArrayWithUserInt()
It could be implemented as follows
public static int[] fillArrayWithUserInt() {
Scanner sc = new Scanner(System.in);
System.out.println("How big will the array be?");
int arraySize = sc.nextInt();
sc.nextLine(); // clears rest of input, including carriage return
int[] integerArray = new int[arraySize];
System.out.println("Enter the " + arraySize + " numbers now.");
System.out.println("What are the numbers in your array?");
for (int i = 0; i < integerArray.length; i++) {
integerArray[i] = sc.nextInt();
}
return integerArray;
}
The above function will ask the user for the array size. Create the array with the given size. Then prompt the user to fill the array with the correct number of values. The array created in this process is then returned.
All you must handle differently now is finding the value to compare. This must be done outside the fillArrayWithUserInt function.
Like so :
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] array = fillArrayWithUserInt();
displayArray(array);
System.out.println("To which number would you like to compare the rest? Your n value is: ");
int n = sc.nextInt();
printGreaterThanN(array, n);
}
Lastly, you should not need to declare any static variables at the top of your class.
These lines can all be deleted :
//initialize n
static int n;
static int arraySize;
//setup the array
static int [] integerArray = new int [] {};
Here is my solution check it out.
import java.util.Scanner;
public class LargerThanN {
static int[] integerArray = null;
static int n ;
public static void printGreaterThanN() {
for (int i = 0; i < integerArray.length; i++) {
if (integerArray[i] > n) {
System.out.println(integerArray[i]);
}
}
}
public static void fillArrayWithUserInt() {
Scanner sc = new Scanner(System.in);
System.out.println("How big will the array be?");
int arraySize = sc.nextInt();
sc.nextLine(); // clears rest of input, including carriage return
integerArray = new int[arraySize];
System.out.println("Enter the " + arraySize + " numbers now.");
System.out.println("What are the numbers in your array?");
for (int i = 0; i < integerArray.length; i++) {
integerArray[i] = sc.nextInt();
}
System.out.println("To which number would you like to compare the rest? Your n value is: ");
n = sc.nextInt();
}
/**
* This method prints the array to the standard output
*
* #param array
*/
private static void displayArray() {
for (int i = 0; i < integerArray.length; i++) {
System.out.print(integerArray[i] + " ");
}
}
public static void main(String[] args) {
fillArrayWithUserInt();
displayArray();
printGreaterThanN();
}
}

Passing an array to another method and copying it

I am trying to pass an array from one method to another method and then copy the contents of that array into a new array. I am having trouble with the syntax to accomplish that task.
Does anyone have some reference material that I could read about this topic or maybe a helpful tip that I could apply?
I apologize if this is a noob question, but I have only been messing with Java for 3-4 weeks part time.
I know that Java uses pass by value, but what where I'm getting lost is...should I invoke the sourceArray before copying it to the targetArray?
My goal here is not to be just handed an answer, I need to understand WHY.
Thanks...in advance.
package cit130mhmw08_laginess;
import java.util.Scanner;
public class CIT130MHMW08_Laginess
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the total number of dealers: ");
int numDealers = input.nextInt();
numDealers = numberOfDealers(numDealers);
System.out.printf("%nPlease enter the required data for each of your dealers:");
dataCalculation(numDealers);
}//main
//METHOD 1
public static int numberOfDealers(int dealers)
{
int results;
Scanner input = new Scanner(System.in);
while(dealers < 0 || dealers > 30)
{
System.out.printf("%nEnter a valid number of dealers: ");
dealers = input.nextInt();
}
results = dealers;
return results;
}//number of dealers methods
//METHOD 2
public static void dataCalculation(int data)
{
String[] dealerNames = new String[data];
Scanner input = new Scanner(System.in);
System.out.printf("%nEnter the names of the dealers:%n ");
for(int i = 0; i < data; i++)
{
String names =input.nextLine();
dealerNames[i]= names;
}
int[] dealerSales = new int[data];
System.out.printf("%nEnter their sales totals: %n");
for(int i = 0; i < data; i++)
{
int sales = input.nextInt();
dealerSales[i] = sales;
}
for(int i = 0; i < data; i++)
{
System.out.println(" " + dealerNames[i]);
System.out.println(" " + dealerSales[i]);
}
//gather the required input data.
//Perform the appropriate data validation here.
}//data calculations
//METHOD 3
public static int commission(int data)
{
//Create array
int[] commissionRate = new int[dealerSales];
//Copy dealerSales array into commissionRate
System.arraycopy(dealerSales, 0, commissionRate, 0, dealerSales.length);
//calculate the commission array.
//$1 - $5,000...8%
//$5,001 to $15,000...15%
//$15,001...20%
//
}//commission method
}//class
If you want to copy an array, you can use the Arrays.copyOf(origin, length) method. It takes 2 arguments, first one is the array from which the data is supposed to be copied and second is the length of the new array, and import java.util.Arrays.
-See the link for more info https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOf(int[],%20int)

Having some issues with my java program

I am trying to do my java assignment. I am just struggling with some of the issues. I could not make the program calculates and outputs the information needed. When I try to run the program, and input the social security number, I get an error and a message saying
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Salaries.inputData(Salaries.java:34) at
Salaries.main(Salaries.java:18)
What am I doing wrong?
Thanks.
Here is a description of the program I am suppose to do.
Write a program that reads Social Security numbers and salaries from the keyboard (Test this by only reading 3 or 4 to start). After the data is read in, the program should add a 2% raise to everyone’s salary. After you give the raise, print out 3 nicely formatted columns of information (Social, salary before raise, salary after raise). You will need a 3rd array holding the new salaries. A separate method should be used for the input, raise, and output.
This is the code I got so far.
import java.util.Scanner;
public class Salaries {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
int[] ssNumbers = new int [10];
double[] salaries = new double [10];
int c;
int rais;
c = inputData (ssNumbers, salaries);
raise (salaries, c, ssNumbers);
output (ssNumbers, salaries, nSalary);
}
public static int inputData (int[]ssn, double[]sals){
int c = 0;
Scanner input = new Scanner (System.in);
int ssNum;
System.out.print("Enter social security number");
ssNum = input.nextInt();
while (ssNum != -1) //using while loop.
{
ssn[c] = ssNum;
System.out.print("Enter salary");
sals[c] = input.nextDouble();
c++;
System.out.print("Please input next security numbers or -1 to quit");
ssn[c] = input.nextInt();
}
return c;
}
public static void raise ( double[] salaries, int c, int[] ssNumbers)
{
double salar;
double rais = 0.02;
for (int count = 0; count < c; count++ )
rais+= rais;
System.out.printf("the salary after raise is %f\n", rais);
return;
}
public static void output (int[] ssNumbers, double[] salaries, double[] nSalary )
{
System.out.printf("%10d%-20d%-20f%", ssNumbers, salaries, nSalary);
return;
These statements:
int[] ssNumbers = new int [0];
double[] salaries = new double [0];
double[] nSalary = new double[0];
create an array of size 0. So when you try to add an element to that array, you are adding to an index that is greater than the array size. I would use an ArrayList so you can grow your array dynamically behind the scenes if you do not know the length.
If you have to use arrays, make sure they are big enough for the data you want to put in, eg
int[] ssNumbers = new int [maxSize];
The value of c in the function inputData is exceeding the length of ssn[] (or maybe sals[], can't tell in your code). Your boundary checking is off, try making sure that c is never less than 0, or larger than the size of ssn[].
You are initializing your array to 0 in the below statement,
int[] ssNumbers = new int [0];
double[] salaries = new double [0];
And you are trying to read past 0 in the below statement (inside inputData function)
ssn[c] = ssNum;
That is the error
You are reading till user inputs -1 which means that you didn't know the number of data/user-inputs prehand. Use LinkedList instead of array.

need scanner input for array

does anyone know how to set a user input for an array, I cant find the command anywhere. my array 'grades' have 20 locations. im not so sure about 'grades.length' function but I think it prompts 20 times. BUT I added a while statement to override BUT ITS TOTALLY IGNORING THE FOR STATEMENT. if I could set user input for array I could get rid of the while statement...
program has to accept grade for number of students the user inputs btw..
import java.util.Scanner;
public class gradesaverage {
public static void main(String[] args) {
int [] grades = new int [20];
int i;
int numStudents;
System.out.print("Enter number of students: ");
Scanner scanint = new Scanner (System.in);
numStudents = scanint.nextInt();
for ( i = 1; i <= grades.length; ++i)
{
System.out.println("Enter grade: ");
grades[i] = scanint.nextInt();
}
while(i <= numStudents );
}
}
Not sure what you mean, but assuming all input is correct,
int [] grades = new int [numStudents ];
Should work if you move this line after declaration and assignment of numStudents. There is no problem in java with variable length arrays.
Also note - your iterator i starts from 1, while in java arrays start from 0.
public static void main(String[] args) {
int i;
int numStudents;
System.out.print("Enter number of students: ");
Scanner scanint = new Scanner (System.in);
numStudents = scanint.nextInt();
int [] grades = new int [numStudents]; //the size we wanted
for ( i = 0; i < grades.length; ++i) //starting from 0, not 1.
{
System.out.println("Enter grade: ");
grades[i] = scanint.nextInt();
}
//print the array - for checking out everyting is ok
System.out.println(Arrays.toString(grades));
}

Finding the maximum out of three doubles from standard input

We have a java assignment where in we're supposed to develop a method that scans one line that is supposed to contain three double values and returns the largest. Throwing all possible exceptions is allowed.
Here is what I've done so far:
import java.util.Scanner;
public class s3dv {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double entered;
System.out.println("Enter 3 values to find the maximum:");
entered = input.nextDouble();
System.out.println("Maximum is - " + getMaxValue(entered));
}
//Find maximum (largest) value in array using loop
public static double getMaxValue(double[] numbers){
double maxValue = numbers[0];
for(int i = 1; i < numbers.length; i++){
if(numbers[i] > maxValue){
maxValue = numbers[i];
}
}
return maxValue;
} // End getMaxValue method
}
I'm having an error at line 15.
change your code to
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
double[] entered = new double[3];
System.out.println("Enter 3 values to find the maximum:");
for(int i=0;i<3;i++){
entered[i] = input.nextDouble();
}
System.out.println("Maximum is - " + getMaxValue(entered));
}
//Find maximum (largest) value in array using loop
public static double getMaxValue(double[] numbers){
double maxValue = numbers[0];
for(int i = 1; i < numbers.length; i++){
if(numbers[i] > maxValue){
maxValue = numbers[i]; } } return maxValue;
}
You cannot give a double parameter to a method while it expects a double array. And also you request user to enter double value only once, you should repeat that procedure. Change your main method to this:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double[] entered = new double[3];
int counter = 0;
while (counter != 3)
{
System.out.println("Enter a double value:");
entered[counter++] = input.nextDouble();
}
System.out.println("Maximum is - " + getMaxValue(entered));
}
Your getMaxValue() method seems OK, however when entering doubles from console use comma(,) instead of dot(.), you might get InputMismatchException otherwise.
this main code will read the 3 double value in a single line, split them and pass it to the getMaxValue
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userLine, lineSplitted[];
System.out.println("Enter 3 values to find the maximum:");
userLine = input.nextLine();
lineSplitted=userLine.split(" ");
double entered[]=new double[lineSplitted.length];
for (int i=0; i<lineSplitted.length; i++) entered[i]=Double.valueOf(lineSplitted[i]);
System.out.println("Maximum is - " + getMaxValue(entered));
}

Categories

Resources