2

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);
    }
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
Bob
  • 69
  • 10
  • Are you sure about the logic to calculate ascii values of each character? First focus on that, the main method won't be an issue – Balwinder Singh Nov 01 '15 at 22:21
  • @BalwinderSingh that is a correct way to get the ascii code of each char of a string. – Manos Nikolaidis Nov 01 '15 at 22:35
  • @ManosNikolaidis He wishes to use the integer which are being passed during method call and which are not used to get the given ascii values – Balwinder Singh Nov 01 '15 at 22:37
  • @Bob how where you intending to use `ValOne` and `ValTwo` ? – Manos Nikolaidis Nov 01 '15 at 22:38
  • @ManosNikolaidis I'm just trying to add together the ascii values of two strings. so: "ABC"( which equals 198) + "ABC" = 396. I just thought I needed two values to be able to write the overloaded method. I think I'm just confused about the process. – Bob Nov 01 '15 at 22:55
  • @Bob Then you should define overloaded methods that take 1 or 2 `String`. There are 2 suggestions below. – Manos Nikolaidis Nov 01 '15 at 23:09

3 Answers3

0

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);
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • His logic for getting ascii value of any integer is totally incorrect. The calls from main method won't help him right now but will do once he figure out the logic for getting ascii value of any integer – Balwinder Singh Nov 01 '15 at 22:30
  • his logic for getting ascii values of each char in the string is actually correct – Manos Nikolaidis Nov 01 '15 at 22:34
  • That's true. But he wishes to use the integer which are being passed during method call and which are not used to get the given ascii values – Balwinder Singh Nov 01 '15 at 22:36
  • @ManosNikolaidis Thank you! – Bob Nov 02 '15 at 00:11
  • @ManosNikolaidis in my main, it says "cannot find symbol" for Integer1 and Integer2. What's wrong? I apologize for being so slow with this. I've been having a hard time with methods. Thanks again for the help. – Bob Nov 02 '15 at 00:17
  • You have to pass `String` variables and these `String` variables must have been initialized with values. I edited my answer with example. Hope it helps – Manos Nikolaidis Nov 02 '15 at 00:19
0

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);
}
The Law
  • 344
  • 3
  • 20
0

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);
}
Shadow
  • 3,926
  • 5
  • 20
  • 41