Reputation: 61
How to remove this error ? please anyone help I'm getting an error "The operator + is undefined for the argument type(s) java.util.Scanner" don't know why is it showing this?
package com.learn.java;
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
System.out.println("Enter marks of MATHS");
Scanner math = new Scanner(System.in);
System.out.println(math.nextFloat());
System.out.println("Enter marks of ENGLISH");
Scanner eng = new Scanner(System.in);
System.out.println(eng.nextFloat());
System.out.println("Enter marks of SOCIALSCIENCES");
Scanner socialScience = new Scanner(System.in);
System.out.println(socialScience.nextFloat());
System.out.println("Enter marks of SCIENCE");
Scanner science = new Scanner(System.in);
System.out.println(science.nextFloat());
float avgMarks = averageCalculator(math, eng, socialScience, science);
System.out.println(avgMarks);
calculatePerformanceGrades(avgMarks);
}
private static float averageCalculator(Scanner math, Scanner eng,
Scanner socialScience, Scanner science) {
float averageMarks;
averageMarks=((math + eng + socialScience + science) / 4);//getting an error here(The operator + is undefined for the argument type(s) java.util.Scanner)
return averageMarks;
}
private static void calculatePerformanceGrades(float avgMarks) {
if (avgMarks < 40) {
System.out.println("Poor");
} else if (avgMarks >= 40 && avgMarks < 59) {
System.out.println("Average");
} else if (avgMarks >= 60 && avgMarks < 79) {
System.out.println("Good");
} else if (avgMarks >= 80 && avgMarks < 89) {
System.out.println("Very Good ");
} else if (avgMarks >= 90 && avgMarks <= 100) {
System.out.println("Excellent");
}
}
}
Please help to solve the issue.
Upvotes: 1
Views: 3578
Reputation: 99
You are using scanner to perform arithmetic operations which cannot be done. Instead pass float parameters to the method and do manipulations.
public static void main(String[] args) {
System.out.println("Enter marks of MATHS");
Scanner math = new Scanner(System.in);
float math1=(math.nextFloat());
System.out.println("Enter marks of ENGLISH");
Scanner eng = new Scanner(System.in);
float eng1 = eng.nextFloat();
System.out.println("Enter marks of SOCIALSCIENCES");
Scanner socialScience = new Scanner(System.in);
float socialScience1= socialScience.nextFloat();
System.out.println("Enter marks of SCIENCE");
Scanner science = new Scanner(System.in);
float science1= science.nextFloat();
float avgMarks = averageCalculator(math1, eng1, socialScience1, science1);
System.out.println(avgMarks);
calculatePerformanceGrades(avgMarks);
}
private static float averageCalculator(float math, float eng,
float socialScience, float science) {
float averageMarks;
averageMarks=((math + eng + socialScience + science) / 4);
return averageMarks;
}
private static void calculatePerformanceGrades(float avgMarks) {
if (avgMarks < 40) {
System.out.println("Poor");
} else if (avgMarks >= 40 && avgMarks < 59) {
System.out.println("Average");
} else if (avgMarks >= 60 && avgMarks < 79) {
System.out.println("Good");
} else if (avgMarks >= 80 && avgMarks < 89) {
System.out.println("Very Good ");
} else if (avgMarks >= 90 && avgMarks <= 100) {
System.out.println("Excellent");
}
}
Upvotes: 0
Reputation: 37043
You can't use scanner to add data and find an average later. You could just perform arithmetic operations on numbers i.e. integer, floating point numbers etc. Instead define your method to accept input data type like float
(i would take double
here instead)
private static float averageCalculator(float math, float eng, float socialScience, float science) {
....
}
While the input that you take from standard input i.e. Keyboard, you need to capture the value and send that value to your method averageCalculator
like:
Scanner scanner= new Scanner(System.in);//define it once
float math = scanner.nextFloat();//store in a variable
System.out.println(math);
...
Upvotes: 1