What does" .class expected error " mean in java [closed] - java

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 5 years ago.
Improve this question
Here is my code. I got an error as ".class expected".
What should I do to rectify it.
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50))
double cur = acnt_balc - (withdraw + 0.50);
System.out.println(cur);
else
System.out.println(acnt_balc);
}
}

Try curly braces in the context of your if-else.
It should look like this:
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
So you can see, that there is a if-block and a else-block. You have to say which code belongs to if and which belongs to else. You can do that with the braces.

Scanner requires you to import java.util.*;
I tried it works and also format properly , see below , it runs
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >=(withdraw + 0.50)))
{
double cur=(acnt_balc-(withdraw + 0.50));
System.out.println(cur);
}
else
{
System.out.println(acnt_balc);
}
}
}
Also make sure your Filename is the same as class name , in this case Atm2.java
The Scanner Class in present in the 'java.util' package , since you forgot to import it and used Scanner sc=new Scanner(); the compliler is complaining, and remember to compile use javac filename.java , to run use java filename

Related

error cannot convert int to boolean in the if else if statement in java [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 9 months ago.
Improve this question
package Fare;
import java.util.Scanner;
public class fare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" Enter Class ");
System.out.println(" a. Senior b. Student c. Regular ");
System.out.println("Enter class : ");
int num = input.nextInt();
if (num = 1) {
double fare = 9 * 0.10;
System.out.println("Your fare will be " + fare + "ā–’. Thank you.");
}
else if (num = 2 ) {
double fare = 9 * 0.08;
System.out.println("Your fare will be " + fare + "ā–’. Thank you.");
}
else {
System.out.println("your fare will be 9ā–’. Thank you.");
}
}
}
Hi i can't seem to understand why there is an error in my if statement stating that it cannot convert int to boolean. I have already checked it and still can find any problem or mybe i just don't know.
You should change if(num=1) to if(num==1) and if else(num=2) to if else(num==2).

Guessing Game for Java (Print Different Statement Loop) [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 5 years ago.
Improve this question
I am not sure how to exactly ask my question, but I wanted to print the statement "your first/second/third try" every time the user guesses ( I think what I commented in my code will seem clearer than what I am trying to convey now) but I'm confused on how to do so. also, sorry if my code is messy, I'm a newbie at this lol.
import java.util.Scanner;
import java.util.Random;
import java.lang.System;
public class GuessingGame {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random randGen = new Random();
int userGuess = 0;
int userTries = 0;
int userCompRandom = randGen.nextInt(11)+1;
String userName = "";
boolean isWrong = true;
string guessNumberTag = "";
System.out.println("Welcome to the number guessing game. What's
your name?");
userName = scnr.nextLine();
System.out.println("Iā€™m thinking of an integer between 1 and 10.
You have 3 guesses.");
while (isWrong = true){
userTries +=1;
userGuess = scnr.nextInt();
if (userGuess > userCompRandom){
System.out.println(userGuess);
System.out.println("Too high, guess lower!");
}
else if (userGuess < userCompRandom){
System.out.println(userGuess); **//First/second/third
try: userGuess//**
System.out.println("Too low, guess higher!");
}
else if (userGuess == userCompRandom){
System.out.println(userGuess);//First/second/third try: userGuess//
System.out.println("Congratulations " + userName + "! It took you " + userTries + "!");
break;
}
if (userTries>4){
System.out.println("Game over " +userName + ",you lose!:p");
break;
}
}
}
}
String[] guessStatement = {"Your first try.", "Your second try.", "Your third try."};
System.out.println(guessStatement[userTries]);
Array index starts at 0, so adjust userTries.
You could create a method to do this. Note your check is >4 so I added fourth.
private static String getPrompt(int tries, int guess) {
String[] tryWords = { "first", "second", "third", "fourth"} ;
return String.format("Your %s try: %d", tryWords[tries-1], guess );
}
and call it:
System.out.println(getPrompt(userTries, userGuess )); //**First/second/third try: userGuess//**

Java using else if to assign a value [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 6 years ago.
Improve this question
I'm very new to programming and Java. I'm trying to use else if to assign a value to a variable if they selected the correct input. But whenever I try to compile it, it says it cannot find the variable.
import java.util.Scanner;
public class TDEE
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double tdee = 0.0;
System.out.print("Please enter your name: ");
String name = in.next();
System.out.print("Please enter your BMR: ");
double bmr = in.nextDouble();
System.out.print("Please enter your Gender (M/F): ");
String gender = in.next();
System.out.println();
// providing menu items
System.out.println("Select your activity level: ");
System.out.println("[0] Resting (Sleeping. Reclining)");
System.out.println("[1] Sedentary (Minimal Movement)");
System.out.println("[2] Light (Sitting, Standing)");
System.out.println("[3] Moderate (Light Manual Labor, Dancing, Riding Bike)");
System.out.println("[4] Very Active (Team Sports, Hard Manual Labor)");
System.out.println("[5] Extremely Active (Full-time Athlete, Heavy Manual Labor)");
System.out.println();
// accept user choice with a Scanner class method
System.out.print("Enter the number corresponding to your activty level(0,1,2,3,4, or 5): ");
String choice = in.next();
System.out.println();
if(choice.equalsIgnoreCase("0"))
{
double activityFactor = 1.0;
}
else if(choice.equalsIgnoreCase("1"))
{
double activityFactor = 1.3;
}
else if(choice.equalsIgnoreCase("2") && "M".equals(gender))
{
double activityFactor = 1.6;
}
else if(choice.equalsIgnoreCase("2") && "F".equals(gender))
{
double activityFactor = 1.5;
}
else if(choice.equalsIgnoreCase("3") && "M".equals(gender))
{
double activityFactor = 1.7;
}
else if(choice.equalsIgnoreCase("3") && "F".equals(gender))
{
double activityFactor = 1.6;
}
else if(choice.equalsIgnoreCase("4") && "M".equals(gender))
{
double activityFactor = 2.1;
}
else if(choice.equalsIgnoreCase("4") && "F".equals(gender))
{
double activityFactor = 1.9;
}
else if(choice.equalsIgnoreCase("5") && "M".equals(gender))
{
double activityFactor = 2.4;
}
else if(choice.equalsIgnoreCase("5") && "F".equals(gender))
{
double activityFactor = 2.2;
}
else
{
System.out.println("You did not choose a valid manu option.");
}
tdee = bmr * activityFactor;
System.out.println();
System.out.println("Name: " + name + " Gender: " + gender);
System.out.println("BMR: " + bmr + " Activity Factor: " + activityFactor);
System.out.println("TDEE: " + tdee);
}
}
The reason it is not compiling is because of scoping. The variable you are declaring is inside an if statement. That gives its scope, ability to access it, to only within the if statement. In order to reference it outside the if statement, you must declare it within the scope you want to access it. Then you can assign values to it like as follows, activityFactor = 1.0;, within the if else statements. Where you declare your variables determines what can access them.
You have to declare double activityFactor outside of the if else statements. Just include double activityFactor; above the statements and replace all double activityFactor with activityFactor, and it should compile.

Don't know how to display only one syso if i have more "if" who are satisfied [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 7 years ago.
Improve this question
How i make this simple program to show me only one answer, if multiple conditions are satisfied like: if age<16 syso("you can't rent cars") and if age <18 syso("You can't vote").
For example, if i introduce 17 i want to display only ("You can't vote") not ("You can't vote" and "You can't rent cars").
I tried to use 2 conditions sticked by "&&", it didn't work.
Code from comments
import java.util.Scanner;
public class Assign1 {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int age;
System.out.println("Insert age ");
age = x.nextInt();
if (age<16) {
System.out.println("You can't drive");
}
if (age<18 && age<16) {
System.out.println("You can't vote");
}
if (age<25) {
System.out.println("You can't rent cars");
}
if (age>25) {
System.out.println("You can do anything ");
}
}
}
This do what you want:
public class test {
public static void main(String[] args){
int age = 17;
if(age < 16){
System.out.println("You can't drive");
}
else if(age < 18){
System.out.println("You can't vote");
}
else if(age < 25){
System.out.println("You can't rent cars");
}
else{
System.out.println("You can do anything");
}
}
}
output:
You can't vote
The program print only the first if that is satisfied.
But i suggest you to study the conditional operators and logic. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

When I compile my code it says error '{' expected in the 4th line where I have my class, but I have a '{' there [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 8 years ago.
Improve this question
I am using java and having problem with my code. So for some reason when I compile my project on the 4th line it says error: '{' expected. resource says proj 2.java. Any help would be appreciated! thanks.
import java.io.*;
import java.util.*;
class proj 2 {
public static void main(String[] args)throws IOException {
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
int a=0, b=0, c=0, d=0, f=0, totalNumber=0;
stars();
System.out.println("Enter a list of exam scores from 0-100 ");
System.out.println("use a negative one if you're done with your list:");
stars();
for(int grade=0;grade>=0;totalNumber++)
{
try
{
grade=Interger.parseInt(dataIn.readLine());
if(grade>=90 && grade<=100)
a++;
else if (Grade>=80 && grade<=89)
b++;
else if (grade>=70 && grade>=79)
c++;
else if (grade>=60 && grade<=69)
d++;
else if (grade>=0 && grade<=59)
f++;
else if (grade>100)
{
System.out.println("Invalid Grade");
totalNumber--;
}
}
catch(NumberFormatException e)
{
System.out.println("Invalid input. Please enter your score in number form.");
totalNumber--;
continue;
}
}
System.out.println("");
System.out.println("Total number of grades: " +(totalNumber-1));
System.out.println("A grades: " + a);
System.out.println("B grades: " + b);
System.out.println("C grades: " + c);
System.out.println("D grades: " + d);
System.out.println("F grades: " + f);
}
public static void stars()
{
System.out.println("**********************************************************************");
}
}
Change
class proj 2 {
to
class proj2 {
A class name can't have spaces.
Also change Interger to Integer and Grade to grade.

Categories

Resources