import java.io.*;
public class Main {
public static void main(String args[])throws Exception{
String MidtermLecGrade, MidtermLabGrade;
String FinalLecGrade, FinalLabGrade;
Float MG, temp;
Float FG;
Float Average;
Float SemG;
double a =0.6;
double b = 0.4;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your Midterm Lecture Grade:");
MidtermLecGrade=br.readLine();
System.out.print("Enter your Midterm Lab Grade:");
MidtermLabGrade=br.readLine();
temp= (a * MidtermLecGrade) + (b*MidtermLabGrade);
MG = Float.parseFloat(temp);
System.out.println("Your Midterm Grade is :" + MG);
}
}
error: bad operand types for binary operator '*'
temp= (a * MidtermLecGrade) + (b*MidtermLabGrade);
^
first type: double
second type: String
error: bad operand types for binary operator '*'
temp= (a * MidtermLecGrade) + (b*MidtermLabGrade);
^
first type: double
second type: String
error: incompatible types: Float cannot be converted to String
MG= Float.parseFloat(temp);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
You need to convert a and b variables to float.
Also you need parse the user input to float.
No need for other variables.
public static void main(String args[]) throws Exception {
String MidtermLecGrade, MidtermLabGrade;
Float temp;
float a = 0.6f;
float b = 0.4f;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your Midterm Lecture Grade:");
MidtermLecGrade = br.readLine();
System.out.print("Enter your Midterm Lab Grade:");
MidtermLabGrade = br.readLine();
temp = ((a * Float.parseFloat(MidtermLecGrade)) + (b * Float.parseFloat(MidtermLabGrade)));
System.out.println("Your Midterm Grade is :" + temp);
}
String MidtermLecGrade, MidtermLabGrade;
double a =0.6;
double b = 0.4;
//...
temp= (a * MidtermLecGrade) + (b*MidtermLabGrade);
a is a double
MidtermLecGrade is a String
double * String makes no sense, hence your error
At a guess, you will need to convert the String value to a Double. See Double.parseDouble(String)
Related
I have two java errors that I need help on solving, please help!!
Error 1: incomparable types: Scanner and String
Error 2: bad operand types for binary operator '+'
Here is my code:
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
x.nextInt();
Scanner y = new Scanner(System.in);
y.nextInt();
Scanner function = new Scanner(System.in);
function.next();
if (function == "add") {
int sum = x + y;
System.out.println(sum);
}
You can't use + for a non-primitive type (or String). In your case, you try to use it for a Scanner reference.
You probably meant:
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
String function = scanner.next();
if (function == "age") {
int sum = x + y;
System.out.println(sum);
}
You can try this:
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("List of functions: 'age'");
System.out.println("Please enter the function:");
String function = reader.next();
System.out.println("Enter the first age:");
int x = reader.nextInt();
System.out.println("Enter the second age:");
int y = reader.nextInt();
if (function.equalsIgnoreCase("age")) {
int sum = x + y;
System.out.println("Total is: " + sum);
}
}
This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 5 years ago.
New to java. Do not understand error. Basically trying to return value to then determine output but error "Cannot make static reference to non static field appears on line 13" in the class bosscalc. Return values from operators class.Please help. I have indicated line 13 in the class bosscalc. Thanks
package calculator;
import java.util.Scanner;
public class bosscalc {
Scanner input = new Scanner(System.in);
public static void main(String args[]) {
operators operatorobjects=new operators();
String answer;
System.out.println("What would you like to do? ");
answer =input.nextLine(); -------------------------LINE 13
if (answer=="a"){
double adding = operatorobjects.add();
}
if (answer=="s") {
double subtrat = operatorobjects.sub();
}
if (answer=="m") {
double multiply = operatorobjects.sub();
}
}
}
Class operators:
package calculator;
import java.util.Scanner;
public class operators {
double add() {
double n1,n2,a;
Scanner input=new Scanner(System.in);
System.out.print("Enter number 1 ");
n1=input.nextDouble();
System.out.print("Enter number 2 ");
n2=input.nextDouble();;
a=n1+ n2;
return a;
}
double sub() {
double n1,n2,d;
Scanner input=new Scanner(System.in);
System.out.print("Enter number 1 ");
n1=input.nextDouble();
System.out.print("Enter number 2 ");
n2=input.nextDouble();;
d=n1 - n2;
return d;
}
double m() {
double n1,n2,m;
Scanner input=new Scanner(System.in);
System.out.print("Enter number 1 ");
n1=input.nextDouble();
System.out.print("Enter number 2 ");
n2=input.nextDouble();;
m=n1/n2;
return m;
}
}
As the error message says: From a static context (your static main function) you cannot reference a non-static variable (input).
You can fix it by making input static, i. e. declare it as follows:
static Scanner input = new Scanner(System.in);
I have spent five minutes changing (refactoring) your code. There were a few simple errors. I have moved everything into a single class, and added some comments.
There are lots of improvements which can be made. But this is all down to practice and experience:
import java.util.Scanner;
public class Operators {
/**
* add numbers
* #return n1 + n2
*/
double add() {
double n1, n2, a;
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 ");
n1 = input.nextDouble();
System.out.print("Enter number 2 ");
n2 = input.nextDouble();
a = n1 + n2;
return a;
}
/**
* subtract numbers
* #return n1 - n2
*/
double sub() {
double n1, n2, d;
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 ");
n1 = input.nextDouble();
System.out.print("Enter number 2 ");
n2 = input.nextDouble();
d = n1 - n2;
return d;
}
/**
* multiply numbers
* #return n1 * n2
*/
double multiply() {
double n1, n2, m;
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 ");
n1 = input.nextDouble();
System.out.print("Enter number 2 ");
n2 = input.nextDouble();
m = n1 * n2;
return m;
}
/**
* divide numbers
* #return n1 / n2
*/
double divide() {
double n1, n2, m;
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 ");
n1 = input.nextDouble();
System.out.print("Enter number 2 ");
n2 = input.nextDouble();
m = n1 / n2;
return m;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Operators operatorobjects = new Operators();
String answer;
System.out.println("What would you like to do? ");
answer = input.nextLine();
/**
* String equality use String.equals()
*/
if (answer.equals("a")) {
double adding = operatorobjects.add();
/**
* Debug output println
*/
System.out.println("adding = " + adding);
} else if (answer.equals("s")) {
double subtract = operatorobjects.sub();
System.out.println("subtract = " + subtract);
} else if (answer.equals("m")) {
double multiply = operatorobjects.multiply();
System.out.println("multiply = " + multiply);
} else if (answer.equals("d")) {
double divide = operatorobjects.divide();
System.out.println("divide = " + divide);
}
/**
* More debug exiting
*/
System.out.println("exiting");
}
}
I have added a divide method, and renamed to multiply. The output from running is:
What would you like to do?
a
Enter number 1 10
Enter number 2 10
adding = 20.0
exiting
What would you like to do?
s
Enter number 1 10
Enter number 2 2
subtract = 8.0
exiting
What would you like to do?
m
Enter number 1 2
Enter number 2 5
multiply = 10.0
exiting
What would you like to do?
d
Enter number 1 6
Enter number 2 3
divide = 2.0
exiting
I'm starting on methods today & I ran into an error that one of my equations in the main method isn't applicable throughout the whole program. It's the fourth line of code & I thought it was typed correctly. Basically, when you input the length & width, you will get the area of a rectangle as the output. Here's the code:
double area = areaOfRectangle();
String YES = "Y";
String YES2 = "y";
String NO = "N";
String NO2 = "n";
boolean validInput = false;
System.out.print("Please enter a length: ");
float length = input.nextInt();
System.out.print("Please enter a width: ");
float width = input.nextInt();
System.out.printf("Area: %.2d %n", area);
do{
System.out.print("Enter more? (Y/N): ");
String input2 = input.next();
if(input.hasNextLine()){
if(input2.equals("Y") || input2.equals("y")){
System.out.print("Please enter a length: ");
length = input.nextFloat();
System.out.print("Please enter a width: ");
width = input.nextFloat();
System.out.printf("Area: %.2d %n", area);
}
else if(input2.equals("N") || input2.equals("n")){
System.exit(1);
}
}
}while(!validInput);
}
public static void areaOfRectangle(float length2, float width2){
length2 = length;
width2 = width;
double rectangle = (length2 * width2);
}
}
What am I doing wrong?
You have
double area = areaOfRectangle();
and
public static void areaOfRectangle(float length2, float width2){
So you have a method returning void (nothing), which compiles since you're not returning anything, but you're trying to assign that nothing to a double. Also, you're not passing the parameters at all. In java, you cannot assign a method to a variable as in a functional language.
You need:
public static double areaOfRectangle(float length2, float width2){
double rectangle = (double)length2 * width2;
return rectangle;
}
And then calculate area when you have the parameters:
double area = areaOfRectangle(length,width);
Maybe don't do conversions between float and double either, just use doubles throughout.
Ok,so I'm a complete novice at programming and I just started coding in Java. I tried to write a code for temperature conversion (Celsius to Fahrenheit) and for some reason it simply won't run! Please, help me find out errors in this code(however silly it may be).
Here's the code:
package tempConvert;
import java.util.Scanner;
public class StartCode {
Scanner in = new Scanner(System. in );
public double tempInFarenheit;
public double tempInCelcius;
{
System.out.println("enter the temp in celcius");
tempInCelcius = in .nextDouble();
tempInFarenheit = (9 / 5) * (tempInCelcius + 32);
System.out.println(tempInFarenheit);
}
}
You forgot to write the main method which is the start point for a program to run. Let me modify your code.
import java.util.Scanner;
public class StartCode
{
Scanner in = new Scanner (System.in);
public double tempInFarenheit;
public double tempInCelcius;
public static void main (String[] args)
{
System.out.println("enter the temp in celcius");
tempInCelcius = in.nextDouble() ;
tempInFarenheit = (9/5)*(tempInCelcius+32);
System.out.println(tempInFarenheit);
}
}
I think this is going to work better for you:
import java.util.Scanner;
public class StartCode
{
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
double tempInFarenheit;
double tempInCelcius;
System.out.println("enter the temp in celcius");
tempInCelcius = in.nextDouble() ;
tempInFarenheit = 1.8*tempInCelcius+32;
System.out.println(tempInFarenheit);
}
}
You equation for Farenheit was incorrect. Integer division isn't for you, either.
You need a main method. I also suggest using an IDE such as Eclipse, which can generate the skeleton code for you (including the syntax of the main method).
import java.util.*;
public class DegreeToFahrenheit {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a temperature: ");
double temperature = input.nextDouble();
System.out.println("Enter the letter of the temperature type. Ex: C or c for celsius, F or f for fahrenheit.: ");
String tempType = input.next();
String C = tempType;
String c = tempType;
String F = tempType;
String f = tempType;
double celsius = temperature;
double fahrenheit = temperature;
if(tempType.equals(C) || tempType.equals(c)) {
celsius = (5*(fahrenheit-32)/9);
System.out.print("The fahrenheit degree " + fahrenheit + " is " + celsius + " in celsius." );
}
else if(tempType.equals(F) || tempType.equals(f)) {
fahrenheit = (9*(celsius/5)+32);
System.out.print("The celsius degree " + celsius + " is " + fahrenheit + " in fahrenheit." );
}
else {
System.out.print("The temperature type is not recognized." );
}
}
}
I am new to java and i read a few chapters. Just can't figure out how to use another method in this program that converts temps from F to C and vice versa
Here is my code right now:
import java.io.*;
import javax.swing.JOptionPane;
public class Converter {
public static void main(String[] args) throws Exception{
String unit = JOptionPane.showInputDialog("Enter unit F or C: ");
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp1 = JOptionPane.showInputDialog("Enter the Temperature: ");
double temp = Double.valueOf(temp1).doubleValue();
if((unit.equals("F"))||(unit.equals("f"))){
double c= (temp - 32) / 1.8;
JOptionPane.showMessageDialog(null,c+" Celsius");
}
else if((unit.equals("C"))||(unit.equals("c"))){
double f=((9.0 / 5.0) * temp) + 32.0;
JOptionPane.showMessageDialog(null,f+" Fahrenheit");
}
}
}
You could create static methods to convert from on to another, e.g.
public static double fahrenheitToCelsius(double temp) {
return (temp - 32) / 1.8;
}
etc.
A side note: you could simplify your if clause to if(unit.equalsIgnoreCase("F")) or better if("F".equalsIgnoreCase(unit)), since that would handle unit = null as well.
One thing you can do is split the logic which converts temperature i.e.:
public static double toDegreesCelsuis(double tempF){
double c= (tempF - 32) / 1.8;
return c;
}
public static double toFahrenheit(double tempC){
double f=((9.0 / 5.0) * tempC) + 32.0;
return f;
}
These can then be called in your main method like:
double c = Converter.toDegreesCelsuis(40.0);
Here it is,
public class Converter {
public static void main(String[] args) throws Exception{
String unit = JOptionPane.showInputDialog("Enter unit F or C: ");
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp1 = JOptionPane.showInputDialog("Enter the Temperature: ");
double temp = Double.valueOf(temp1).doubleValue();
double f = getTemprature(temp, unit);
JOptionPane.showMessageDialog(null,f+" Fahrenheit");
}
double getTemprature(double temp, String unit){
if((unit.equals("F"))||(unit.equals("f"))){
double c= (temp - 32) / 1.8;
JOptionPane.showMessageDialog(null,c+" Celsius");
}
else if((unit.equals("C"))||(unit.equals("c"))){
double f=((9.0 / 5.0) * temp) + 32.0;
}
}
}