Problem of printing ABC... and a number in Java [closed] - java

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));
}

Related

Beginner question, incompatible types: boolean to int. Not seeing the problem [closed]

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.

Method to work out win/loss/tie and return points (football) [closed]

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 2 years ago.
Improve this question
static final int WIN = 3;
static final int TIE = 1;
static final int LOSS = 0;
public static int berekenWedstrijdPunten(int[] mpUserTeamPunten, int[] mpTegenstanderTeamPunten){
if (mpUserTeamPunten > mpTegenstanderTeamPunten){
return WIN;
}
if (mpUserTeamPunten == mpTegenstanderTeamPunten){
return TIE;
}
if (mpUserTeamPunten < mpTegenstanderTeamPunten) {
return LOSS;
}
}
getting error lines under the if statements with > & <, trying to get a return of the win/tie/loss point into a print statement.
You can't compare two arrays with logical operators.

Bubble sort sorts weirdly [closed]

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;
}
...

error: the left-hand side must be a variable [closed]

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

Parsing an array of integers into a single int with Java [closed]

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 8 years ago.
Improve this question
I am trying to make this work as a Python user forced to use Java:
private int parseInt(char[]){
private int parsed = 0
for (i=0 ; i < char.length(); i++;)
{parsed += (10**i) * char[i];}
}
I need to know why this method refuses to work and also how to fix it without using a built in function
First of all, change 10**i to Math.pow(10,i).
Then name your char[] array. Let's call it c.
Then, change c[i] to c[i]-'0', assuming c[i] is a digit.
private int parseInt(char[] c)
{
int parsed = 0;
for (i=0 ; i < c.length; i++)
parsed += Math.pow(10,i) * (c[i]-'0');
return parsed;
}

Categories

Resources