Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I can not figure out why my code won't let me remove a value from my list. I am suppose to make the list have 10 values in it, and then when I want to take a value out and replace it I use remove() and then add(). My error says this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at MyList.remove(MyList.java:36)
at MyListDriver.main(MyListDriver.java:17)
Here is my code for those sections:
public class MyListDriver{
public static void main(String[] args){
MyList alpha = new MyList(9);
alpha.add("22");
alpha.add("2");
alpha.add("1");
alpha.add("22");
alpha.add("13");
alpha.add("6");
alpha.add("7");
alpha.add("32");
alpha.add("172");
alpha.add("2");
alpha.remove("12");
alpha.add("23");
alpha.print();
}
}
And the section from the MyList that is the remove(), which is where I am getting an error.
public void remove(String data){
int index = 0;
for(int r = 0; r <=arr.length; r++)
{
if(arr[r].equals(data)){
index=r;
break;
}
}
remove(index);
}
public void remove(int index){
if(index>=0 && index<loc){
for(int i=index; i<loc-1; i++){
arr[index]=arr[index+1];
}
loc--;
}
}
I am suppose to have an overloaded remove method. I dont know why I am getting an Array out of bounds error.
Thank you for any help in advance.
I know U got The Answer
for(int r = 0; r <=arr.length; r++)
{
if(arr[r].equals(data)){
index=r;
break;
}
}
why u getting ArrayIndexOutOfBoundsException
because of when r=arr.lenght for Ex. array lenght is 3 means 0 1 2
when r=3 the condition retunrn true because 3<=3 so change it
for(int r = 0; r < arr.length; r++)
{
if(arr[r].equals(data)){
index=r;
break;
}
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The below code is not giving any error. After debugging i found it is not going inside the first loop.
package string_pattern;
public class Naive {
public static void main(String[] args)
{
String pattern="This is a text book";
String text="text";
child obj=new child();
obj.search(pattern, text);
}
}
class child{
public void search(String pat,String text)
{
int m,n;
m=pat.length();
n=text.length();
System.out.println("i am outside loop");
for(int i=0; i<= (n-m); i++)
{
int j;
System.out.println("i am inside first loop");
for(j=0;j<m;j++)
{
if(text.charAt(i+j) != pat.charAt(j))
{
System.out.println("OVER HERE");
break;
}
if(j==m)
System.out.println("pattern found at pos" + i);
}
}
}
}
You do the following:
m=pat.length();
n=text.length();
The problem is that text has a length of 4, while pat is longer. So when you do the loop like this -> for(int i=0; i<= (n-m); i++), you end up with a negative number. Since 0 is greater than any negative number you never enter the loop.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I just started learning java, and I created a support code (called HELP) to help me track some variables in another code im writing. But when I try to run HELP I get this exception in return, can someone help me?
Im using INTELIJ
public static void main(String [] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int T = Integer.parseInt(args[2]);
for (int i = 0; i < T; i++) {
//bob vĂȘ a carta
int see;
int unseen;
if (Math.random() > .5) {
see = a;
} else see = b;
System.out.println(see);
}
}
the output is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at HELP.main(HELP.java:4)
You aren't specifying any arguments when you run the program so args[0], args[1], args[2] isn't a valid index.
In one old post founded this
// to use 10 when there aren't args...
int trials = (args.length > 0) ? Integer.parseInt(args[0]) : 10;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I just tried coding my first sorting algorithm aand it does weird things. I dont actually know, where this comes from but i think it has to do something with array length. because the first numbers always equal array length. Here i put the simple code i wrote in hopes, that somebody helps me debug it. Thank you very much in advance!!
public class BubbleSort {
private static void sort(int[] pole) {
for (int i = 0; i < pole.length; i++) {
for (int j = 1; j < pole.length - i; j++) {
if (pole[j-1] > pole[j]) {
int tm = pole[j-1];
pole[j-1] = j;
pole[j] = tm;
}
}
}
}
public static void main(String[] args) {
int[] pole = { 65, 210, 41, 23, 3, 2, 4, 78 };
System.out.println("before: " + Arrays.toString(pole));
sort(pole);
System.out.println("after: " + Arrays.toString(pole));
}
}
You have an error in your if check. You're assigning the index of a value rather than the value itself - pole[j-1] = j;. So, you should correct it to:
...
if(pole[j-1] > pole[j]) {
tm = pole[j-1];
//assign the value here
pole[j-1] = pole[j];
pole[j] = tm;
}
...
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm just really confused on why there's an error at the for statement line. This is literally code from a tutorial that I can't run because there's an error at line 3
class RoundOne{
public static void main(String[] args){
for(int i =0,i<10,i++){
System.out.println("The number is: "+i);
}
}
}
for(int i =0;i<10;i++){
System.out.println("The number is: "+i);
}
You wrote , instead of ;
In your for loop instead of
for(int i =0,i<10,i++)
write
for(int i =0;i<10;i++)
For loops use ; between the parts, not ,
class RoundOne {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("The number is: " + i);
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to display multi arrays.
public class apples {
public static void main(String[] args){
int Tabela_Nr1[][] = {{8,9,10,11}, {12,13,14,15}};
int Tabela_Nr2[][] = {{30,31,32,33}, {43},{4,5,6,}};
System.out.println("To jest pierwsza tabela: ");
wyswietl(Tabela_Nr1);
System.out.println("To jest druga tabela: ");
wyswietl(Tabela_Nr2);
}
public static void wyswietl(int x [][]){
for (int row = 0; row < x.length; row++){
for(int counter = 0; counter < x[row].length; counter++);
System.out.print(x[row][counter] + "\t");
}
System.out.println();
}
}
Debugger tells me that variable counter is not declare, but it is in "for" loop...
What is the cuz of the problem ?
Console:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
counter cannot be resolved to a variable
at apples.wyswietl(apples.java:15)
at apples.main(apples.java:8)
The problem is the extra semi colon :
for (int row=0;row<x.length;row++){
for(int counter=0;counter<x[row].length;counter++); //<-- here
System.out.print(x[row][counter]+"\t");
This semi colon end the inner loop, which means the print statement is outside the loop, where counter is unknown.
I think that your System.out.println(); is also wrongly placed. You probably want to place it after each row :
public static void wyswietl(int x[][]){
for (int row=0;row<x.length;row++) {
for(int counter=0;counter<x[row].length;counter++) {
System.out.print(x[row][counter]+"\t");
}
System.out.println();
}
}
for(int counter=0;counter<x[row].length;counter++); --> remove the ; and it will work just fine :)
BTW you might also want to look into --> Arrays.deepToString() which displays multi-dimensional arrays.