This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
How to find the maximum value in an array? [duplicate]
(3 answers)
Closed 5 years ago.
I am a beginner programmer working on this exercise for an online class. I'm supposed to write a program that takes ten numbers from a user, finds the lowest number, and prints it back. I've viewed a few other similar questions, but couldn't find anything that worked for me. Here's what I have thus far, no errors or anything. I can't figure out why it gives me this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Exercise6_9.min(Exercise6_9.java:41)
at Exercise6_9.main(Exercise6_9.java:31)
after entering 10 numbers. As a note, I have looked at this question, they are not the same. I wasn't quite asking what my error was and how to solve it, but rather why it's coming up in my example when my IDE shows no problems prior to running the program.
import java.util.Scanner;
public class Exercise6_9 {
public static void main(String[] args) {
double[] numbers = new double[10];
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 numbers.");
for (int i = 0; i < numbers.length; i++ ){
numbers[i] = input.nextDouble();
}
min(numbers);
}
public static double min(double[] numbers){
double min = numbers[10];
for (int i=0;i<numbers.length;i++){
if (numbers[i] < min){
min = numbers[i];
System.out.println(min);
}
}
return min;
}
}
The array index start from 0 , so you don't have an element numbers[10];, instead the 10th element is represented by :
double min = numbers[0];
// ^-------//this is the 1st element
double min = numbers[9];
// ^-------//this is the 10th element
Related
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 3 months ago.
I have been writing the below mentioned code for the problem and it always shows me the error that my index is out of bounds for my length. I have tried printing the size and it is comparitively very large.
import java.util.*;
class Main {
public static void countingSort(int numbers[]) {
// int n = numbers.length - 2;
int largest=Integer.MIN_VALUE;
for (int i=0;i<numbers.length;i++){
largest=Math.max(largest,numbers[i]);
}
int count[]=new int[largest+1];
System.out.println(count.length+"= length\n");
for (int i=0;i<count.length;i++)
count[numbers[i]]++;
int j=0;
for (int i=0;i<count.length;i++){
while(count[i]>0){
numbers[j]=i;
j++;
count[i]--;
}
}
for(int i=0;i<numbers.length;i++)
System.out.println(numbers[i]);
}
public static void main(String args[]) {
int numbers[] ={5,8,7,19,25,4,2,3,1};
countingSort(numbers);
}
It is because your variable "numbers" only holds 9 values.
for (int i=0;i<count.length;i++)
count[numbers[i]]++;
This for loop will iterate depending on the size of your variable "count". In your case, the variable "count" will hold 26 values. It throws an error when the variable "numbers" is being accessed with an index beyond its size.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
import java.util.Scanner;
public class Java3{
int sumAll = 0;
int sumFive = 0;
int sumLast = 0;
int arr[];
int n;
void arraySum(int[] arr) {
for (int i = 0; i < arr.length; ++i) {
sumAll += arr[i];
if (i == 4)
sumFive += sumAll;
if (i >= (arr.length - 5))
sumLast += arr[i];
}
System.out.println("sum all = " + sumAll
+ " sum first five " + sumFive
+ " sum last five " + sumLast);
}
public static void main(String [] args) {
Java3 obj1 = new Java3();
Scanner scan1 = new Scanner(System.in);
System.out.println("enter the number of elements");
obj1.n = scan1.nextInt();
System.out.println("enter the elements");
for (int i = 0; i < obj1.n; ++i)
obj1.arr[i] = scan1.nextInt();
obj1.arraySum(obj1.arr);
}
}
The error I get
java.lang.NullPointerException
Errors on Null pointer exception has been asked and answered several times before, but I do not understand those explanations. Can anyone please explain it in a very easy manner. This is my code, and it is showing me the error at runtime.
This code is supposed accepts the size of an integer array followed by the integer array. Then calls a function which calculates:
sum of all digits
sum of first 5 digits
sum of last 5 digits
and prints each value in the console..
Seems like you are a beginner in coding, nullPointerException occurs whenever you are trying to access the value of an object that is not defined.
In your case you did not define your array. You should have defined your array as given below before trying to add values into the array this will remove the exception.
obj1.arr = new int[obj1.n];
Doing this will define your array in your object and with obj1.n it will initialize its size. Hope you can proceed from here. Please refer the usage of new and constructors in java.
The problem was you took the length of array as input , but you did not initialize the array . Please add this one line in the code:
obj1.n = scan1.nextInt();
obj1.arr = new int[obj1.n];
You should define your arr size. until you define the arr size it is null, and there is no memory will be allocated. You can avoid this by using ArrayList instead of array or define your arr with sum big number of size and asker user to enter the number of values less than that size.
int arr[] = new int[100];
define the array size once you know the value
obj1.arr = new int[obj1.n+1];
This question already has answers here:
Error message for arrays [duplicate]
(2 answers)
Closed 5 years ago.
I know that the problem lies in the command line, but I have tried every which way to find a way to solve it, but I have completely no idea how I can fix the array to not be zero. I am still really new to arrays and have looked everywhere in my textbook on how to do this, but the only example does not include arrays.
here is the error I am getting.
I am using Netbean
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at arraysize.Arraysize.main(Arraysize.java:20)
Here is my code as well
package arraysize;
public class Arraysize {
public static void main(String[] args) {
int[] array = new int[ 10 ];
for ( int counter = 0; counter < array.length; counter++)
array[counter] = Integer.parseInt(args[counter]);
System.out.printf("%s%8s\n", "Index", "Value");
for (int counter = 0; counter < array.length; counter++)
System.out.printf("%5d%8d\n", counter, array[counter]);
}
}
It is because your input args array length is less than length of array, in the moment you iterate to 10 in row
array[counter] = Integer.parseInt(args[counter]);
and in moment counter variable exceed args length you get exception, for solve this you need to
pass args array with 10 or more length. To checkargs array argument you may pass it's size to console:
public static void main(String[] args) {
System.out.printf("arguments array size is: %d", args.length);
//your code
}
This question already has answers here:
Check whether number is even or odd
(16 answers)
Closed 6 years ago.
Extremely new to Java, have just started using it in the last 2 weeks in school, we've been given the assignment to make a program to determine whether 5 given intergers are even or odd and then give an output to look like this
Even: 2
Odd: 3
We need to use the modulo operator and switch statements, and i'm just not sure how to go about this, any help would be appreciated.
Let us say you got 5 numbers. Create the array for them and then loop through it. Check every element if the remainder of division by 2 is 0 or 1.
int even = 0;
int odd = 0;
int[] array = new int[5];
for (int i=0; i<array.length; i++)
if (array[i]%2 == 0)
even++;
else
odd++;
You need a loop, inside distinguish if odd or even (switch), modulo as part of the condition and counters. Think about what the second argument of modulo must be to find out if odd or even.
If you have written code and it does not work you can extend your question and get further help.
Hope it helps
import java.util.Scanner;
public class Out {
public static void main(String[]args){
int even=0;
int odd=0;
Scanner scan= new Scanner(System.in);
int num =0;
while(true){
System.out.println("Enter a number or -99 to quit :");
num=scan.nextInt();
if(num==-99){
System.out.println("Even: "+even+" odd: "+odd);
break;
}
if(num%2==0){
even++;
}
else if(num%2!=0){
odd++;
}
}
}
}
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
The program should print out values stored in an array of size 5 after the user has input the values.
Here is my code:
import java.util.Scanner;
public class Arrays_Qu1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int arr[]= new int [5];
System.out.println("Enter a number");
int i;
for (i=0;i<arr.length;i++) {
arr[i]=sc.nextInt();
}
System.out.println(arr[i]);
}
}
After I enter the 5th value, the program does not terminate but instead throws:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
Because you are printing outside the loop and it is trying to print arr[5] which is out of bound of the array. The print should be in loop if you want to print each element.
int i;
for (i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
System.out.println(arr[i]); // to print each element
}
// value of i is now 5, so arr[i] is invalid
System.out.println(arr[i-1]); // to print last element
System.out.println(Arrays.toString(arr)); // to print whole array
You should access the last element of the array like:
System.out.println(arr[i - 1])
but I believe printing just the last element of an array is not what you want. So you should move line
System.out.println(arr[i])
in a for loop and it should be ok.