Results 1 to 9 of 9

Thread: Empty String

  1. #1
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Empty String

    This is just a part of a program

    When my code asks me
    Enter diamond size ("short", "average", or "tall"):
    how do i make it so that if i don't put anything and press enter it asks me exactly same questions again??

    i tried putting
    input.length() == 0 or input.equals("") or input == null but none of them seems to work

    Java Code:
    import java.util.Scanner;
    public class Pattern 
    {
        public static void main(String[] args) 
        {
    
            Scanner kb = new Scanner(System.in);
            String input;
            String character;
          
            do 
            {
                System.out.print("Enter diamond size (\"short\", \"average\","); 
    	        System.out.print(" or \"tall\"): ");
                input = kb.next().toLowerCase();
            }
            while (checkSize(input) == -1 || input.length() == 0);
            do 
            {
                System.out.print("Enter pattern character: ");
                character = kb.next();
            } 
            while (character.length() > 1);
          
              char ch = character.charAt(0);
          
              displayPattern(checkSize(input), ch);
        }
        public static int checkSize(String size) 
        {
            if (size.equals("short"))
    	{
                return 6;
            }
            else if (size.equals("average")) 
            {
                return 12;
            }
            else if (size.equals("tall")) 
    	    {
                return 22;
            }
     
            return -1;
        }

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Empty String

    .isEmpty() method. look it up in the API in the String class.
    Java Platform SE 7

  3. #3
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Empty String

    i added it inside the public static int checkSize(String size)

    also isn't isEmpty same as .length() == 0??
    else if (size.isEmpty())
    {
    return -1;
    }

    but it doesn't really do anything.. evertime i press enter it would just go down to next line and not ask the questions again

  4. #4
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Empty String

    You still have to build in the logic for it to ask the question again.

    if(String.isEmpty())
    {
    ask question again.
    }

    else
    {
    go on or do something else
    }

    It doesn't know to ask the question again unless you build in the logic to do so.

  5. #5
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Empty String

    Java Code:
    Scanner kb = new Scanner(System.in);
            String input;
            String character;
           
            do
            {
                System.out.print("Enter diamond size (\"short\", \"average\","); 
                System.out.print(" or \"tall\"): ");
                input = kb.next().toLowerCase();
            }
            while (checkSize(input) == -1);
            do
            {
                System.out.print("Enter pattern character: ");
                character = kb.next();
            } 
            while (character.length() > 1);
           
              char ch = character.charAt(0);
           
              displayPattern(checkSize(input), ch);
        }
        public static int checkSize(String size) 
        {
            if (size.equals("short"))
        {
                return 6;
            }
            else if (size.equals("average")) 
            {
                return 12;
            }
            else if (size.equals("tall")) 
            {
                return 22;
            }
            [B]else if (size.isEmpty())
            {
            	return -1;
            }[/B]
            return -1;
        }
    doesn't that return -1 when string is empty?? and since in my do while if it returns -1 it would ask the question again
    Last edited by Norm; 02-26-2012 at 04:38 AM. Reason: added code tags

  6. #6
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Empty String

    No, its not correct. You need to debug and step through your method. Its not a good idea to have so many return statements. You should declare a variable and assign the correct value to it given a certain condition and then return that variable. If you debug it you'll see where your error is.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Empty String

    doesn't that return -1 when string is empty?
    Yes, it does. The best way to check is by adding a line of code to your main method:

    Java Code:
    public static void main(String[] args)   
    {
        System.out.println(checkSize(""));
        Scanner kb = new Scanner(System.in);
        // etc
    -----

    Which leaves you with the question: so why isn't the question being repeated? It would be repeated were checkSize() being called with an empty string as the argument. So, ... is it ever called? Again, add some code to see what happens:

    Java Code:
    do
    {
        System.out.print("Enter diamond size (\"short\", \"average\","); 
        System.out.print(" or \"tall\"): ");
        input = kb.next().toLowerCase();
        System.out.println("Input is -->" + input + "<--");
        System.out.println("About to end the loop and check the size");
    }
    while (checkSize(input) == -1 || input.length() == 0);
    // etc

  8. #8
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Empty String

    .......................omg i just fixed the issue and all i had to do was
    input = kb.nextLine().toLowerCase();
    wow.... this really made me sit in front of my computer for 6 hrs looking at this

  9. #9
    javin.paul is offline Member
    Join Date
    Oct 2011
    Posts
    35
    Rep Power
    0

    Default Re: Empty String

    One Suggestion that you should consider using Enum for fixed set of inputs like "short", "average", or "tall". This will help you to build more robust program which will not break with any typo in code like comparing with "tal".

Similar Threads

  1. How to find empty string from a cell.
    By deshmukh.niraj04 in forum New To Java
    Replies: 3
    Last Post: 09-21-2011, 01:54 PM
  2. getting rid of empty space at the end of a string
    By BoomPony in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 10:40 PM
  3. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  4. Problem with empty search string
    By virendra in forum Lucene
    Replies: 0
    Last Post: 01-06-2010, 11:42 AM
  5. NumberFormatException: empty String
    By svpriyan in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •