Results 1 to 9 of 9
Thread: Empty String
- 02-26-2012, 02:29 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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; }
- 02-26-2012, 02:42 AM #2
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Re: Empty String
.isEmpty() method. look it up in the API in the String class.
Java Platform SE 7
- 02-26-2012, 02:56 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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
- 02-26-2012, 03:07 AM #4
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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.
- 02-26-2012, 03:19 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
Re: Empty String
doesn't that return -1 when string is empty?? and since in my do while if it returns -1 it would ask the question againJava 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; }Last edited by Norm; 02-26-2012 at 03:38 AM. Reason: added code tags
- 02-26-2012, 03:37 AM #6
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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.
- 02-26-2012, 03:57 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Empty String
Yes, it does. The best way to check is by adding a line of code to your main method:doesn't that return -1 when string is empty?
-----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
- 02-26-2012, 04:28 AM #8
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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
- 02-26-2012, 04:48 AM #9
Member
- Join Date
- Oct 2011
- Posts
- 33
- Rep Power
- 0
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
-
How to find empty string from a cell.
By deshmukh.niraj04 in forum New To JavaReplies: 3Last Post: 09-21-2011, 12:54 PM -
getting rid of empty space at the end of a string
By BoomPony in forum New To JavaReplies: 3Last Post: 02-14-2011, 09:40 PM -
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
Problem with empty search string
By virendra in forum LuceneReplies: 0Last Post: 01-06-2010, 10:42 AM -
NumberFormatException: empty String
By svpriyan in forum New To JavaReplies: 1Last Post: 08-12-2009, 10:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks