0

I am writing this program for school work that asks the user to input a sequence of letters and numbers and determine whether it is a digit or not.

My teacher wants me to add up the characters that are digits(numbers) and output it as "Your numbers added up together are: place added number here" and output the letters are a string saying "These are your letters: *place letters here."

I think I got the code right that determines whether it is a digit or not but how do I end up adding them? I tried using an if statement that checked if Character.isDigit is true but I keep getting errors and I think I'm writing it totally wrong. Any response would be greatly appreciated.

Here is my code:

import java.util.*;
import java.util.Scanner;
public class MyClass 
{
public static void main(String args[]) 
{

    int sum1 = 0;
    String stringFull = "";
    boolean isTrue = Character.isDigit(string1.charAt(i));




    Scanner getString = new Scanner(System.in);
    System.out.println("Please enter a sequence of letters: ");
    String string1 = getString.nextLine();

    for(int i = 0; i < 5; i++)
    {
        if (Character.isDigit(string1.charAt(i)))
        {
            sum1 += Character.getNumericValue(string1.charAt(i));

        } else {
            stringFull += string1.charAt(i);
        }

        if (isTrue)
        {
             System.out.println(sum1 * sum1);
        }

    }




}

}
2
  • add up the characters that are digits you mean sum ? Commented Jan 26, 2018 at 17:30
  • In Java you can't use variables that you haven't yet declared. For instance, when you try boolean isTrue = Character.isDigit(string1.charAt(i));, i doesn't exist yet. Commented Jan 26, 2018 at 17:30

2 Answers 2

1

You will get compilation error for below as i wasn't declared before this line

boolean isTrue = Character.isDigit(string1.charAt(i));

instead you should initialize it with false

boolean isTrue = false;

Then, your loop should loop till given string length, but you have hard-coded to 5, which will cause exception at runtime when string length is less than 5.

 for(int i = 0; i < 5; i++)

should change to

 for(int i = 0; i < string1.length(); i++)

Also, inside you for loop you are have following line of code, but you have never changed isTrue value inside the loop.

if (isTrue)
{
    System.out.println(sum1 * sum1);
}

And, I think, right place to change isTrue value, where you are checking for digit.

if (Character.isDigit(string1.charAt(i)))
{
    sum1 += Character.getNumericValue(string1.charAt(i));
    isTrue = true;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should use

Integer.parseInt(String.valueOf(string1.charAt(i)))

which returns the number, instead of

Character.getNumericValue(string1.charAt(i))

which returns the ascii code for the number

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.