6 Replies - 1292 Views - Last Post: 30 August 2019 - 04:13 AM Rate Topic: -----

#1 yeakub_ali   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-August 19

showing error in switch operator please help

Posted 29 August 2019 - 07:07 AM

import java.util.Scanner;

public class SwicthClass {

	public static void main(String[] args) {
		// switch value showing error
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter a Name");
		float value=sc.nextFloat();
		switch (value) 
		{
		case "value>=80 && value<=100":
			System.out.println("Congratulations! You are grade A+");
			break;
		case "value>=70 && value<80":
			System.out.println("Congratulations! You are grade A");
			break;
		default:
			System.out.println("No entry found, try again");
		}
	}
		

}
:code:

This post has been edited by modi123_1: 29 August 2019 - 07:10 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: showing error in switch operator please help

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15219
  • View blog
  • Posts: 60,921
  • Joined: 12-June 08

Re: showing error in switch operator please help

Posted 29 August 2019 - 07:10 AM

Please copy/paste your full error message.
Was This Post Helpful? 0
  • +
  • -

#3 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,899
  • Joined: 25-December 13

Re: showing error in switch operator please help

Posted 29 August 2019 - 07:10 AM

Please copy the full text of the error message and paste it here.
Also please wrap all posted code in code tags.
Was This Post Helpful? 1
  • +
  • -

#4 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 51
  • View blog
  • Posts: 694
  • Joined: 23-September 15

Re: showing error in switch operator please help

Posted 29 August 2019 - 07:19 AM

Your switch is holding float value, but case is String.
Was This Post Helpful? 1
  • +
  • -

#5 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,727
  • Joined: 20-September 08

Re: showing error in switch operator please help

Posted 29 August 2019 - 10:48 AM

Your code doesn't seem to make much sense. Please state in simple words what you're attempting to do
Was This Post Helpful? 0
  • +
  • -

#6 chanjul   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 11-September 18

Re: showing error in switch operator please help

Posted 29 August 2019 - 09:04 PM

same like goose I don't know what you are trying to do but I'll try my best to help
Scanner sc=new Scanner(System.in);
        System.out.println("Enter a Name");//I don't know why you ask a name if you want the user to enter number
        int value=sc.nextInt();//it's better to use int. float means the number is in decimal
        //if you want to add condition better using if else rather using switch case.
        switch (value)
        {
        case 80 :
        case 81 :
        case 82 :
        //make case number until 100
        case 100:
            System.out.println("Congratulations! You are grade A+");
            break;
        case 70:
        case 71:
        //make case number until 79
        case 79:
            System.out.println("Congratulations! You are grade A");
            break;
        default:
            System.out.println("No entry found, try again");
        }

        

or you can add the condition only on the switch statement not inside the case
Scanner sc=new Scanner(System.in);
        System.out.println("Enter a Name");//I don't know why you ask a name if you want the user to enter number
        int value=sc.nextInt();//it's better to use int. float means the number is in decimal
        //if you want to add condition better using if else rather using switch case.
        switch ((value>=80 && value<=100)? 1 :
                (value>=70 && value < 80) ? 2 :3){
        case 1 :
            System.out.println("Congratulations! You are grade A+");
            break;
        case 2:
            System.out.println("Congratulations! You are grade A");
            break;
        case 3:
            System.out.println("No entry found, try again");
        }

        


I hope this help :bigsmile:/>/>/>

This post has been edited by chanjul: 29 August 2019 - 09:06 PM

Was This Post Helpful? 1
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,899
  • Joined: 25-December 13

Re: showing error in switch operator please help

Posted 30 August 2019 - 04:13 AM

A switch statement is not the right statement to use for ranges of values.
An if/else if/else statement would be better.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1