how to empty (or reset) an edittext - java

Say the user answers a mathematical question, then he moves on to answer the next one. What statement do you use to empty the EditText?
i.e. 1 + 1 = 2, 2 is still in the EditText when the equation is now 3 + 3.
If I haven't made myself clear please let me know.

Set the text to "":
editText.setText("");

Related

I am trying to use the isEmpty() method and I keep getting an error saying: "Cannot resolve method 'isEmpty'. "

this is my click event. The code is supposed to extract entries from text controls and use it to make calculations. At this stage I m trying to make sure that the user enters all of the values that are required, that is: marks for assignments 1, 2, 3 and the exam mark. I realize that I had not entered the "!" to indicate that the controls should not be empty
I believe you wanted to do something like this:
Double assign1;
String stringAssign1 = mark1.getText().toString();
if (!stringAssign1.isEmpty()){
assign1 = Double.parseDouble(stringAssign1);
}
Firstly, you should check if string is empty (also as mentioned before isEmpty() is string function), then if not, parse it to double.
Next time please use codeblock instead of screenshot of code :)

Dividing by four in combobox

I'm a new programmer doing a date of birth selector for part of my project. I have got everything set up apart from a few things and I am unsure of how to do these things.
ArrayList<String> years_tmp = new ArrayList<String>();
years_tmp.add("Year");
for(int years = Calendar.getInstance().get(Calendar.YEAR) ; years>=Calendar.getInstance().get(Calendar.YEAR)-90;years--)
{
years_tmp.add(years+"");
}
Y = new JComboBox(years_tmp.toArray());
Above is my part of my code for a JComboBox which lists the previous 90 years and has the word "Years" as the first object.
For my code above how would I list the years like it currently does, but to only display years which divide by four exactly (leap years)?
Also how do I make it so once the JComboBox list has been opened the selection years can not be selected so when the value is saved in my save file it does not allow the save of the word "Years"?
To get a value which can be devided be 4 without a rest you can use the modulo operator '%':
if(year % 4 == 0) {
...
}
To disallow the Selection of "year" itself, you have several ways. One could be to append an ItemListener to your ComboBox and check whether the user selected the "year" value. If the user selected this value you can print an error message or just select another value - maybe the next possible one. You also can do more fancy stuff like disabling the save Button if the user selected an invalid value..
I think this should help you to get to the right direction.

How to verify the user input in a textfield to compare it with a variable?

I want to verify the user input (number) in a textfield that if it's bigger than 9 or not. Note that the number from 1 to 9.
if it's bigger than 9 I want to show a jOptionPane
some code i have traied:
else
if(jTextField1.contains()){ // want to compare it if it's bigger than 9 or not
jOptionPane1.showMessageDialog(this,"Please enter the number of the tab"); // wich means from 1 to 9
}
So how to do that with java?
Thanks in advance :)
You can directly do it with help of js using jQuery just give id to our text field and add a script tag with the following code :
num = $("#yourId").val();
if(num>9){
alert("your message");
}
or any thing else you want to achieve

How can you compare strings in android with greater than

I was wondering if there is a way to compare strings in android with greater than or >.
Lets say I have this:
String numbers = number.getText().toString();
if (numbers.equals("9")){
output.setText("50");}
so if you enter 9 in the number EditText field the output TextView will display 50.
I have quite a few different numbers that will then = a different number but what can I do if I want 10,11,12,13,etc to = 100?
Is there a way to do this by using something like this?
if (numbers.equals("9"++))
or is there some kind of wildcard in android like
if (numbers.equals("1"+"*"))
i know if i replace the * with zero it will be 10 so if this is possible I could make one for 1, one for 2, one for 3, etc. and this would still save me so much code.
If this is not possible let me know if you have any ideas.
Thanks guys!
You'll need to convert the String to a number first. Something like this:
int number = Integer.parseInt(number.getText().toString());
if (number > 9)
{
output.setText("50");
}
If the String is not guaranteed to be a valid integer you'll have to handle NumberFormatException too.
Is there a reason you can't use
Integer.valueOf("9");

I dont know how to make dropdown in java

(using netbeans and java)
I have the following
1 text field named input 1 (named x5)
1 text field named input 2 (named plus10)
1 text field named input 3 (named plus5perc)
1 answer field (an uneditable text field)
1 button
When a number is placed into either input a calculation is done when the calculate button is pressed e.g. if i put in 2 in input 1 and click the button = input1 * 5 and the answer is displayed in the answer field
when 2 is put into input 2 = (input 2 + 10) * 5
when 2 is put into input 3 = input 3 + 5%
instead of having 3 input fields i would like 1 drop down list and one input
so you choose from the drop down which you want and only have 1 input field.
i don't know how to do dropdowns etc and any help would be appreciated
edit
anyone know how to on load hide the 3 inputs and then show the relivant input once it is selected from the combo box?
The drop down is called combo box in most UIs. The Java swing object is JComboBox
Here's the doc:
http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html
And a tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
I gave this a try (hope that's what you want).
With all that links and tutorials already provided, you should have been able to do that (IMO).
That's what it looks like:
Screenshot http://img97.imageshack.us/img97/9557/socombobox.png
It does not do proper exception handling, does not round the results and is not really object oriented (just uses hardcoded indexes, be careful when changing).
Add the components (called txtInput, cmbChoose, btnDo and txtResult in my case.
Edit the model property of your JComboBox, using Combo Box Model Editor and set it to
x5
plus10
plus5perc
This will generate the following source:
cmbChoose.setModel(new javax.swing.DefaultComboBoxModel(
new String[] { "x5", "plus10", "plus5perc" }));
Put the following into your JButtons ActionPerformed method.
try {
float input = Float.valueOf(txtInput.getText());
float output = 0;
switch (cmbChoose.getSelectedIndex()) {
case 0:
output = input * 5; break;
case 1:
output = input + 10; break;
case 2:
output = input * 1.05f;
}
txtResult.setText(String.valueOf(output));
} catch (Exception e) {
txtResult.setText("[Error]");
}
Sorry about the confusion.
please ignore the other post.
answer from user: italy
two approaches:
(1) Use setVisible - When you create the fields invoke setVisible(false) on each. When a selection is made in the combo box invoke setVisible(true) on the relevant input field and setVisible(false) on the others.
(2) Use one input field - when a selection is made on the combo-box change its name

Categories

Resources