I am trying to create a simple program that asks you 10 integers and the program will automatically add them all. I always get an error from Java which is this
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 55
at Sum2.main(Sum2.java:29)
How can I add those multiple array values once?
I tried using
integerArray[0]+[1].....
But it is still not working, please help.
import java.util.Scanner;
public class Sum2 {
private static Scanner sc;
public static void main(String[] args) {
int totalsum;
int[] integerArray = new int[11];
sc = new Scanner(System.in);
System.out.println("Please enter your 10 integers : ");
integerArray[0] = sc.nextInt();
integerArray[1] = sc.nextInt();
integerArray[2] = sc.nextInt();
integerArray[3] = sc.nextInt();
integerArray[4] = sc.nextInt();
integerArray[5] = sc.nextInt();
integerArray[6] = sc.nextInt();
integerArray[7] = sc.nextInt();
integerArray[8] = sc.nextInt();
integerArray[9] = sc.nextInt();
integerArray[10] = sc.nextInt();
totalsum = integerArray[0+1+2+3+4+5+6+7+8+9+10];
System.out.println("The sum of the first 10 integers is: " +totalsum);
}
}
> totalsum = integerArray[0]
+ integerArray[1]
+ integerArray[2]
+ integerArray[3]
+ integerArray[4]
+ integerArray[5]
+ integerArray[6]
+ integerArray[7]
+ integerArray[8]
+ integerArray[9];
Or
totalsum = 0;
for(int i = 0; i < 10; i++) {
totalsum += integerArray[i];
}
By the way, your array contains 11 Integers, not 10.
EDIT: (reply on your comment)
About making code cleaner, this is a lot better than 10 times the same line:
System.out.println("Please enter your 10 integers : ");
for(int i = 0; i < 10; i++) {
integerArray[i] = sc.nextInt();
}
Related
I am trying to find the minimum of a variable input of the scanner class. I have as many inputs as the user wants but I cannot seem to find out how to find the minimum of multiple inputs. Any help would be appreciated.
public static void minimum(int count)
{
double input;
boolean lessThan;
double lesser = 0;
for(count = count; count > 0; count--)
{
System.out.print("Enter a double: ");
input = console.nextDouble();
lessThan = input < input;
if(lessThan = true)
{
lesser = input;
}
else
{
lesser = input;
}
}
System.out.println("The minimum is " + lesser);
}
public static void main(String [] args){
Scanner scanner = new Scanner(System.in);
System.out.println("How many inputs?: ");
int answer = scanner.nextInt();
int arr[] = new int[answer];
int y = 0;
while(y < answer){
System.out.println("Enter a value: ");
arr[y] = scanner.nextInt();
y++;
}
int smallNum = arr[arr.length - 1];
for(int i = arr.length; i > 0; i--){
if(smallNum > arr[i - 1]){
smallNum = arr[i - 1];
}
}
System.out.println("Minimum is: " + smallNum);
}
Here is the answer but my answer is a little bit different. Firstly I created an initialized array because we need to execute the code several times, then I stored the user inputs into the array after that I found the min value using array indexes.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double arr[] = new double[5];
for (int i = 0; i < arr.length; i++) {
System.out.println("Enter the double");
arr[i] = scanner.nextDouble();
}
double min = arr[0];
for (int j=0;j<arr.length;j++){
if(arr[j]<min)
min=arr[j];
}
System.out.println("min value is"+" "+min);
}
import java.util.Scanner;
public class Qa2QeArray2 {
public static void main(String[] args) {
int numOfAircrafts;
Scanner sc = new Scanner(System.in);
System.out.print("Enter num Of Aircrafts : ");
numOfAircrafts = sc.nextInt();
//To Determine the length of the Array
String[] stringArray = new String[numOfAircrafts];
System.out.println("Please enter the names of the Aircrafts : ");
for(int i = 0; i < numOfAircrafts; i++)
{
stringArray[i] = sc.next();
}
//To insert the string values to the Array
double[] doubleArray = new double[numOfAircrafts];
System.out.println("Please enter the shipment rate through each aircraft carrier : ");
for(int i = 0; i < numOfAircrafts; i++)
{
doubleArray[i] = sc.nextDouble();
}
//To insert the double values to the Array
String[] dubArr = new String [stringArray.length * doubleArray.length];
for (int x = 0; x < stringArray.length ; x++)
for (int y = 0; y < doubleArray.length ; y++) {
System.out.println(stringArray[x] + " \t " + doubleArray[y]);
}
}
}
I am looking for o/p as below
Emirates 20.89
Indigo 10.34
But I am getting o/p as
Emirates 20.89
Emirates 10.34
Indigo 20.89
Indigo 10.34
You have two nested loops that are running.
The x loop and y loop.
When x=0 prints the values and again when x=1
it prints values.
That is why you are getting output two times.
Remove one loop and you are good to go.
Trying to find the average of the integers entered as input into the list.
Cant figure out how, im getting an error saying that it can't find symbol in
in the line total = total + in;
import java.util.*;
import java.io.*;
import java.lang.*;
import type.lib.*;
public class Lists
{
public static void main(String[] args)
{
PrintStream print = new PrintStream(System.out);
Scanner scan = new Scanner(System.in);
List<Integer> bag = new ArrayList<Integer>();
print.println("Enter your integers");
print.println("(Negative=sentinel)");
int total = 0;
int count = 0;
for (int in = scan.nextInt(); in > 0; in = scan.nextInt());
{
total = total + in;
count = count + 1;
}
double x = total / count;
print.println("The average is: " + x);
}
}
also, is there an easy way to output the numbers above average divided with a comma?
Remove the semi-colon after the for loop:
for (int in = scan.nextInt(); in > 0; in = scan.nextInt())
The reason in is not defined for the compiler is that the semi-colon would de-scope its definition by acting as an empty body of the for loop.
Remove ; from your loop.
for (int in = scan.nextInt(); in > 0; in = scan.nextInt());
change to
for (int in = scan.nextInt(); in > 0; in = scan.nextInt())
You shouldn't have ; after for loop...
for (int in = scan.nextInt(); in > 0; in = scan.nextInt())//remove ; here
The syntax of a for loop is:
for(initialization; Boolean_expression; update)
{
//Statements
}
You have added semicolon ; so it's become empty body for for loop. you have to remove the semicolon after for (int in = scan.nextInt(); in > 0; in = scan.nextInt()) line in your code.
Modified Code : I am providing code after modification.
public class Lists
{
public static void main(String[] args)
{
PrintStream print = new PrintStream(System.out);
Scanner scan = new Scanner(System.in);
List<Integer> bag = new ArrayList<Integer>();
print.println("Enter your integers");
print.println("(Negative=sentinel)");
int total = 0;
int count = 0;
for (int in = scan.nextInt(); in > 0; in = scan.nextInt()){
total = total + in;
count = count + 1;
}
double x = total / count;
print.println("The average is: " + x);
}
}
The above code requires two changes:
1.You need to remove the semicolon after for loop which is making loop useless and all the iterations you want to do won't take place.
2.The second change is you need to cast one of the variables total or count to double to avoid truncation else your results will always be integers.
The modified code is as follows:
import java.util.*;
import java.io.*;
import java.lang.*;
import type.lib.*;
public class Lists
{
public static void main(String[] args)
{
PrintStream print = new PrintStream(System.out);
Scanner scan = new Scanner(System.in);
List<Integer> bag = new ArrayList<Integer>();
print.println("Enter your integers");
print.println("(Negative=sentinel)");
int total = 0;
int count = 0;
for (int in = scan.nextInt(); in > 0; in = scan.nextInt())
{
total = total + in;
count = count + 1;
}
double x = (double)total / count;
print.println("The average is: " + x);
}
}
Really having issues with this program I'm making. I've searched this site and a few others, although have yet to find a solution. It may look like I'm just soliciting help but I truly am stuck. I am to make a program that reads in 5 numbers from the user and average those numbers. My extent of knowledge of Java is the Scanner class and for loops, yet haven't used while loops yet. Here is the very poorly written code:
public class Average5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num;
System.out.print("Please enter a number: ");
num = sc.nextInt();
for(int num1 = 0; num1 <= num; num1++)
{
System.out.print("Please enter a number: ");
num = sc.nextInt();
}
I honestly have no clue what to do. More or less self taught.
public class Average5 {
public static void main(String args[]) {
int numlenngth = 5;
double total = 0;
Scanner sc = new Scanner(System.in);
for (int num1 = 0; num1 < numlenngth; num1++) {
System.out.print("Please enter a number: ");
total += sc.nextInt();
}
System.out.println("Average : " + (total / numlenngth));
}
}
output >>
Please enter a number: 1
Please enter a number: 1
Please enter a number: 1
Please enter a number: 1
Please enter a number: 2
Average : 1.2
For calculating exact average, you need to take double(sum) instead of int and add(sum) everytime with the user value.
public class Average5
{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Please enter how many numbers you want to enter for calculating average: ");
int totalCount = sc.nextInt();
double sum=0;
for(int i= 0; i<totalCount; i++)
{
System.out.print("Please enter a number: ");
sum += sc.nextDouble();
}
System.out.println("Average of number is"+(sum/totalCount));
}
This is almost the same as the others posted, but it is limited to entering only 5 numbers and it takes care of possibly resulting floating number in the final division:
import java.util.Scanner;
public class Average5 {
private static final int TOTAL = 5;
public static void main(final String[] args) {
final Scanner scanner = new Scanner(System.in);
double sum = 0;
System.out.format("You have to enter %d numbers now.\n", TOTAL);
for (int cnt = 1; cnt <= TOTAL; cnt++) {
System.out.format("Please enter number %d of %d: ", cnt, TOTAL);
sum += scanner.nextInt();
}
System.out.format("The average is %f.\n", sum / TOTAL);
scanner.close();
}
}
Here you can use the array to store the numbers:
public class Average5 {
private static final int MAX = 5;
public static void main(String[] args) {
int[] numbers = new int[MAX];
Scanner sc = new Scanner(System.in);
for(int i = 0; i < MAX; i++) {
System.out.println("Please type the number");
numbers[i] = sc.nextInt();
}
float average = getAverage(numbers);
System.out.println("The average of the five numbers: " + average);
}
public static float getAverage(int[] arg0) {
int sum = 0;
float Ret = 0.0f;
if(arg0 != null) {
for(int i = 0; i < MAX; i++) {
sum += arg0[i];
Ret = sum / MAX;
}
return Ret;
}
}
This is not the only way to slove this problem.
int num = 0;
float total = 0f;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a number: ");
num = sc.nextInt();
for(int num1 = 0; num1 <= num; num1++)
{
System.out.print("Please enter a number: ");
total += sc.nextInt();
}
System.out.println("Average : "+(total/num));
I am building an array that ask how many different inputs you have, then allowing you to enter each input. At the end I want to sum them up, but I keep getting an error.
Also when I go above 5 inputs, I lose one..... For example when I respond to the first question: Enter "10". When I start adding different numbers in it stops at nine. Please help.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int[] numArr = new int[size];
System.out.println("Enter the number of CUs for each course: ");
for (int i=0; i<numArr.length; i++)
{
numArr[i] = input.nextInt();
}
int totalSum = numArr + totalSum;
System.out.print("The sum of the numbers is: " + totalSum);
Change your logic and code to the following:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
System.out.println("Enter the number of CUs for each course: ");
int totalSum = 0;
for (int i = 0; i < size; i++) {
totalSum+=input.nextInt();
}
System.out.print("The sum of the numbers is: " + totalSum);
try
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int totalSum = 0;
for (int i=0; i< size; i++)
{
System.out.println("Enter the number of CUs for each course: ");
int cuNum = input.nextInt();
totalSum += cuNum ;
}
System.out.print("The sum of the numbers is: " + totalSum);
Pretty sure your intended result is
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int[] numArr = new int[size];
int totalSum = 0;
System.out.println("Enter the number of CUs for each course: ");
for (int i=0; i<numArr.length; i++)
{
numArr[i] = input.nextInt();
totalSum += numArr[i];
}
System.out.print("The sum of the numbers is: " + totalSum);
}
Your other code didn't work mainly because of
int totalSum = numArr + totalSum;
You can't define totalSum to defines itself! And you can't just use numArr... numArr is an array - you have to access indexes, not the array as a whole!