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
Related
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 3 years ago.
Hope this is better. I am writing a java assignment to implement a sorting technique. I used a scanner object within a loop to input user data to be sorted. However the scanner object continues to read data after array is filled and an index out of bounds error is shown. How do I deal with this.
public static void set1(int N)
{
int i,c,e,f;
double a,b;
int[] arr1 = new int[N];
int[] arr2 = new int[N];
System.out.println("Please enter numbers to be sorted");
for(i=0;i<=N;i++)
{
Scanner Secondin = new Scanner(System.in);
arr1[i] = Secondin.nextInt();
}
}
From 0 to and including N is too much. a new int[5] has slots 0, 1, 2, 3, and 4. That's 5 slots. arr1[5] is an ArrayIndexOutOfBoundsException because that'd be the 6th slot. Try for (int i = 0; i < N; i++) instead (note, < instead of <=).
Don't keep making scanners. Make 1 scanner, once, and keep using it.
Inside the for loop at the end, try this code:
if (i == N) {
break;
}
This will break out of the for loop as soon as you reach the limit = N.
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:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
I want to shift elements in an array to the right by 1. The last element should go to position 0. So far, I only have the shifting to the right part done, I want to see if this is the right but when I press run, nothing shows up in the console. I know I have to put System.out.println(); in the main but I don't know what to call with the print command. Here is my code.
I tried putting in System.out.println(rotate({1,2,3,4})); into main but I get an error...
public class Rotate {
static void rotate(int[] A) {
int arrayLength = A.length;
for(int i = 0; i <= arrayLength-1; i++){
A[i] = A[i+1];
}
}
public static void main(String[] args){
}
}
You need to make a call to a function that can print, for example System.out.println(). Now there are some other issues with your code and you'll need to make some changes (there is more than one way to do this). One way would be to return an array from your rotate function, and then print the arrays that is returned.
static int[] rotate(int[] A) {
int arrayLength = A.length;
for(int i = 0; i <= arrayLength-1; i++){
A[i] = A[i+1];
}
return A;
}
We can call this now from our main method, and print each element. If you just call the System.out.println()method and pass it an array, it will print the Hashcodefor this object. This is not that usefull for printing information of an array. So instead, you can write a loop and print each element in the array.
public static void main (String[] args)
{
int[] x = {1,2,3,4};
int[] y = (rotate(x));
System.out.println(y) // prints the hash, not what you want..
for(int i = 0; i < y.length(); i++){
System.out.println(y[i]);
}
}
Instead of printing each element of the array seperately, you could also print the entire array using System.out.println(Arrays.toString(y));.
You'll still get an error now
This is because your rotate method is not implemented correctly. But that is beyond the scope of this question I suppose.
To verify that this is actually working, you could try to print the array before applying rotateto it.
int[] x = {1,2,3,4};
for(int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
// or alternative
System.out.println(Arrays.toString(x));
To solve rotate and print problem (and if you don't want to return array):
static void rotate(int[] A) {
int arrayLength = A.length;
int t = A[arrayLength-1];
for (int i = arrayLength - 1; i > 0; i--) {
A[i] = A[i-1];
}
A[0] = t;
for (int i : A) {
System.out.println(i);
}
}
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
so basically this is my code:
Random randomgenerator = new Random();
int[] arr = new int[8];
for (int i = 0; i < arr.length; i++) {
arr[i] = randomgenerator.nextInt(100);
}
System.out.println(arr);
}
}
and this is what appears in the console :
[I#106d69c
I really need help with this, I am probably doing some terrible mistake because I am new to Java coding.
You are printing the whole array, so the default toString method of the array gets called (and it prints a hashcode and the type of the array, nothing useful for you).
What you want is something like :
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Or better yet as #azurefrog said :
System.out.println(java.util.Arrays.toString(arr));
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 8 years ago.
I'm trying to create a single array that contains the numbers 1-9 and the characters A-F. The array should look like: 1 2 3 4 5 6 7 8 9 A B C D E F. I don't know how to set the array up and would appreciate any advice.
Try this following piece of code,This will work for you,However you must keep in mind that An array can contain only a single type of value as said by #MadProgrammer in comments
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char[] arr = new char[16];
/*taking input from the keyboard*/
for(int i = 0; i < 16; i++)
{
arr[i] = sc.next().charAt(0);
}
/*displaying the contents of the array*/
for(int i = 0; i < 16; i++)
{
System.out.println(arr[i] + ",");
}
}
}
In this code the numbers 0-9,which you are taking as input are still inserted as characters.