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
I am having an issue with this part of the method. The program is supposed to pass arguments, which in my binaryToDecimal method it does just fine. But everytime this method, the decimalToBinary attempts to pass the value of 5 to binary, it tells me the Number Format Exception occurs and it doesn't do the calculation. Anything I could try to avoid this from happening?
public static String decimalToBinary(String decimalString) {
int decimal = 0;
try {
decimal = Integer.parseInt("decimalString");
}catch(NumberFormatException e){
System.out.println("Number format exception occured");
}
String answer = "";
while(decimal > 0) {
answer = decimal%2+answer;
}
return answer;
}
Your issue is in here decimal = Integer.parseInt("decimalString");
you should used your parameter decimalString
instead of this decimal = Integer.parseInt("decimalString");
try using this decimal = Integer.parseInt(decimalString);
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 3 years ago.
Improve this question
Why an int becomes zero when it exceed the int limit in java?
Eg:
import java.util.Scanner
public class MyClass{
public static void main(Stings [] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); // enter 50 or higher.
int answer = 1;
while(x>0){
answer = answer*x;
x--;
}
System.out.println(answer);
}
}
At some point answer becomes 0 (integer overflow) and stays 0.
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 8 months ago.
Improve this question
I'm trying to print an exception of division in zero but its not working :
this is my code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int d=0;
Scanner S=new Scanner(System.in);
System.out.println("Please Enter an integer :");
try{
d=S.nextInt();
System.out.println(1/d);
}
catch(ArithmeticException e){
System.out.print("%s",e);
}
}
}
Your code does not work for me, unless I change the print to printf:
System.out.printf("%s", e);
But as you can see in the Documentation of java.lang.ArithmeticException
[..] an integer "divide by zero" throws an instance of this class[..]
But the result of a division is in your case Integer, since you are dividing two integer values, but it would make more senes to receive a double or float value and in both cases that Exception is not thrown (result of the division is Double.POSITIVE_INFINITY).
Which means you have to add a check if your variable "d" equlas 0. If it is zero then handle it (e.g. print a message).
I just tested it - you need a printf("%s", e) statement instead of the current print statement you have. Then, if you enter 0 when you enter a number, it should point the division by zero error.
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
Why I try to use the loop to display the numbers I had put into the ArrayList, it says Unreachable code what am I doing wrong.
private void nOther1() {
ArrayList<Integer> multiples = new ArrayList<Integer>();
int n = 1;
while (1 <= 100) {
multiples.add(n);
n++;
}
// UNREACHABLE CODE
for (int num : multiples) {
System.out.println(num);
}
}
Because
while (1 <= 100)
will never terminate since 1 is always <= 100, and the compiler knows that.
See Chapter 14.21. Unreachable Statements for detailed explanation.
This condition while(1 <= 100){ is always returning true
it would be the same as you do while(true){ so all behind that point is code that NEVER will be executed..
I think you wanted trying instead:
while(n <= 100){
multiples.add(n);
n++;
}
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
What's wrong with my code?
import java.util.Scanner;
class Pali
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("\nInserisci una stringa: ");
String p = in.next();
int n = p.lenght();
}
}
I get a "cannot find symbol" error in p variable. Why? Many thanks (sorry,if I did something wrong, it's my first post).
It's a typo:
int n = p.lenght();
Should be p.length();
It is a typo.
int n = p.lenght();
Correction: int n = p.length();
It happens to the best of us.
Use: int n = p.length(); instead of p.lenght();