-3

I am trying to write an if/else statement as I think that would be my best way. What I need it to do is determine if the name is a Short name, average length name, or a long name. Now with 13 being the average length that would mean I would need to code as below, but I cannot seem to make it work

IT WORKS NOW

      System.out.println("Please Enter your first and last name");
String str = input.nextLine();
// here the code wil display the string's length and its first character
System.out.println("The number of characters in your name is " + 
str.length());

  if(str.length() == 13)
System.out.println("Your name is average length.");
else if (str.length() > 13)
System.out.println("Your name is long length.");
else if (str.length() < 13)
System.out.println("Your name is short length.");
  • 1
    You forgot to write `System.out.println` on the last line, and I hope that word "character" is not truly on it's own line in your code. Also, `length` is a *method*, so it should be `str.length()`m just like you did a few lines earlier. – Andreas Jul 21 '18 at 23:07
  • I strongly recommend using an IDE, which will help you immensely in figuring out and fixing syntax errors. – Andreas Jul 21 '18 at 23:12
  • Hmm I am using Netbeans as my IDE, and with this only being my second application I am struggling a bit with this one. I have edited my code with what you have suggested and it still isnt working – cPlusPlusRocks Jul 21 '18 at 23:47
  • NVM, thank you all so much for your help!!!! – cPlusPlusRocks Jul 21 '18 at 23:54

1 Answers1

0

You've got the right idea with the overall concept, but as Andreas points out, you've got some syntax errors with your code.

Java also requires that you import a scanner to read the users input. I also tweaked a line that subtracts one from your total length of the user input to compensate for the space in the users name.

Everyone starts somewhere... and this is my first response to a post! Keep at it!

package javaapplicationName;
import java.util.Scanner;
public class JavaApplication41 {

    public static void main(String[] args) {
        // You'd want to grab the user's
        // input so we need to initialize a scanner object.
        Scanner input = new Scanner(System.in);
        System.out.println("Please Enter your first and last name");
        String str = input.nextLine();
        System.out.println("The number of characters in your name is " + (str.length() - 1));
        if (str.length() - 1 == 13) {
            System.out.println("Your name is average length.");
        } else if (str.length() - 1 > 13) {
            System.out.println("Your name is long length.");
        } else if (str.length() - 1 < 13) {
            System.out.println("Your name is short length.");
        }
    }
}
Perry Moen
  • 41
  • 5