Im getting RuntimeException ,how can i fix it? [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 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.

Related

"Int cannot be converted to java.util.Scanner" and " java.util.Scanner cannot be converted to boolean" [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 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)

Java If Statement Trouble [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 1 year ago.
Improve this question
I'm in college and am trying to learn Java but I'm having a bit of trouble with some code. The "if (totCreditsEarned >= 180)" is erroring in my Eclipse, specifically the part in parentheses, and it's unable to give any suggestions to fix it.
import java.util.Scanner;
public class CSC161lab6_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Credits Earned: ");
String totCreditsEarned = scanner.nextLine();
System.out.println("Credits Earned is: " + totCreditsEarned);
if (totCreditsEarned >= 180) {
System.out.println("Wow!");
} else {
System.out.println("Oof!");
}
}
}
180 is an Integer. totCreditsEarned is a String.
You will need to convert totCreditsEarned to an Integer
if (Integer.valueOf (totCreditsEarned) >= 180)
...
Of course if you enter a non-integer value it will fail
The full compilation error is.
CSC161lab6_1.java:13: error: bad operand types for binary operator '>='
if (totCreditsEarned >= 180) {
^
first type: String
second type: int
1 error
It's telling you that you can't compare an int and a String using >=. You need two ints, so totCreditsEarned must be an int or converted to an int before you can compare them.
You can read it from the Scanner as an int. So no need to convert after.
int totCreditsEarned = scanner.nextInt();

Number Format Exception occuring, try catch throwing exception? [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 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);

How can i get my PrimeNumber generator to work? [closed]

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

Read a string of two (distinct) numbers (floats). Output bigger number. Do not use “if” [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This question need to be done by using Exception Handling(try-catch).
I tried dividing the string into 4 different integer numbers. Two for Integer part, and other two for decimal part.
But I'm still stuck how can i generate an error with it.
For 2 Integer numbers my answer is,
import java.lang.*;
import java.util.*;
class s7
{
public static void main(String args[])
{
int a,b,c;
String str;
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
StringTokenizer st=new StringTokenizer(str," ");
a=Integer.parseInt(st.nextToken());
b=Integer.parseInt(st.nextToken());
c=a/b;
try
{
int d=20/c;
System.out.println("Bigger: "+a);
}
catch(Exception e)
{
System.out.println("Bigger: "+b);
}
}
}
Assuming that numbers can't be negative you can try dividing one by another like x/y.
if x<y you will get number lesser than 1 like 0.xxxxxx
if x>=y you will get number greater or equal to 1 like 1.xxxx or 2.xxxx and so on.
Now to get rid of fractional part all we need to do is cast such number to int (I assume numbers are not that big). This way for x=y` you will get bigger value.
So now problem is to test if (int)x/y is 0 or not.
To test it you can try use arrays and IndexOutOfBoundsException. Just create array which can contain one element new SomeType[1] and try to access this element using result something like array[(int)x/y]. If you will be able to do it then print y (because result was 0 so it is case where x<y), if you will catch IOB exception print x.

Categories

Resources