Input loop number missing loop - java

Whenever I input x (number of empoyee) as any number it always short in 1 loop when asking the employee salary. And if i put x as 1 it doesn't ask me.
Need help how to fix it.
class salary {
public static void main(String[] args) {
int x = 0;
int y;
System.out.println("Enter how many employee");
x = EasyIn.getInt();
for (int i = 1; i < x; ++i) {
System.out.println("Enter the salary of employee " + i);
y = EasyIn.getInt();
if (y < 20000) {
System.out.println("This employee Bonus Rate is 7%");
}
}
}
}

You have an error in your for loop. It should be:
for(int i = 0; i < x; ++i)
or:
for(int i = 1; i <= x; ++i)
What is EasyIn?
There might also be an issue with your EasyIn class.

Your problem is here
for (int i = 1; i < x; ++i)
Think about the terminating condition of the for loop.

Try something like this -
// start at 0.
for (int i = 0; i < x; ++i)
{
// add one for display.
System.out.println("Enter the salary of employee " + (1+i));
y = EasyIn.getInt();
if(y < 20000)
{
System.out.println("This employee Bonus Rate is 7%");
}
}

you are starting i from 1 instead of zero so it will always short by 1.
Simply change int i=1; to int i=0;

Related

How to initialize variable back to 0 in a while loop in java?

I want my code to loop, but reinitialize the variable back to 0. Every time I input a number, it add it to the previous result, but I want it to reset. I attached two images below. One is the actual output and the other is the expected output.
import java.util.Scanner;
public class AddOrMultiplyNNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String am;
int sum = 0;
int total = 1;
int n = 0;
while (true) {
System.out.print("enter an integer number: ");
n = input.nextInt();
if(n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
am = input.nextLine();
if (am.equals("a")) {
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
}
}
Actual Output
Desired Output
You can use continue
if(n == 0) {
sum = 0;
total = 1;
continue;
}
You can initialize the variables inside the while loop
public class AddOrMultiplyNNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String am;
while (true) {
int sum = 0;
int total = 1;
int n = 0;
System.out.print("enter an integer number: ");
n = input.nextInt();
if(n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
am = input.nextLine();
if (am.equals("a")) {
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
}
}
To zero a variable, simply assign 0 at the appropriate point.
sum = 0;
An appropriate place to insert this statement for your desired output is immediately before the first for loop. Likewise, reset total before the second for.
However, a much better way to write this is to declare the variable where it is needed, as you have done for x and y. This applies to am, sum, total and n
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("enter an integer number: ");
int n = input.nextInt();
if (n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
String am = input.nextLine();
if (am.equals("a")) {
int sum = 0;
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
int total = 1;
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
I don't know if I fully understand your question, but just do sum=0; and total=1; after you print out the final result. You should also consider doing a try/catch statement for robustness on the nextInt so that characters and strings don't break your program...
try {
n = input.nextInt();
}
catch (Exception e) {
System.out.println("Not a Number");
}

How to quit scanner when input is negative?

This is the instructions.
Write a program that reads a sequence of input values and displays a bar chart of the values using asterisks. You may assume that all values are positive. First figure out the maximum value. That's value's bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks.
This is what I came up so far. It's all good except I need to enter a letter instead of a negative number to quit scanning. I have tried (if( < 0) things) but those didn't work.
import java.util.Scanner;
public class BarChart1 {
public static void main(String [] args) {
int[] arr = new int[100];
int currentSize = 0;
System.out.println("Enter a sequence of positive integers. "
+ ("Enter a negative value to quit:"));
Scanner in = new Scanner(System.in);
while(in.hasNextInt()) {
int num = in.nextInt();
if (num < 0) {
break;
}
else {
arr[currentSize] = in.nextInt();
currentSize++;
}
}
//will find the max
double max = arr[0];
int y = 0;
for (int i = 1; i < arr.length; i++) {
y = i + 1;
if(max < arr[i]) {
max = arr[i];
//y = i + 1;
}
}
System.out.println("Max number is: " + max);
System.out.println("Number of digits = " + y);
System.out.println(Math.abs(-1));
double scale = 40/max;
System.out.println("Scale = " + scale);
for (int i = 0; i < y; i++) {
double h = scale * arr[i];
if (h != 0) {
for (int j = 1; j <= h; j ++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
This is the result.
1
2
3
4
-1
Max number is: 4.0
Number of digits = 100
Scale = 10.0
********************
****************************************
I only need the asterisks. Everything else that is being printed is just for checking purposes.
You can try this:
while(in.hasNextInt()) {
int num =in.nextInt();
if(num <0){
break;
}
else{
arr[currentSize] = num;
currentSize++;
}
}

Guessing the number that the user has thought with java (arrays)

I'm kind of new at programming and our teacher asked us to make a programme that can guess the number the user has thought using arrays. I did this:
import java.util.Scanner;
public class Exercise11 {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
System.out.println("Think a number between 1 and 100");
int array[] = new int[100];
for (int x = 0; x < 100; x++) {
array[x] = (int) ((Math.random() * 100) + 1);
}
//This allow us to fill the array with random numbers, without caring if they are repeated or not.
for (int i = 0; i < 100; i++) {
for (int j = 0; i < 100; j++) {
while (true) {
if (i != j && array[i] == array[j]) {
array[j] = (int) ((Math.random() * 100) + 1);
} else
break;
}
//If a number is repeated, this will swap that number with another number.
}
}
//Now we have filled the array. We ask the user:
for (int y = 0; y < 100; y++) {
System.out.println("¿Is it your number " + array[y] + "?");
String respuesta = entrada.next();
switch (respuesta) {
case "Yes":
System.out.println("I knew it! I only needed " + y + " trys!");
break;
case "No":
break;
}
}
}
}
But it is still throwing errors when I execute it, like this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at Ejercicio11.main(Ejercicio11.java:25).
I have tried to debug it but I'm still learning how to do it and I cannot find the mistake. Can someone help me identify where the error is and how can I fix it? Thanks a lot!
The condition of your inner for loop should be
for (int j = 0; j < 100; j++){
(The condition is j < 100, not i < 100)

JAVA: Passing an array to a method

I can't seem to pass my array to a secondary method. It only sees one of the 10 inputs, so I can't properly use it in the secondary method.
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter some numbers: ");
double [] numbers = new double [10];
int n = 0;
int i = 0;
while(n < numbers.length) {
numbers[i] = input.nextDouble();
n+=1;
}
System.out.println(min(numbers));
}
public static double min(double[] array) {
System.out.println(array[1]);
double smallest = array[0];
for (int l = 1; l < array.length; l++) {
if (array[l] < smallest) {
smallest = array[l];
System.out.println("Your smallest = " + smallest);
}
}
return 0;
}
In the first while loop, the variable i does not change.
while (n < numbers.length) {
numbers[i] = input.nextDouble();
n+=1;
}
variable i is never being changed so you are assigning each new number to the same spot in the array overwriting the previous number.
Just use your n variable instead:
while (n < numbers.length) {
numbers[n] = input.nextDouble();
n += 1;
}

Java string triangle

import java.util.Scanner;
public class DrawTriangle
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a height");
while (!scan.hasNextInt()) // while non-integers are present
{
scan.next();
System.out.println ("Bad input. Enter an integer.");
}
int input = scan.nextInt();
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < input; y++)
{
System.out.print(" ");
for (int z = 1; z < y; z++)
{
System.out.print("x");
}
System.out.println();
}
}
}
}
I have to make a triangle of x's relating to the height specified by the user. Can't get it to work at all, any help would be appreciated.
Thanks!
Sorry should have clarified
I need it to look like this -
x
xxx
xxxxx
You don't need nested loop upto 3 levels. Just 2 levels are needed. One for traversing along columns, and one for traversing along rows.
So, change your loop to: -
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < x; y++)
{
System.out.print("x ");
}
System.out.println();
}
UPDATE : -
For equilateral triangle, you would need to add one more loop to print spaces before x on the starting rows. Here's the code: -
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < input - x; y++) {
System.out.print(" ");
}
for (int y = 0; y < x; y++) {
System.out.print("x ");
}
System.out.println();
}

Categories

Resources