(Java) want to return String value from a method [closed]



I need to return the return value type in String.

This is working:

   import java.util.Scanner;
    public class Score {
        public void Display(){
            Scanner input = new Scanner(System.in);
            System.out.println("Please enter your marks: ");
            int newPoint = input.nextInt();
            newPoint = calculateSc(newPoint);
            System.out.println(newPoint);
         }

       public static int calculateSc(int point) {
            if (point <= 100 && point >= 80) {
                return 1;
            }else if (point <= 79 && point >= 60) {
                return 2;
            }else if (point <= 59 && point >= 50) {
                return 3;
            }else if (point <= 49 && point >= 40) {
                return 4;
            }else if (point <= 39 && point >= 30) {
                return 5;
            }else {
                return -1;
           }
       }
   }

This is desired code(Showing error):

    import java.util.Scanner;
        public class Score {
            public void Display(){
                Scanner input = new Scanner(System.in);
                System.out.println("Please enter your marks: ");
                int newPoint = input.nextInt();
                newPoint = calculateSc(newPoint);
                System.out.println(newPoint);
            }
            public static String calculateSc(int point) {  
                if (point <= 100 && point >= 80) {
                     return "You got  A+";//String value
                }else if (point <= 79 && point >= 60) {
                     return "You got A";
                }else if (point <= 59 && point >= 50) {
                     return "You got  A-";
                }else if (point <= 49 && point >= 40) {
                     return "You got  B";
                }else if (point <= 39 && point >= 30) {
                     return "You got  C";
                }else {
                     return "You got F (failed";
                }
           }
       }

I think you all understand, what I want to do.If you have any proper solution please answer.But don’t wanna change second String method calculateSc()

Answer

When you are returning String why are you putting return type to be int? Try learn more about methods and return types

public static String calculateSc(int point) { 
        if (point <= 100 && point >= 80) {
            return "You got A+";
        } else if (point <= 60 && point >= 70) {
            return  "You got A";
        } else {
            return "You got F (Failed)";
        }
    }

you cannot assign String value in int variable remember method returns String after change. So replace these two lines,

newPoint = calculateSc(newPoint); 
System.out.println(newPoint);

with only this,

System.out.println(calculateSc(newPoint));


Source: stackoverflow