Where are the three run time errors? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is the code, where are the run time errors? I tried and tried to find it but I just could not, does anyone else see where these errors could be within the code?
public class HasErrors
{
public static void main(String [] args)
{
int x = 0;
int y = 0;
Scanner in = new Scanner("System.in");
x = in.readInt();
System.out.print("Please enter another integer: ");
x = in.readInt();
System.out.println("The sum is " + x + y);
}
}

If you're instantiating a new Scanner with System.in it shouldn't have quotes around it:
Scanner in = new Scanner(System.in);
See here.

Well if a user enters in somethign other than a number, say Hello World, then readInt() will throw an InputMismatchException. Also the Scanner class does not have a readInt function. I think you meant to put nextInt.

Along with the readInt errors already mentioned, X and Y will be concatenated in the println statement rather than added. This could be fixed by putting the x + y in parentheses.

Related

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

Homework problem on Java: I'm trying to run my program, it shows no error however when I try to run it it doesn't work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My homework is to create a program that tells how many calories you consumed depending on the numbers of cookies you ate. But when I run the program, it doesn't show anything and I need it to work for my homework.
Here's the code for more context:
package cookie.calories;
import java.util.Scanner;
public class CookieCalories {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
int TotalNumberOfCookiesPerBag = 40;
int CaloriesPerServing = 300;
int ServingsPerBag = 10;
//The next equation represent the number of cookies per servings
int NumberOfCookiesPerServings = TotalNumberOfCookiesPerBag/ServingsPerBag;
//number of calories per cookies
int CaloriesPerCookies =CaloriesPerServing/NumberOfCookiesPerServings;
Scanner ob2 = new Scanner(System.in);
System.out.println("This programm can determined how many calories you ate depending on the number of cookie you ate.");
//Below this sentence, the amount of calories will be determined
System.out.println("Enter the number of cookies you ate");
int Cookies = ob.nextInt(4);
int TotalCaloriesConsumed = CaloriesPerCookies*Cookies;
System.out.println(TotalCaloriesConsumed + "for the amount of cookies you ate");
}
}
The program shows me that I didn't made any error in the way I wrote my program but when I play it, it doesn't show anything in the output. Why?
When I run your program I get this output
This programm can determined how many calories you ate depending on the number of
cookie you ate.
Enter the number of cookies you ate
when I enter (for example) 4 and press enter I get this error
Exception in thread "main" java.util.InputMismatchException: For input string: "4"
under radix 4
at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
at testasdf.Testasdf.main(Testasdf.java:38)
Not really sure why you have "ob.nextInt(4);" instead of "ob.nextInt();" should change your code to be nextInt();
So I am unsure of why nothing is showing up for you, are you sure you have a output screen set up?
What IDE are you using?
Remove the '4' from the nextInt method params.
int Cookies = ob.nextInt();
Also you don't need another Scanner object, you can use the same one to get input from the user multiple times, so you can remove this from your code.
Scanner ob2 = new Scanner(System.in);

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

Calculator in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Lately I've worked on simple calculator that can add, substract, multiply and divide.
public static void main(String arr[]){
double num1, num2;
String ans = null;
System.out.println("Calculator manu:");
System.out.println("\n"+"a for adding");
System.out.println("b for substracting");
System.out.println("c for multiplying");
System.out.println("d for dividing");
Scanner NumInput = new Scanner(System.in);
System.out.println("\n"+"Enter number one: ");
num1 = NumInput.nextDouble();
System.out.println("\n"+"Enter number two: ");
num2 = NumInput.nextDouble();
while(true)
{
Scanner AnsInput = new Scanner(System.in);
System.out.println("\n"+"Choose operation.");
String answer = AnsInput.next();
if(AnsInput.equals("1"))
{
answer = Double.toString(num1+num2);
System.out.println("\n"+"The sum of the first and second number is: "+answer);
}
else if(AnsInput.equals("2"))
{
answer = Double.toString(num1-num2);
System.out.println("\n"+"Subtraction of the first and the second number is: "+answer);
}
else if(AnsInput.equals("3"))
{
answer = Double.toString(num1*num2);
System.out.println("\n"+"The product of the first and second number is: "+answer);
}
else if(AnsInput.equals("4"))
{
answer = Double.toString(num1/num2);
System.out.println("\n"+"Ratio of the first and the second number is: "+answer);
}
}
}
But what if I want to wright program that works like an ordinary calculator; it adds, subtracts, multiplies, divides, ... ,but not only whit two but multiple numbers.
This can certainly be done! Although it requires substantial framework. However, if you want to use this type of simplistic code, what you need to do is store the numbers somewhere else, outside this function, and every two numbers you apply an operation to, you store it and use it as the first argument along the third you wanted to use. And so on. Does this make sense?

Compilation failures in program? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I run it in JCreater but I keep getting compile errors. What did I do wrong? I saw someone set all the values to 0.0, but I don't see why I would need that.
import java.util.Scanner;
public class C2p3 {
public static void main(String[] args) {
double num1, num2, sum, difference, product; //setting variables
Scanner scan = new Scanner(System.in);
System.out.println("Enter first number:");
num1 = scan.nextDouble(); // not really sure what it does but I always use it and it works fine
System.out.println("Enter second number:");
num2 = scan.nextDouble();
sum = num1 + num2;
product = num1 * num2;
difference = num1 - num2;
System.out.println("Sum = " + sum);
System.out.println("Product = " + product);
System.out.println("Difference =" + difference);
}
}
Look at the Import statement carefully.
The statement
import java.util*;
should be
import java.util.*;
Extra info
If you really want to start writing good programs. I would suggest you make following changes to your code.
the statement import java.util.*; means that you are importing all the classes that are there inside the util package, But you are not using all of them in your code but the Scanner Class. So, I would suggest that replace the current import statement with import java.util.Scanner;.
Whenever you create an object of Scanner class. Call the method scan.close() when you dont need the object anymore. Else this might lead to resource leakage.
Please start using a proper IDE like eclipse, netbeans etc

Categories

Resources