Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to solve the problem of letting empty value of EditText be parsed into integer

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code.

 
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

In the catch block, assign whatever default value the variable should take to it. If there is no appropriate default value, communicate that to the user, and prevent whatever action would otherwise have taken place from happening.
 
Keong Jian Wai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Welcome to the Ranch.

In the catch block, assign whatever default value the variable should take to it. If there is no appropriate default value, communicate that to the user, and prevent whatever action would otherwise have taken place from happening.



Can give more detailed example? Because I want to store the number that I entered on EditText et there. And directly add the number I entered into textview.
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can give more detailed example?


An example of what? You already have a catch block - what problem are you facing, specifically, to assign a default value to food in there? Or to display a Toast to the user instead? Obviously, you wouldn't want to have the addition happen in the latter case.


Because I want to store the number that I entered on EditText et there. And directly add the number I entered into textview.


Well, if the number cannot be parsed, then it cannot be added, and it wouldn't make sense to display it, no? :-}
 
Keong Jian Wai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:

Can give more detailed example?


An example of what? You already have a catch block - what problem are you facing, specifically, to assign a default value to food in there? Or to display a Toast to the user instead? Obviously, you wouldn't want to have the addition happen in the latter case.


Because I want to store the number that I entered on EditText et there. And directly add the number I entered into textview.


Well, if the number cannot be parsed, then it cannot be added, and it wouldn't make sense to display it, no? :-}



I can parse the string into integer, but due to the first edittext is blank and it parsed the blank into integer. Now, my problem is how to avoid empty value of edittext to be added to textview.
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously, you should only set a value if you have one that makes sense. An empty field does not constitute a valid number, so the code should ignore it entirely (or display a message that a valid number is required).
 
Keong Jian Wai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my codes are firstly convert edittext blank value into string and parsed it into integer when I clicked the listview. This is occur when alertdialog is prompt up.So, got what method is to prevent user when clicked listview,then edittext will directly convert to string and then integer.And lastly become value at textview.
 
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another way to test for numbers without using an exception. Remember that exception handling is expensive in terms of execution time. Pass the text to the constructor of a Scanner and use the hasNextInt() method. More details in this old thread, and a little more info here.
Warning(1): those techniques may be locale‑specific. Go through the Scanner documentation to find out.
Warning(2): those techniques will happily report, “1234567890 CodeRanch,” or similar as containing a correct number.
 
Keong Jian Wai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
During the moment of alertdialog prompt up, edittext value is defined as blank value.Then, I needed to enter second number so that textview will display the first number I entered before. This means that whatever I entered at first time, it must be blank. How to let the edittext not be blank when I clicked on amount listview.
 
Rancher
Posts: 92
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "not be blank". What do you want there instead? Zero?
 
Keong Jian Wai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:What do you mean by "not be blank". What do you want there instead? Zero?


I want the textview displayed will be the number I entered on the edittext. But now the problem is when I first time entered a number, textview displayed is null. Second time I entered a number, textview will become the number I entered for first time. This means that when I clicked on the listview, the moment of alertdialog pop up, since at first my edittext is blank, not entered any number yet. Then, it proceed blank value and convert it to string and then integer. So that first number displayed at textview is blank.
 
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps writing some pseudo code would help you. This is an example, but probably not correct.
Other options, of course, is to prompt the user to fill in a valid value and the re-process.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!