This loop to modify a arraylist of numbers based on arraylist of strings isnt working - arrays

So, in my program you input employee information and the salary you input depends on what you input for designation. If temporary, yo input how much they make per hour, if permanent you input what they make per year. At the end, you have the option to sort by salary, so if they choose that then I must multiply the temporary's per-hour by a set number (1920) in order to convert it to what they make per year. I tried this but I got the messages
" The type of the expression must be an array type but it resolved to java.util.ArrayList"
and "The type of the expression must be an array type but it resolved to java.util.ArrayList", I got that second one twice.
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String Continue = "y";
int Count = 0;
int SortingChoice;
ArrayList<String> Names = new ArrayList<String>();
ArrayList<String> Department = new ArrayList<String>();
ArrayList<String> Designation = new ArrayList<String>();
ArrayList<Float> Salary = new ArrayList<Float>();
//******************************************************//
do
{
System.out.println("Enter Employee Name: ");
String x = in.next();
Names.add(x);
System.out.println("Enter Employee Designation ('temporary or permanent'): ");
String y = in.next();
Designation.add(y);
System.out.println("Enter Employee Department: ");
String z = in.next();
Department.add(z);
System.out.println("Enter Employee Salary: ");
float i = in.nextFloat();
Salary.add(i);
System.out.println("Do you wish to add another employee? ('y'/'n'): ");
Continue = in.next();
Count = Count + 1;
}
while(Continue.equals("y"));
//***********************************************************************//
System.out.println("Enter sorting Criterion Number: 1. Name, 2.Department, 3. Salary. ");
SortingChoice = in.nextInt();
if(SortingChoice == 1)
{
Collections.sort(Names);
for(int i=0; i<Names.size(); i++)
{
System.out.println(Names.get(i));
}
}
if(SortingChoice == 2)
{
Collections.sort(Department);
for(int i=0; i<Department.size(); i++)
{
System.out.println(Department.get(i));
}
}
if(SortingChoice == 3)
{
for(int k=0; k<Salary.size(); k++)
{
if(Designation[k].equals("temporary"))
{
Salary[k] = Salary[k]*1920;
}
}
Collections.sort(Salary);
for(int i=0; i<Salary.size(); i++)
{
System.out.println(Salary.get(i));
}
}
}
What I am most concered about if the loop under if(SortingChoice==3)

Related

How can I check if a user input of 2D Array contains a given user input integer value? If found, how can I print its index?

For example, a given user input array is {{1,2,3},{4,5,6}} and the user input integer value is 6. If 6 is in the array, I want to print its index (for example [1][2]). I was able to write the code that will scan the given elements of the array however, I'm not sure what to do next in order to find the given integer value in the array.
Here's my code:
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
int toFind;
System.out.print("Enter the number of rows: ");
int row =scan.nextInt();
System.out.print("Enter the number of columns: ");
int column =scan.nextInt();
int input[][] = new int[row][column];
System.out.println("Elements: ");
for(int ctr1 = 0; ctr1 < input.length; ctr1++)
{
for(int ctr2 = 0; ctr2 < input[ctr1].length; ctr2++)
{
input[ctr1][ctr2] = scan.nextInt();
}
}
System.out.println("Enter the number you want to find: ");
toFind = scan.nextInt();
I'm not sure what to next here in order to initiate the checking of the number in the given array and print its index.

It should continue to accept a price of an item and displays the current total while the user input is not equals to zero

/* I need some help. I'm stuck with this code. It should continue to accept a price of an item and displays the current total while the user input is not equal to zero. However, output for the current total won't show the current amount. */
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String customerName;
char promtSenior;
int num = 1;
double itemPrice;
double currentTotal = 0;
ArrayList<Double> amounts = new ArrayList<Double>();
System.out.print("Please enter customer name: ");
customerName = scanner.nextLine();
while (true) {
System.out.print("Please enter item " + num++ + " price or 0 to quit: ");
itemPrice = scanner.nextDouble();
amounts.add(itemPrice);
scanner.nextLine();
for (double i : amounts) {
currentTotal = currentTotal + i ;
}
System.out.println(currentTotal);
if (itemPrice == 0) {
System.out.println("Are you a Senior Citizen? [Y/N]: " + customerName);
promtSenior = scanner.next().charAt(0);
if (promtSenior == 'N') {
System.out.println("No Discount");
}
}
}
}
}
while (true) {
System.out.print("Please enter item " + num++ + " price or 0 to quit: ");
itemPrice = scanner.nextDouble();
amounts.add(itemPrice);
scanner.nextLine();
for (double i : amounts) {
currentTotal = currentTotal + i ;
System.out.println(currentTotal);
}
should be this as previously you where printing the total after the for loop not during
i think you accidentally messed up your curly brackets and indented before

Create for loop in Java to take in information

I want to use a for loop to ask the user for a movie title, genre and rating and get for it to iterate three times and store in an array and display the information back to the user.
In the for loop in the code, I am not sure what to insert as "Movie 1" and how to get it to change to "Movie 2" and "Movie 3" after each iteration.
This is what I have so far :
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Creates a Movie array of size three.
Movie[] movies = new Movie[3];
for (int i=0; i < movies.length; i++) {
// Initialises the 3 movie objects in the array.
movies[i] = new Movie();
}
// Loop will execute 3 times.
for (int x = 0; x < 3; x += 1){
System.out.println("Please enter the title of Movie 1: ");
System.out.println("Please enter the genre of Movie 1: ");
System.out.println("Please enter the rating of Movie 1: ");
}
}
Please post your Movie class to help us suggest a more detailed answer.
you can try something like this ...
Scanner sc=new Scanner(system.in);
for (int x = 0; x < 3; x++){
System.out.println("Please enter the title of Movie 1: ");
movies[i].title=sc.next();
System.out.println("Please enter the genre of Movie 1: ");
movies[i].genre=sc.next();
System.out.println("Please enter the rating of Movie 1: ");
movies[i].genre=sc.nextInt();
}
Use x++ instead of x+=1 , try using enhanced for loop in Java , and import java.util.* for the Scanner to work
You did most of the stuff right, just that you need to figure out what to do when as explained in comments below:
Scanner input = new Scanner(System.in);
// Creates a Movie array of size three.
//here you are declaring your array of movies of size 3
Movie[] movies = new Movie[3];
String name;
//lets ask user to enter movie name three times and simultaneously populate movie object and store them in an array.
for (int i=0; i < movies.length; i++) {
System.out.println("Please enter the title of Movie " + (i+1));
name = input.nextLine();
movies[i] = new Movie(name);
}
// Loop will execute 3 times and just print out the movie names that we populated.
for (int x = 0; x < movies.length; x += 1){
System.out.println("movie " + (x+1) + " is " + movies[x].name);
}

reached end of file while parsing,

what is wrong with this , i am getting an error that states: reached end of file while parsing. what do i do to fix this?
i have tried several thing with no result
public class FindMin
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int smallest = 9999999;
String userInput;
boolean quit = false;
System.out.println("This program finds the smallest number"
+ " in a series of numbers");
System.out.println("When you want to exit, type Q");
while(quit != true)
{
System.out.print("Enter a number: ");
userInput = keyboard.next();
if(userInput.equals("Q")) userInput.equals("q");
{
if(quit == true) {
}
else
{
int userNumber = Integer.parseInt(userInput);
if(UserNumber < smallest)
smallest = userNumber;
}
}
System.out.println("The smallest number is " + smallest);
System.exit(0);
}
Check your braces: they don't match up, which is why the compiler complains:
FindMin.java:35: reached end of file while parsing
Besides there's a typo and some other issues with that program. See other answers for reference :)
It was just too messy, try this
import java.util.Scanner;
public class FindMin{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int smallest = 9999999;
String userInput;
boolean quit = false;
System.out.println("This program finds the smallest number"
+ " in a series of numbers");
System.out.println("When you want to exit, type Q");
while(true)
{
System.out.print("Enter a number: ");
userInput = keyboard.next();
if(userInput.equalsIgnoreCase("q"))
break;
else{
int userNumber = Integer.parseInt(userInput);
if(userNumber < smallest)
smallest = userNumber;
}
}
System.out.println("The smallest number is " + smallest);
}
}

Error in code, NullPointException

I am working on a program for class due today at midnight and I just can't figure why it's giving me this Null Point Exception error. I would really appreciate if you guys can look at my code and help me out.
The goal of the project is as follows..
Program Statement: Write a Payroll class that uses the following arrays as fields:
employeeId - an array of 7 integers to hold employee id numbers. The array field should be initialized with the following numbers:
5658845
4520125
7895122
8777541
8451277
1302850
7580489
hours - An array of seven integers to hold the number of hours worked by each employee
payRate - An array of seven doubles to hold each employee's hourly rate.
wages - An array of seven doubles to hold each employee's gross wages.
The class should relate the data in each array through the subscripts. For example, the number in element 0 of the hours array should be the number of hours worked by the employee whose ID number is stored in Element 0 of the employeeId array. That same employees pay rate should be stored in Element 0 of the payRate array. In addition to the appropriate accessor and mutator methods, the class should have a method that accepts an employee's id number as an argument and returns the gross pay for that employee. Demonstrate the class in a complete program that displays each employee number and asks the user to enter the employee's hours and pay rate. It should then display each employee's id number and gross wages.
Input Validation : Do not accept negative values for numbers and numbers less than 6.00 for pay rate.
int[] employeeId;
int[] hours;
double[] payRate;
double[] wages;
Scanner kboard = new Scanner(System.in);
public ParrishPayroll(int[] ids){
employeeId = new int[ids.length];
// Copy the values in ids
for (int index = 0; index < ids.length; index++)
employeeId[index] = ids[index];
System.out.println("Employee ID's");
System.out.println(Arrays.toString(employeeId));
System.out.println("Please enter the id number you would like to edit.");
int input = kboard.nextInt();
if(input == 5658845){
int index = 0;
setHours(index);
}
else if(input == 4520125){
int index = 1;
setHours(index);
}
else if(input == 7895122){
int index = 2;
setHours(index);
}
else if(input == 8777541){
int index = 3;
setHours(index);
}
else if(input == 8451277){
int index = 4;
setHours(index);
}
else if(input == 1302850){
int index = 5;
setHours(index);
}
else if(input == 7580489){
int index = 6;
setHours(index);
}
else {
System.out.println("Invalid ID number!");
}
}//end startSequence
public void setHours(int i){
System.out.println("How many hours were worked?");
hours[i] = kboard.nextInt();
if(hours[i] < 0){
System.out.println("Please input a positive number.");
kboard.nextInt(hours[i]);
setPayRate(i);
}
else
setPayRate(i);
}
public void setPayRate(int index){
int input = index;
System.out.println("What is the employee's pay rate?");
payRate[input] = kboard.nextDouble();
if(payRate[input] < 0){
System.out.println("Please input a positive number.");
payRate[input] = kboard.nextDouble();
calcWages(input);
}
else if(payRate[index] < 6.00){
System.out.println("Wages must be higher than $6.00.");
payRate[input] = kboard.nextDouble();
calcWages(input);
}
}
public void calcWages(int index){
int input = index;
wages[input] = hours[input] * payRate[input];
}
public void getGross(int i){
int input = i;
if(input == 5658845){
System.out.print("Employee number: " + input + " wages: " + wages[0]);
}
else if(input == 4520125){
System.out.print("Employee number: " + input + " wages: " + wages[1]);
}
else if(input == 7895122){
System.out.print("Employee number: " + input + " wages: " + wages[2]);
}
else if(input == 8777541){
System.out.print("Employee number: " + input + " wages: " + wages[3]);
}
else if(input == 8451277){
System.out.print("Employee number: " + input + " wages: " + wages[4]);
}
else if(input == 1302850){
System.out.print("Employee number: " + input + " wages: " + wages[5]);
}
else if(input == 7580489){
System.out.print("Employee number: " + input + " wages: " + wages[6]);
}
else {
System.out.println("Invalid ID number!");
getGross(input);
}
}
public static void main(String[] args){
int[] idlist = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
ParrishPayroll user1 = new ParrishPayroll(idlist);
Scanner kboard = new Scanner(System.in);
System.out.println("What employee would you like to see the gross wages for?");
int i = kboard.nextInt();
user1.getGross(i);
}
It looks like you are not initializing the hours, payRate or wages variables, which means they are all null. When you call, say, hours[i] = kboard.nextInt() it will throw a NullPointerException because you can't set the i th element of null.
Make sure you have a line like hours = new int[whatever] before you try to use the hours variable (same for the other two). You've got it right for employeeId, so follow that pattern.

Resources