Why does this simple code throw ArrayIndexOutOfBoundsException? - java

This is just a dummy code.
I fail to understand what is wrong as I am new to JAVA.
I have already referred:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
The answers there only pertain to using wrong length indices.
Code:
class abc{
static int n;
static int[] arr=new int[n];
static void print_arr(){
for(int x: arr) System.out.print(x+" ");
}
}
class Main {
public static void main(String[] args) {
abc.n=5;
for(int i=0;i<abc.n;i++){
abc.arr[i]=10;
}
abc.print_arr();
}
}
I want this code to print 10 five times.

One possible way.
class abc{
static int n;
static int[] arr;
static void init(int size) {
arr=new int[size];
}
static void print_arr(){
for(int x: arr) System.out.print(x+" ");
} }
class Main {
public static void main(String[] args) {
abc.n=5;
init(abc.n);
for(int i=0;i<abc.n;i++){
abc.arr[i]=10;
}
abc.print_arr();
} }
Better way
class ABC{
private int size;
private int[] arr;
ABC(int n) {
size = n;
arr = new int[n];
}
public void print_arr(){
for(int x: arr)
System.out.print(x+" ");
}
public int getSize() {
return size;
}
public int[] getArray() {
return java.util.Arrays.copyOf(arr,arr.length);
}
public void setArray(int [] array) {
arr = array.clone();
} }
class Main {
public static void main(String[] args) {
int size = 5;
ABC abc = new ABC(size);
int [] array = new int[size];
for(int i=0;i<abc.getSize();i++){
array[i]=10;
}
abc.setArray(array);
abc.print_arr();
} }

class abc
{
static int n=5;
static int[] arr=new int[n];
static void print_arr()
{
for(int x: arr) System.out.print(x+" ");
}
}
class Main
{
public static void main(String[] args)
{
for(int i=0;i<abc.n;i++)
{
abc.arr[i]=10;
}
abc.print_arr();
}
}
In your case ArrayIndexOutOfBounds exception occurs because you are trying to initialize the array by a variable which has not been initialized yet. So either initialize n with a value before array initialization or use dynamic sized array.

Related

Print Random Generated Sudoku Java

I am having troubles printing the generated Sudoku on this program. Every time I try to print it, it says "null". Originally the code doesn't have the main method so I added one.
class GenerateSudoku{
public static int[][][] sudoku;
public static Random rand=new Random();
GenerateSudoku(int[][][] sudoku){
this.sudoku=sudoku;
for(int ctr=0; ctr<sudoku.length; ctr++){
for(int ct=ctr; ct<sudoku.length; ct++){
double first=rand.nextDouble(), second=rand.nextDouble();
if(first>1-second){
this.sudoku[ct][ctr][0]=0;
this.sudoku[ct][ctr][1]=1;
}
else{
this.sudoku[ct][ctr][1]=0;
}
if(ct!=ctr && first>1-second){
this.sudoku[ctr][ct][0]=0;
this.sudoku[ctr][ct][1]=1;
}
else if(ct!=ctr){
this.sudoku[ctr][ct][1]=0;
}
}
}
}
public int[][][] getSudoku(){
return sudoku;
}
private void sop(Object obj){
System.out.println(obj+"");
}
public static void main(String[] args) {
System.out.println(sudoku);
}
}
As discussed in the comments, you should create an instance of the GenerateSudoku class and get the resulting sudoku from getSudoku() method:
import java.util.Random;
class GenerateSudoku{
public static int[][][] sudoku;
public static Random rand=new Random();
GenerateSudoku(int[][][] sudoku){
this.sudoku=sudoku;
for(int ctr=0; ctr<sudoku.length; ctr++){
for(int ct=ctr; ct<sudoku.length; ct++){
double first=rand.nextDouble(), second=rand.nextDouble();
if(first>1-second){
this.sudoku[ct][ctr][0]=0;
this.sudoku[ct][ctr][1]=1;
}
else{
this.sudoku[ct][ctr][1]=0;
}
if(ct!=ctr && first>1-second){
this.sudoku[ctr][ct][0]=0;
this.sudoku[ctr][ct][1]=1;
}
else if(ct!=ctr){
this.sudoku[ctr][ct][1]=0;
}
}
}
}
public int[][][] getSudoku(){
return sudoku;
}
private void sop(Object obj){
System.out.println(obj+"");
}
public static void main(String[] args) {
int[][][] sudoku = new int[9][9][2];
GenerateSudoku generator = new GenerateSudoku(sudoku);
sudoku = generator.getSudoku();
for(int i = 0 ; i < sudoku.length; i++) {
for(int j = 0 ; j < sudoku.length; j++) {
System.out.print(sudoku[i][j][1]);
}
System.out.println();
}
}
}
Generates:
110110011
100010111
001111100
101110010
111101111
001011100
011011000
110110000
110010001

Write out the table in other method

Hey i want to write out table in second method.
In first i changed int x into table(each digit in other array index) and in second method i want to write out the table. How to do it ?
int tab [] =new int [4];
public int[] change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());
return tab;
}
public void writeout(int a[]) {
this.tab=a;//how to connect tab from change() with int a[]
for( int i=0;i<=3;i++) {
System.out.println(a[i]);
}
You can use newest Java 8 API to print your array. Your code may look like this:
import java.util.Arrays;
public class App {
int tab[] = new int[4];
public int[] change(int x) {
tab[0] = x/1000;
tab[1] = (x/100)%10;
tab[2] = (x/10)%10;
tab[3] = x%10;
return tab;
}
public void writeout(int array[]) {
Arrays.stream(array).forEach(System.out::println);
}
public static void main(String[] args) {
App app = new App();
app.writeout(app.change(2));
}
}
I fiddled with it. It appears to work after adding a closing brace at the end. tab.toString() doesn't result in sensible results though.
public class MyClass {
public static void main(String args[]) {
MyClass c = new MyClass();
c.writeout(c.change(3));
}
public int[] change(int x) {
int tab [] =new int [4];
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());
return tab;
}
public void writeout(int a[]) {
for( int i=0;i<=3;++i) {
System.out.println(a[i]);
}
}
}
If you want to use class fields, then you could do it like this:
public class MyClass {
public static void main(String args[]) {
MyClass c = new MyClass();
c.change(3);
c.writeout();
}
private int tab [] = new int [4];
public void change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
}
public void writeout() {
for( int i=0;i<=3;i++) {
System.out.println(tab[i]);
}
}
}

I get errors, any suggestions?

When compiling this java program, I get errors like cannot find symbol... any suggestions?
import java.io.*;
import java.util.Scanner;
public class joel001
{
public int d;
// find the smallest number of an array
public static int small(int a[])
{
int smallest=0;
for(int i=0;i<a.length();i++)
{
if(a[i]<smallest)
{
smallest=a[i];
}
}
return smallest;
}
// subtract the smallest number of an array from all its elements
public static int[] sub(int a[],int d)
{
this.d=d;
for(int i=0;i<a.length();i++)
{
a[i]=a[i]-d;
}
return a;
}
// count the array's non zero elements
public static int count(int a[])
{
int countn=0;
for(int i=0;i<a.length();i++)
{
if(a[i]!=0)
{
countn=countn+1;
}
}
return countn;
}
public static void main(String args[])
{
int b,c,z,k=1;
int a[]=new int[1000];
Scanner s=new Scanner(System.in);
b=s.nextInt(); //input
for(i=0;i<b;i++)
{
a[i]=s.nextInt();
}
while(k==1)
{
z=count(a);
if(z==0)
{
break;
}
System.out.println(z);
c=small(a);
a=sub(a,c);
}
}
}
For a start, the length of an array is arr.length, not arr.length().
Secondly, in sub(), there is no this because it's a static function.
Thirdly, in main(), you need to declare i before trying to use it.
That will take care of all your compile-time errors. Run-time, or logic, errors are something you need to learn to fix in a debugger.
You are declaring all your methods of joel001 as static. Static methods are not related to an instance or object of the joel001 class. You need to research OOP before you continue.
Try something like this:
import java.io.*;
import java.util.Scanner;
public class joel001
{
public int d;
// find the smallest number of an array
public int small(int a[])
{
int smallest=0;
for(int i=0;i<a.length();i++)
{
if(a[i]<smallest)
{
smallest=a[i];
}
}
return smallest;
}
// subtract the smallest number of an array from all its elements
public int[] sub(int a[],int d)
{
this.d=d;
for(int i=0;i<a.length();i++)
{
a[i]=a[i]-d;
}
return a;
}
// count the array's non zero elements
public int count(int a[])
{
int countn=0;
for(int i=0;i<a.length();i++)
{
if(a[i]!=0)
{
countn=countn+1;
}
}
return countn;
}
public void main2() {
int b,c,z,k=1;
int a[]=new int[1000];
Scanner s=new Scanner(System.in);
b=s.nextInt(); //input
for(int i=0;i<b;i++)
{
a[i]=s.nextInt();
}
while(k==1)
{
z=count(a);
if(z==0)
{
break;
}
System.out.println(z);
c=small(a);
a=sub(a,c);
}
}
public static void main(String args[])
{
joel001 obj = new joel001();
obj.main2();
}
}

Java Grid fit problems in input

I ran my code... there was an error on line 28. it's the one that says
hey.remove(whatnumber);
I can't figure out what's wrong with it. I tried using debug, but
I don't get how to use it.
Here is the code.
import java.util.ArrayList;
import java.util.Scanner;
public class ACSL_Grid_Fit {
public static void main(String args[]) {
run();
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
}
public static ArrayList<Integer> hey = new ArrayList<Integer>();
public static int howmany;
public static int whatnumber;
public static int choice;
public static Scanner sc = new Scanner(System.in);
public static void run() {
input();
countcalc();
}
public static void input() {
{
sc.useDelimiter(", |\n");
howmany= sc.nextInt();
for(int x = 1; x<=howmany;x++) {
whatnumber = sc.nextInt();
hey.remove(whatnumber);
}
}
choice = sc.nextInt();
switch(choice) {
case 1:
int i = 0;
while(i<hey.get(0)) {
i++;
}
System.out.println(i);
hey.remove(i);
case 2:
case 3:
}
}
public static void countcalc() {
}
}
To avoid an IndexOutOfBoundsException, you need to add values to list before you try and run()
And with this code here, you have to keep in mind there is no index 0
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
You are starting the index at 1, so 0 is null;
Edit: I just caught this
You're trying to run() before you even have any values in your list
public static void main(String args[]) {
run();
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
}
Just switch them around
public static void main(String args[]) {
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
run();
}
EDIT: I knew this was a problem to begin with
hey.add(w,w);
Your trying to add at index w when no index w exists. hey has a size of 0, until you add to it.
Just do this.
for(int w=1;w<=25;w++) {
hey.add(w);
}
When you want to remove the numbers use this
hey.remove(whatnumber - 1)
// example, since 20 is at index 19, you want to remove whatnumber - 1

Wondering how to direct out into array then output from array

I am having to write a program that loads the first 10 integers' into an array. I've tried it every way and have not been able to make it work with an array. This is as far as I have gotten.
public class midtermcube2 {
public static void main(String[] args) {
int totz;
for (int numz=0; numz<=9;numz++)
{
totz=numz*numz*numz;
System.out.println(totz);
}
}
}
try this:
public class AddArray
{
public static void main(String[] args)
{
int arr[]=new int[10];
for(int i=0;i<10;i++)
{
arr[i]=i;
System.out.println(arr[i]);
}
}
}

Categories