简体   繁体   中英

How to print return from overloaded methods in Java

I'm trying to sum the Ascii values of different strings while adhering to the following instructions:

Create two overloaded methods. One that will give the sum of the ascii values of each character in a String and another which will produce the sum of the ascii values of two Strings.

Using the methods that I already wrote, how could I print out the sum in my main? Thanks!

public class Exam3Question2 
{
    public static int sumAscii(String Input)
    {

        String s = Input; 
        int sum = 0;
        for (int i = 0; i < s.length(); i++)
        {
            sum+= (int)s.charAt(i);
        }
        return sum;
    }

    public  int sumAscii( String Input1, String Input2)
    {
        int sum = sumAscii(Input1) + sumAscii(Input2);
        return sum;
    }
    public static void main(String[] args) 
    {
        Exam3Question2 c = new Exam3Question2();
        Scanner in = new Scanner(System.in);
        String word;
        System.out.println("Enter some text");
        word = in.nextLine();
        sumAscii(word);
        int sum1 = c.sumAscii(Input1);
        int sum2 = c.sumAscii(Input1, Input2);
        int sum3 = sum1 + sum2;
        System.out.println("The sum of the two strings is: " + sum3);
    }
}

Using the two methods that you have written you would print like this

Exam3Question2 c = new Exam3Question2();
int one = c.sumAscii(0);
int two = c.sumAscii(0, 0);
System.out.println("The sum of one String is " + one);
System.out.println("The sum of two Strings is " + two);

You are passing integer(s) ValOne ValTwo to these functions and according to your comments you never planned to use them. These are dummy variables to create overloaded functions. Using String variables would be recommended for that purpose. Since @The Law has already suggested a solution for that with for loops, I will simply add a Java 8 alternative.

public int sumAscii(String s) {
    return s.chars().sum();
}

public int sumAscii(String s1, String s2) {
    return sumAscii(s1) + sumAscii(s2);
}

You would then call these in your main :

public static void main(String[] args) {
    Exam3Question2 c = new Exam3Question2();
    Scanner in = new Scanner(System.in);

    System.out.println("Enter some text");
    String word1 = in.nextLine();

    System.out.println("Enter some text");
    String word2 = in.nextLine();

    int sum1 = c.sumAscii(word1);
    System.out.println("The sum of one string is: " + sum1);
    int sum2 = c.sumAscii(word1, word2);
    System.out.println("The sum of two strings is: " + sum2);
}

You may want to change your method for calculating the one String to this, so it takes the String as parameter and move the userInput outside of the method, to make it more clean

public int sumAscii(String userInput)
{

    String s = userInput; 
    int sum = 0;
    for (int i = 0; i < s.length(); i++)
    {
        sum+= (int)s.charAt(i);
    }
    return sum;
}

And your next method could actually use the previous method in the new overloaded method like this:

public int sumAscii(String userInput1, String userInput2)
{

int sum = sumAscii(userInput1) + sumAscii(userInput2);
    return sum;
}

And your main would look like

 public static void main(String[] args) 
{
//something to invoke constructor and get strings
int sum1Str= sumAscii(userInput1)
int sum2Str = sumAscii(userInput1,userInput2)
System.out.println("The sum of one string is "+sum1Str);
System.out.println("The sum of two strings is " +sum2Str);
}

Are you having an issue with your code compiling? I think you are because in your main statement you are passing variables that don't exist into functions:

public static void main(String[] args) 
{
    Exam3Question2 c = new Exam3Question2();
    Scanner in = new Scanner(System.in);
    String word;
    System.out.println("Enter some text");
    word = in.nextLine();
    sumAscii(word);

    int sum1 = c.sumAscii(Input1); //What is Input1
    int sum2 = c.sumAscii(Input1, Input2); //What is Input2

    int sum3 = sum1 + sum2;
    System.out.println("The sum of the two strings is: " + sum3);
}

Another thing, there is no need to create an object of your current class to call the sumAscii() function, because they are a part of the current class:

public static void main(String[] args) 
{
    //You don't need this line
    //Exam3Question2 c = new Exam3Question2();

    // Remove the c. from the beginning of the function calls
    int sum1 = sumAscii(Input1); //What is Input1
    int sum2 = sumAscii(Input1, Input2); //What is Input2
    //
}

What you want to be doing in main is calling sumAscii() on your word variable so that you can add the result into sum1 :

public static void main(String[] args) 
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter some text");
    String word = in.nextLine();

    int sum1 = sumAscii(word);
    System.out.println("The sum of the string " + word + " is: " + sum1);
}

If you want to call your overloaded sumAscii() method with 2 different word, you will need to get another word from the user:

public static void main(String[] args) 
{
    Scanner in = new Scanner(System.in);

    System.out.println("Enter some text");
    String word = in.nextLine();

    System.out.println("Enter some more text");
    String word2 = in.nextLine();

    int sum2 = sumAscii(word, word2);
    System.out.println("The sum of the two strings is: " + sum2);
}

Lastly, your overloaded sumAscii() method can be simplified to:

public int sumAscii(String Input1, String Input2)
{
    return sumAscii(Input1 + Input2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM