-1

Currently struggling to grasp what my error messages mean. I understand that it doesn't like that I'm changing from a float to an int, but how can I get around that?

import java.util.Scanner;
public class gradeAverage {
    public static void main (String [] args) {
    Scanner sc = new Scanner(System.in); 
    System.out.println("First test score?");
    float test1 = sc.nextInt();
    System.out.println("Second test score?");
    float test2 = sc.nextInt();
    System.out.println("Third test score?");
    float test3 = sc.nextInt();
    float testAverage = (test1 + test2 + test3)/3;
    System.out.println("Your test average is " + testAverage + ".");
    switch(testAverage) {
        case(97.5 <= testAverage <= 100):
        System.out.println("Grade is an A+");
        break;
        case(93.5 <= testAverage <= 97.49):
        System.out.println("Grade is an A");
        break;
        case(89.5 <= testAverage <= 93.49):
        System.out.println("Grade is an A-");
        default:
        System.out.println("Grade is below an A-");
    }
    }
}

error

gradeAverage.java:13: error: incompatible types: possible lossy conversion from float to int
switch(testAverage) {
          ^

gradeAverage.java:14: error: bad operand types for binary operator '<='
case(97.5 <= testAverage <= 100):
                                 ^

first type:  boolean
second type: int
2 errors
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Darkages11
  • 27
  • 3
  • 1
    You cannot write binary operator (by "binary" here I mean the operator takes 2 operands) expressions in this way (e.g. `x <= y <= z`). Use an expression for each two operands, e.g. `x <= y && y <= z`. – faintsignal Sep 10 '18 at 02:07
  • 1
    You can use switch like this (see [here](https://stackoverflow.com/questions/25940748/can-you-use-conditional-statements-in-switch-case-in-android)). You should convert it to a chain of if/else. – Paul Rooney Sep 10 '18 at 02:18
  • 1
    Also assuming your code was valid, you are missing a break; statement before the default; label. – Ryan Leach Sep 10 '18 at 02:27

4 Answers4

2

The first error occurs, because the expression inside a switch statement is restricted to several primitive types, that make it trivial to build 'jump tables' in the implementation/jvm for performance reasons.

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs. Java Language Specification : Switch Statements

The Java compiler is attempting to narrow the type to Integer to match the restriction, but it's possibly unintended behavior to a severity that the compiler designers marked it as an error.

The second error

gradeAverage.java:14: error: bad operand types for binary operator '<='
case(97.5 <= testAverage <= 100):

Is because you have a fundamental misunderstanding of the syntax of Java in regards to case labels.

Case statements in java (as of Java 10 (and soon 11)) accept values, not expressions or statements.

case(93.5 <= testAverage <= 97.49):
        System.out.println("Grade is an A");
        break;

Simply isn't possible in java.

Java Language Specification : Switch Statements Also shows that Java case statements expect ConstantExpression, or EnumConstantName

case ConstantExpression :
case EnumConstantName :

So the values of case statements, MUST be known at compile time.

However, pattern matching in switch statements, as well as switch expressions are currently under research as a part of Project Amber, as specified here: http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
0

According to this reference, you cannot use float for switch statement. How about using if instead?

Tuan Hoang
  • 586
  • 1
  • 7
  • 14
  • 1
    That would change nothing. The imprecision would still be there. And as @faintsignal pointed out in his comment, the main problem here is that OP is trying to do two comparisons like `x <= y <= z` instead of `x <= y && y <= z`. – Gustavo Passini Sep 10 '18 at 02:22
  • Yes, @GustavoPassini. I agree with the expressions but the message comes from the switch, which is line 13 first, then for the expression, which is at line 14. – Tuan Hoang Sep 10 '18 at 02:33
0
  1. You cannot have a range in a switch case. If you have a switch with a given String or integer value, each case checks if the switch input matches exactly one value.

  2. For the case 100, you should be writing it as 100.0 . If you really need to work with ranges instead of single values, use if-elseif-else clauses.

  3. After each case in the switch statement, you need a break; line to tell the code to leave the switch statement otherwise it will continue reading them and perform all actions inside regardless of whether the case preceding it is true or not.

faris
  • 692
  • 4
  • 18
0

You can't not use expression in the case statements in java. The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums. Consider the following use case of the switch-case statement in java :

public class Test {

public static void main(String args[]) {
  // char grade = args[0].charAt(0);
  char grade = 'C';

  switch(grade) {
     case 'A' :
        System.out.println("Excellent!"); 
        break;
     case 'B' :
     case 'C' :
        System.out.println("Well done");
        break;
     case 'D' :
        System.out.println("You passed");
     case 'F' :
        System.out.println("Better try again");
        break;
     default :
        System.out.println("Invalid grade");
  }
  System.out.println("Your grade is " + grade);
  }
  }