-1

I am trying to say if you enter a character it will do this; else if scanner input == null do this instead. I'm not getting any syntax errors, I just need to know how to word this so that if you don't enter any characters and hit ENTER then it will go to my default settings. Here is what I have so far.

Shape sq = new Square(10);
                System.out.println("Choose character to fill your shape. A-Z, a-z, ! # $ % & ( ) * + Press ENTER.");
                characterChoiceSquare = input.next();
                if(characterChoiceSquare == input.next())
                {
                    for(int m = 0; m < shapeChars.length; m++)
                    }
                    {
                        if(characterChoiceSquare.equals(shapeChars[m]));
                        {
                            char[] c1 = characterChoiceSquare.toCharArray();
                            char[] shapeCharacter = new char[sq.getSizeInt()];
                            for(int i = 0; i < sq.getSizeInt(); i++) 
                            {
                                shapeCharacter[i] = c1[0]; // repeat the char input to fit shapeString
                            }
                            string = String.valueOf(shapeCharacter); //assign the value of the arraylist to shapeString

                            shapeString += string + "\n";

                            System.out.print(shapeString);
                        }
                    }
                }
                else if(characterChoiceSquare == null)
                {
                    System.out.print(sq.displayCharacters());
                }
Pragnani
  • 20,075
  • 6
  • 49
  • 74
StephS
  • 37
  • 1
  • 4
  • you can use .isEmpty() as your condition for when the use does not enter anything instead just hits enter. – Jay T. Feb 29 '16 at 18:24
  • My if - else statement wasn't being used in my code. That does give me errors. So far, without the if-else idea, it prints both options. Maybe if-else isn't the way. but the idea is either pick a character or go to default which is random chars making the same shape – StephS Feb 29 '16 at 18:27
  • you mean if(scanner.isEmpty()) ?? – StephS Feb 29 '16 at 18:27
  • then you can use switch case as @Jonah Haney has already suggested. – Jay T. Feb 29 '16 at 18:28
  • is that a built-in for scanner or would I have to create a new method? – StephS Feb 29 '16 at 18:28
  • ok thanks. I'll work on it =) – StephS Feb 29 '16 at 18:29
  • in your case it would be like characterChoiceSquare.isEmpty() – Jay T. Feb 29 '16 at 18:30
  • I've added this: if(String.valueOf(c1).isEmpty()) { System.out.print(sq.displayCharacters()); } else { System.out.print(shapeString); } – StephS Feb 29 '16 at 19:12

1 Answers1

1

You probably want to use input.nextLine(), then your check would be

else if (String.valueOf(characterChoiceSquare).equals("")){
    ...
}

Or, alternatively, don't even do an else-if check, just have a final else statement that would result if no other if statement returns true.

Another way of doing this would be using switch-case as I would recommend:

switch (String.valueOf(characterChoiceSquare)){
    case "a":
        //do stuff
        break;
    case "b":
        //do stuff
        break;
    case "":
        //do stuff if characterChoiceSquare is empty
        break;
    default:
        //Do this if characterChoiceSquare does not match any cases
}
Jonah Haney
  • 521
  • 3
  • 12
  • wouldn't (characterChoiceSquare.isEmpty()) as a string be the same as (characterChoiceChoiceSquare.toString().isEmpty()) as a char or even (String.valueOf(characterChoiceSquare).isEmpty)) – StephS Feb 29 '16 at 19:52
  • @StephS Oh is `characterChoiceSquare` a `Character` or a `String`? – Jonah Haney Feb 29 '16 at 20:07
  • @ Jonah - It is a char – StephS Feb 29 '16 at 22:25
  • @StephS I have updated my answer to reflect that. And as for your original question, no it is not. `characterChoiceSquare.isEmpty()` does not exist. However the other options are correct. – Jonah Haney Feb 29 '16 at 23:11
  • Ok well I tried isEmpty and they are not working. They simply do nothing as I have them. – StephS Feb 29 '16 at 23:22
  • if(String.valueOf(characterChoiceSquare).isEmpty()) { System.out.print(sq.displayCharacters()); } else { System.out.print(shapeString); } – StephS Feb 29 '16 at 23:24