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;
}
...
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 2 years ago.
Improve this question
This is the function I set:
public int printABCNum(p) {
char textinfo = 65;
String textValue = "";
while (textinfo < 91) {
textValue+=textinfo;
textinfo++;
}
System.out.println(textValue + Integer.toString(p));
Eclipse (a text editor) says that there is a problem with public int printABCNum(p) bit. Basically I'm trying to do is run a function printing "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and an integer. I'm new to Java and is not good 'enough' at programming with it and finding bugs, so please help!
A p parameter of your printABCNum function is missing a type. Based on context it should be an int. Another problem is your function return type is defined to int, but you haven't returned any value.
Your code should be:
public void printABCNum(int p) {
char textinfo = 65;
String textValue = "";
while (textinfo < 91) {
textValue += textinfo;
textinfo++;
}
System.out.println(textValue + p);
}
It looks like there's a few things wrong here.
You shouldn't have a return type int if you are not returning anything.
The parameter needs to have a type, as such: int p
Other than that looks fine. Take a look below at full answer that worked for me.
public void printABCNum(int p) {
char textinfo = 65;
String textValue = "";
while (textinfo < 91) {
textValue+=textinfo;
textinfo++;
}
System.out.println(textValue + Integer.toString(p));
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I tried to write an primenumber generator. The method calcall() should return prime numbers (2,3,5,7...). Unfortunately I get the error, that the method doesn't returns an integer, wich I don't understand. Here is my code:
package primenumber;
public class primecalc {
public static int calcall(int a) { //actual generator
int konstante = a; //is this number a prime num?
int divisor = a-1; //divisor
int var1 = 0; //variable = 0
while(divisor>1) {
int quotient = konstante%divisor; //calc modulo
if(quotient == 0) { //if modulo==0 switch var1 to
var1++; //1 -> no primenumber
break; //stop calculating
} else { //else keep calculculating
divisor--; //until divisor <= 1
}
}
if(var1==0) { //if var1 still 0;
return konstante; //is a primnumber ->
} //return konstante
}
public static void main(String[] args) { //main function
int number = 3; //start with 3
while(True) { //(i'll add 2 manually)
System.out.println(calcall(number)); //print the prime number
number++; //increase number by one
}
}
}
The error is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at primenumber/primenumber.primecalc.calcall(primecalc.java:5)
at primenumber/primenumber.primecalc.main(primecalc.java:28)
What is wrong?
The gray lines on the code you posted are being ignored by the compiler.
The use of /* and */ makes everything between these seen by the compiler as comments. And that is why those lines are grayed out. If you want to comment on the same line as the code, I'd advise you to use //.
Also, it is common practise to use multi-line comments only to describe functions and place them just above the header of the function. Any other comments should be short, concise and describe functionality. Good variable names and well written code should do most of the explaining, and single line comments should be used when it's a bit harder toperceive what's going on.
Cheers
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;
}
}
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);
}
}
}