I am having a little trouble figuring out what to do when it comes down to this project (generally the same for all my projects). I just want to know if I'm headed in the right direction also, I am not sure how to store the float number in the int double array. Thanks
Question & SampleOutput
Write a Java program that prompts the user to enter an integer. Use the integer as the size for a new double array. Use a for loop to prompt the user to enter a floating point number for each element in the
array, and store the number in the array. Use a second for loop to, calculate the average of all of the numbers and print it.
Your output should look something like the following:
How many numbers will you enter: 5
Enter a decimal value: 21.2
Enter a decimal value: 3.7
Enter a decimal value: 10.5
Enter a decimal value: 2.6
Enter a decimal value: 101.123
The average is 27.824599999999997
My current code:
import java.util.*;
public class Loops {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" How many numbers will you enter: ");
int numberOfTimes = input.nextInt();
int[][] Array = new int[numberOfTimes][];
for(int i = 0; i < Array.length; i--)
{
System.out.print("Enter a decimal value: ");
float value = input.nextInt();
Array[i][
{
}
}
}
}
PS: My apologies for bad formatting. New to Stack.
Sir,
The major mistake you're committing is considering double as two. 'Double' is also a data type just like integers and float. So you need only a one dimensional array to store the values which are of type 'double'.
Secondly, i would suggest you to try to completing the code yourself once. If you're having issues in compiling it or getting wrong output, feel free to post the code online.
The desired result could have been achieved with only one 'for' loop, but I've provided the code 2 'for' loops only for the clarity.
Here's the code-
import java.util.Scanner;
public class AverageNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many numbers will you enter: ");
int numberOfTimes = input.nextInt();
double AverageMean = 0;
double[] Array = new double[numberOfTimes];
// Doing the first loop as suggested
for(int i = 0; i < numberOfTimes; i++)
{
System.out.print("Enter a decimal value: ");
Array[i] = input.nextDouble();
}
// Doing the second loop as suggested
for(int i = 0; i < numberOfTimes; i++)
{
AverageMean = AverageMean + Array[i];
}
System.out.println("The average is " + AverageMean/5);
}
}
And the desired output will be-
How many numbers will you enter: 5
Enter a decimal value: 21.2
Enter a decimal value: 3.7
Enter a decimal value: 10.5
Enter a decimal value: 2.6
Enter a decimal value: 101.123
The average is 27.824599999999997
Hope it helps..
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" How many numbers will you enter: ");
int numberOfTimes = input.nextInt();
float[] input = new int[numberOfTimes];
for(int i = 0; i < numberOfTimes; i++) {
System.out.print("Enter a decimal value: ");
float value = input.nextInt();
Array[i] = value;
}
}
1) You need a double array instead of a two dimensional int array to store double.
2) Use input.nextDouble(); to take a double input.
3) for(int i = 0; i < Array.length; i--) will cause i to decrease negative which will next create ArrayIndexOutofBound exception at Array[i].
4) There are some other errors, better try the following code.
Try this:
import java.util.*;
public class Loops {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" How many numbers will you enter: ");
int numberOfTimes = input.nextInt();
double[] Array = new double[numberOfTimes];
double sum = 0;
double average = 0;
for (int i = 0; i < numberOfTimes; i++) {
System.out.print("Enter a decimal value: ");
double value = input.nextDouble();
Array[i] = value;
sum += value;
}
average = sum / numberOfTimes;
System.out.print("The average is: " + average);
}
}
Output:
How many numbers will you enter: 5
Enter a decimal value: 21.7
Enter a decimal value: 3.7
Enter a decimal value: 10.5
Enter a decimal value: 2.6
Enter a decimal value: 101.123
The average is: 27.924599999999998
Related
I'm learning programming in Java, and I found one interesting example and solved it 80%. But there is this part that I don’t know exactly how to solve. I should enter an array indefinitely until I write "0" and then that array should break that array and add the sum of the numbers (but it is necessary that the decimals of the numbers are not limited, and the final sum at the end that is printed is rounded to 2 decimals .)
At the same time, I want to delete this part "Enter the number of purchased items: " where he asks how many items I will have, but only to enter without that part and stop collecting the sum.
import java.util.Arrays;
import java.util.Scanner;
public class SumOfElementsOfAnArray {
public static void main(String args[]){
System.out.println("Enter the number of purchased items: "); // I want to enter a array without this part
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int myArray[] = new int [size];
double sum = 0;
System.out.println("Enter item prices (each separately) : ");
for(int i=0; i<size; i++){
myArray[i] = s.nextInt();
sum = sum + myArray[i];
}
System.out.println("The total bill amount is:" +sum + "$");
}
}
Example with BigDecimal:
BigDecimal myArray[] = new BigDecimal [size];
BigDecimal sum = new BigDecimal(0);
System.out.println("Enter item prices (each separately) : ");
for(int i=0; i<size; i++){
myArray[i] = s.nextBigDecimal();
sum = sum.add(myArray[i]);
}
Use doble and System.out printf,
excample:
double myArray[] = new double [size];
double sum = 0;
System.out.println("Enter item prices (each separately) : ");
for(int i=0; i<size; i++){
myArray[i] = s.nextDouble();
sum = sum + myArray[i];
}
System.out.printf("The total bill amount is:%.2f$",sum);
I have to create a program that gets a range from the user then adds each even number in the range and prints to screen. I am struggling to figure out how to add each number in the for loop. If the range is 5 to 10 the screen should print 24 as the answer. Below is my code.
//worksheet 4 - question 3
//asks user for a range. Adds even numbers in range and prints their sum.
import java.util.Scanner;
class P3LoopRange
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int initial, finalValue;
System.out.print("Enter a start value: ");
initial = input.nextInt();
System.out.print("Enter a second value: ");
finalValue = input.nextInt();
while (finalValue <= initial)
{
System.out.println("Second value must be larger than start value.");
System.out.print("Enter a second value: ");
finalValue = input.nextInt();
}
int range = finalValue - initial;
int x = 0
for (int i = initial; i <= finalValue; i++)
{
if (i%2==0)
{
int x = x+i;
}
}
System.out.printf(x);
}
}
You declare twice the x local variable.
You cannot as it is not legal to have a duplicate local variable.
Besides, declaring it inside the loop makes it be overwritten at each iteration.
So, just declare it before the loop and it should be fine.
int x = 0;
for (int i = initial; i <= finalValue; i++)
{
if (i%2 == 0)
{
x = x + i;
}
}
Change int x = x+i; to x=x+i;. Here you are re initializing x each time.
Problem is at: int x = x+i;
just remove int and your problem is solved
Having difficulty trying to write code for this problem above. Please find the code below. Have to read in 5 numbers and compute the frequency of positive numbers entered.
import java.util.Scanner;
public class Lab02Ex2PartB {
public static void main (String [] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a positive integer");
int number = input.nextInt();
for(int i = -2 ; i < 4 ; i++)
System.out.println("Positive Count is: " + i);
}
}
Your problem is that you have a task that needs to be repeated (about the user entering a value); but your loop (the perfect mean to do things repeatedly) ... doesn't cover that part!
for(int i=-2 ; i<4 ; i++)
System.out.println("Positive Count is: " +i);
Instead, do something like:
for (int loops = 0; loops < 5; loops++) {
int number = input.nextInt();
Then of course, you need to remember those 5 values, the easiest way there: use an array; Turning your code into:
int loopCount = 5;
int numbers[] = new[loopCount];
for (int loops = 0; loops < loopCount; loops++) {
numbers[loops] = input.nextInt();
And then, finally, when you asked for all numbers, then you check the data you got in your array to compute frequencies. A simple approach would work like this:
for (int number : numbers) {
if (number > 0) {
System.out.println("Frequency for " + number + " is: " + computeFrequency(number, numbers));
}
with a little helper method:
private int computeFrequency(int number, int allNumbers[]) {
...
Please note: this is meant to get you going - I don't intend to do all your homework for you. You should still sit down yourself and figure what "computing the frequency" actually means; and how to do that.
Try this one, Remember if you only want to know the frequency(not storing)
import java.util.Scanner;
public class Lab02Ex2PartB {
public static void main (String [] args){
int i = 1;// is a counter for the loop
int positive =0;// counts positive numbers
while(i<=5){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a whole positive number");
int number = input.nextInt();
if(number > 0){
positive ++;
}
i++;
}
System.out.println("Positive Count is: "+ positive);
This is one of my very first java projects and I'm trying to make a mini calculator and right now I'm working on addition.
What I want it to do is like it will ask the user how many numbers they want to add and then after you type all the numbers, and the java code has to get all the numbers that inputted.
Here's the addition part that doesn't work so far:
private static void Addition() { //I already added the Scanner plugin
System.out.println("How many numbers would you like to add?");
Scanner adds = new Scanner(System.in);
int addsput = adds.nextInt();
Scanner numa = new Scanner(System.in);
for(int addloop=1; addloop>addsput; addloop++) {
int numaput = adds.nextInt();
//somehow I want to get all the numbers
}
//Here I want to add all the numbers they typed
}
So I hope you get the idea. Any help would be great, because I've been searching for about an hour to get this figured out. Thanks.
You have two options, either read the values into an array, or find the sum as you read the values.
You only need one Scanner object, and your for loop had some issues:
private static void addition() {
Scanner input = new Scanner(System.in);
System.out.println("How many numbers would you like to add?");
int amountNumbers = input.nextInt();
int sum = 0;
for (int counter = 0; counter < amountNumbers; counter++) {
sum += input.nextInt();
}
System.out.println("Sum: " + sum);
}
Using an array:
private static void addition() {
Scanner input = new Scanner(System.in);
System.out.println("How many numbers would you like to add?");
int[] numbers = new int[input.nextInt()];
for (int index = 0; index < numbers.length; index++) {
numbers[index] = input.nextInt();
}
int sum = 0;
for (int index = 0; index < numbers.length; index++) {
sum += numbers[index];
}
System.out.println("Sum: " + sum);
}
Here is a more advanced way to do it using IntStream from Java 8:
private static void addition() {
Scanner input = new Scanner(System.in);
System.out.println("How many numbers would you like to add?");
int amountNumbers = input.nextInt();
int sum = IntStream.generate(input::nextInt)
.limit(amountNumbers)
.sum();
System.out.println("Sum: " + sum);
}
Here are a few things I would recommend changing:
You only need 1 scanner. And as for adding to a sum, if you make the variable before the loop, you can just add to the sum in the same line you take input.
You also had the < sign mixed up with a > sign. You want the loop to go until the variable addloop has been incremented the amount of times the user wants to input a number to add. Therefore, the loop should continue until it reaches the number the user entered, rather than the other way around.
System.out.println("How many numbers would you like to add?");
Scanner adds = new Scanner(System.in);
int addsput = adds.nextInt();
int sum = 0;
for(int i = 0; i < addsput; i++){
sum += adds.nextInt();
}
System.out.println(sum);
Hello I am new to java and am trying to do this assignment but I can't get the needed output which is the reverse order of the numbers. Can any one help me what I am missing? Thank you so much! My Code's output looks like this:
How many floating point numbers do you want to type: 5 Type in 1.
number: 5,4 Type in 2. number: 6 Type in 3. number: 7,2 Type in 4.
number: -5 Type in 5. number: 2
Given numbers in reverse order:
Assignment
Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.
Program is written to a class called ReverseNumbers.
Example output
How many floating point numbers do you want to type: 5
Type in 1. number: 5,4
Type in 2. number: 6
Type in 3. number: 7,2
Type in 4. number: -5
Type in 5. number: 2
Given numbers in reverse order:
2.0
-5.0
7.2
6.0
5.4
My code:
import java.util.Scanner;
public class apples {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double[] numbers;
System.out.print("How many floating point numbers do you want to type: ");
int size = reader.nextInt();
numbers = new double[size];
for (int i=0; i < numbers.length; i++) {
System.out.print("Type in "+(i+1)+". number:");
numbers[i-1] = reader.nextDouble();
}
System.out.println();
System.out.println("Given numbers in reverse order:");
for (int i=numbers.length-1; i <= 0; i--) {
System.out.println( numbers[i]);
}
}
}
The second for loop condition is wrong. It should be:
for (int i = numbers.length - 1; i >= 0; i--)
because i <= 0 won't be true if numbers.length - 1 is greater than 0, therefore your for loop won't run its body.
Also in the first for loop, the index of numbers are wrong. It should be:
numbers[i] = reader.nextDouble();
This is because if i = 0, then numbers[i - 1] will be numbers[-1], and that's an invalid index.
In the first loop you are using i+1 and not i++, so the value of i is never incremented. So, your
numbers[i-1] becomes numbers[-1], which should throw an error. I am astonished, now your program is even giving you an output !
Try this
for (int i=0; i < numbers.length; i++) {
System.out.print("Type in "+(i+1)+". number:");
//numbers[i-1] = reader.nextDouble();
numbers[i] = reader.nextDouble();
}
and in second loop. You are checking from last to first so i>=0 instead of i<=0
for (int i=numbers.length-1; i >= 0; i--) {
System.out.println( numbers[i]);
}
int[] numbers;
System.out.print("How many integer point numbers do you want to type: ");
int size = reader.nextInt();
numbers = new int[size];
for (int i=0; i < size; i++) {
//System.out.print("Type in "+(i+1)+". number:");
numbers[i] = reader.nextInt();
}
System.out.println();
System.out.println("Given numbers in reverse order:");
for (int i=size-1; i >= 0; i--) {
System.out.print( numbers[i]);
}