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
}
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's the simplest way to print a Java array?
(37 answers)
How to print [ ] in an array (Java)
(7 answers)
Closed last year.
I am struggling to properly output the elements of an array in java. I know I should use a for loop to print out the array, but I am not sure exactly where to start in order to make sure all the values are printed after the function call in main. Any advice would be appreciated. Thank you!
/*
1 creator shares with 3 friends
1 minute = 3 shares/ friend
what does it look like after 10 mins
*/
public int[] shares(int minutes, int people){
int[] array = new int [20];
for(int i = 1; i <= minutes; i++){
people += Math.pow(3, people);
array[i] = people;
}
return array;
}
public static void main(String args[]){
Prc test = new Prc();
System.out.println(test.shares(10, 1));
}
In arrays, you should start with zero.
The for loop could be something like this:
for(int i=0; i<myArray.length; i++){
System.out.println(myArray[i]);
}
This question already has answers here:
Scanner error with nextInt() [duplicate]
(1 answer)
NoSuchElementException with Java.Util.Scanner
(10 answers)
Closed 5 years ago.
i looked around a bit here on the site but found nothing that seemed to help me on this problem.
so here's my little issue. lets say i have a function that looks like this
public static void main(String args[])
{
int[][] array = new int[9][9];
createArray(array);
}
and i'm trying to pass array into this method so that i can initialize it with input that was read from the console.
public static void createArray(int[][] array)
{
Scanner input = new Scanner(System.in);
int i = 0;
int j = 0;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
array[i][j] = input.nextInt();
}
}
input.close();
}
i would think that this would work, since java passes arrays by reference so that means that the createArray() method is receiving the memory address of the array back in main, and therefore any changes here affects the original one in main.
but for some reason that i'm not seeing, running this gives me these errors, and i dont understand what i'm doing wrong
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 5 years ago.
Please help on this code error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -3
at FindDuplicate.firstDuplicate(File.java:9)
at FindDuplicate.main(File.java:19)
class FindDuplicate {
public static int firstDuplicate(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
if (a[Math.abs(a[i])] < 0)
return Math.abs(a[i]);
else
a[a[i]] = -a[a[i]];
}
return -1;
}
/* Driver program to test the above function */
public static void main(String[] args) {
int[] arr = {
2,
4,
3,
5,
1
};
System.out.println(firstDuplicate(arr));
}
}
try to solve this problem
Note: Write a solution with O(n) time complexity and O(1) additional space complexity, since this is what you would be asked to do during a real interview.
Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does. If there are no such elements, return -1.
Either take array element 0 to 4
Or change your logic inside for loop : decrease 1 inside array index
eg. a[a[I] - 1] = ...like this
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