1

i dont know what im doing wrong but the compiler keeps saying:

StringLength.java:3: error: class problem is public, should be declared in a file named problem.java public class problem

here is the code:

import java.util.Scanner;

public class problem {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a string");
    String s = input.nextLine();

    String lower = "";
    String total = "";

    for (int i = 0; i < s.length(); i++) {
        char thisChar = s.charAt(i);

        if (thisChar >= 97 && thisChar <= 122) {
            lower += thisChar;
        }
    }

    System.out.println("Total amount of characters: " + s.length() + " - " + s);
    System.out.println("Lower case letters: " + lower.length() + " - " + lower);
    }
}

can someone help me please?

2
  • thanks that was very helpfull Commented Oct 12, 2015 at 4:01
  • if some answer helps you, accept it. No need to post a thanks comment Commented Oct 12, 2015 at 4:34

3 Answers 3

4

Your file is named StringLength.java but your class is named problem on Line 3. Your class name should have the same name as your file name, without the .java extension, i.e. StringLength.

Sign up to request clarification or add additional context in comments.

Comments

1

when you type like java StringLength, jvm will try to find main method in that class and that class has to be public. In this case your class name in which main method exists and class name given in the command is different. Either you should change your file name to problem.java or change your class name to StringLength

Comments

0
char thisChar = s.charAt(i);

charAt() returns character at the index specified but you are checking if (thisChar >= 97 && thisChar <= 122)

Are you checking for ascii value?? if so please correct your code. Example follows.

char thisChar = s.charAt(i);
int thisAscii = String.valueOf(thisChar).codePointAt(0);
if (thisAscii >= 97 && thisAscii <= 122) {

}

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.