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
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 19 days ago.
Improve this question
No idea how to fix this and I require expertise. (New to Java)
I'm just testing out different basic operations like if statements etc and already at a wall.
import java.util.Scanner;
public class yes {
public static void main(String[] args)
{
Scanner test = new Scanner(System.in);
System.out.println("What is 5+5? ");
int value = test.nextInt();
if (test = 10)
{
System.out.println("You are correct!");
}
else
{
System.out.println("Error!");
}
}
}
The problem is in the line
if (test = 10)
test is your Scanner instance. It's a bad name, so it's not too surprising that you missed that. Try to get into the habit of naming variables with meaningful names, even if they are long. It'll help you in the long run.
value is what you called the number that you get from your Scanner, so the first step is to fix the name in the if:
if (value = 10)
Once you've done that you'll still get an error, because = is an assignment, but you want to compare two values for equality, which is ==:
if (value == 10)
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 1 year ago.
Improve this question
Trying to find out if any of the digits in a number are odd, if so return true. If any are even then return false. Getting error: incompatible types: boolean cannot be converted to int. Any help is appreciated.
public class allDigitsOddTest{
public static void main(String[] args) {
allDigitsOdd(756410);
}
public static int allDigitsOdd(int num){
boolean value = true;
int evens = 0;
int odds = 0;
while (num > 0){
int remainder = num % 10;
if (remainder % 2 == 0){
evens++;
}
else{
odds++;
}
num = num / 10;
}
if (evens > 0){
value = false;
}
return value;
}
}
Your return type is int, instead of boolean
change
public static int allDigitsOdd(int num)
to
public static boolean allDigitsOdd(int num) {
In the beginning it helped me a lot to look at Methods like this:
You try to unlock your door at home, which you have the keys for. It doesn't matter which of your three keys you use, because it works with every one of your keys. But if you try your car key it won't fit and the mission is a failure.
So look at the different data types as keys, you can lock the door with your main key, and your girlfriend unlocks it later with hers.
-> One key in, another one of the same out if you understand what I mean.
Same for Java, you give an integer into the method, you can return every integer you want, but not a different data type (or key type).
Maybe an odd example, but for some reason it helped me a lot.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
If this function is running:
public static void maximizeweight(int[] weight, int[] A){
Random r = new Random();
int random = r.nextInt(A.length);
for(int i=0;i<A.length;i++){
while(totalweight(weight,A) < 630){
if(A[random]==0)
A[random] += 1;
}
}
}
The output some times breaks a bit or freezes eclipse, totally at random, and sometimes it is able to give the whole result, other times it doesn't show the last part of the desired result.
If your A[random] != 0 and still your totalweight() returns < 630, the while loop would be infinite. One possible fix (and the one I think you need) is to move your int random = r.nextInt(A.length); inside your while loop.
public static void maximizeweight(int[] weight, int[] A){
Random r = new Random();
for(int i=0;i<A.length;i++){
while(totalweight(weight,A) < 630){
int random = r.nextInt(A.length);
if(A[random]==0)
A[random] += 1;
}
}
}
Note: This would still loop infinitely if the sum of weight array is still < 630. So you will need additional checks.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
https://projecteuler.net/problem=4
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Here is my code and I find the answer as 580085 but it is not the correct answer:
public class asdas {
public static void main(String[] args) {
int a=100,b=100,answer=0;
while(a<=999)
{
b=100;
while(b<=999)
{
int product=a*b;
int reverse=0;
while(product>0)
{
int lastDigit=product%10;
reverse=(reverse*10)+lastDigit;
product=product/10;
}
product=a*b;
if(product==reverse)
{
answer=product;
}
b++;
}
a++;
}
System.out.println(answer);
}
}
The problem is here:
if(product==reverse)
{
answer=product;
}
You're assuming that if you find a palindrome, it's the largest. That's not true though.
For example, 101*100 is calculated after 100*102, even though the latter is larger. In fact, right after calculating 998*999 (997,002), you calculate 999*100 (99,900), which is nearly 10 times smaller!
You should add another check to make sure the new product is greater:
if(product==reverse && product>answer) {
answer = product;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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
Eclipse is giving me the error "The left-hand side must be a variable" at this part of my code:
else
{ for(int i=0; i>=cellphoneArr.length; i++)
{if (cell_1.equals2(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
else
(cell_1.equals3(cellphoneArr[i])); ---> this is the error
System.out.println(cellphoneArr[i]);
}
The method equals3 is the following:
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone));
}
I've been trying to figure this one out, but the way I invoked my two other methods equals 1 and 2 both worked with the object cell_1.
Try it as:
else
{ for(int i=0; i>=cellphoneArr.length; i++)
{if (cell_1.equals2(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
else if(cell_1.equals3(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
}
and the method equals3 must return a boolean values as:
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone))
return true;
else
return false;
}
Remove ; at the end of else, Just like you did for if.
else(cell_1.equals3(cellphoneArr[i]));
^
Return a Boolean value from you function, instead of if
public boolean equals3(Cellphone phone)
{
return (brand.equals(phone));
}
You need to add an if:
else if (cell_1.equals3(cellphoneArr[i]))
You need an if following the else
EDIT
The code should be
if (cell_1.equals2(cellphoneArr[i])) {
System.out.println(cellphoneArr[i]);
} else if (cell_1.equals3(cellphoneArr[i])) {
System.out.println(cellphoneArr[i]);
}
Formatting allows you to pin point these kind of errors easily. If you are using Eclipse, do ctrl + shift + f