0

Ho do i specify the entered string to be between 30 and 60 characters?I tried the following but i get illegal start of expression error at (if) line.

public class Manipulation {
public static void main(String[] args) {

    // let the user enter a string
    OOPHelper.print("Please enter a string: ");
    String s = OOPHelper.readKeyboardString();

    if (s.length >=30) && (s.length <=60){
        OOPHelper.println(s);
    }
    else

3 Answers 3

10

You have a syntax error. Try...

if ( s.length() >=30 && s.length() <= 60) { ...
Sign up to request clarification or add additional context in comments.

Comments

5

There are two problems:

  1. In an if, the contents of the test-expression have to wrapped in (...). So, if (...) && (...) needs to be changed to if ((...) && (...)).
  2. length is a public method, not a public field, so it needs to be called with parentheses. So, s.length needs to be changed to s.length().

Putting it together — change this line:

if (s.length >=30) && (s.length <=60){

to this:

if (s.length() >= 30 && s.length() <= 60) {

Comments

1

Your if statement should be, like

if (s.length() >= 30 && s.length() <= 60)
{
   OOPHelper.println(s);
}

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.