Following code gives me numberformatexception. can you please tell me where to put exepcions or what to do about following lines? logcat tells that there is a problem on commented line
public void zaplatiti(){
EditText zbrojka = (EditText) findViewById(R.id.editText7);
Float[] strings = new Float[allTexts.size()];
Float zbroj=(float) 0;
for(int i=0; i < allTexts.size(); i++){
strings[i] = Float.valueOf(allTexts.get(i).getText().toString());////problem
}
for (int k=0;k<strings.length;k++){
zbroj =zbroj+ strings[k];}
}
zbrojka.setText(String.valueOf(zbroj)+"KN");
}
for(int i=0; i < allTexts.size(); i++){
try {
strings[i] = Float.valueOf(allTexts.get(i).getText().toString());////problem
}
catch (NumberFormatException e) {
// Code to execute if the entered value is not a number
}
}
This is also possible to avoid by only allowing your EditTexts to have numerical values. That will force the user to only enter a numerical value in the first place, then you don't have to catch the exception. But then you should check if the entered text is empty first.
EDIT:
For doing this you must make an if statement, but the correct comparison beetween String values is:
if ( mEditText.getText().toString().equals("String to compare with")){ ... }
the "numerical mode" of an editText could be set up in the XML file as:
android:inputType = "number"
Other valid numerical modes are numberSigned and numberDecimal.
So after changing the input type your code could look like:
for(int i=0; i < allTexts.size(); i++){
if (!allTexts.get(i).getText().toString().equals("")) {
strings[i] = Float.valueOf(allTexts.get(i).getText().toString());////problem
}
else {
// Code to execute if there is no entered value
}
}
Did you check the allTexts Array type?
Related
I've a String[] and i'd like print all positions this String in textview
code:
for (int i = 0; i < something.length; i++) {
myMsg.setText(something[i]);
}
but in textview, only last position is being displayed.
because you are replacing your text in your textview at every iteration by calling setText.
use append instead. like this:
for (int i = 0; i < something.length; i++) {
myMsg.append(something[i]);
}
if you don't want to use append for any reason. You can use Concatenation Operator +.
myMsg = ""; // initialize String to to empty
for (int i =0 ; i< something.length ; i++){
myMsg+= something[i];
}
You can try this too, If you dont want to use loop.
myMsg.setText(Arrays.toString(something));
or without commas and brackets
myMsg.setText(Arrays.toString(something).replaceAll("[,\\[\\]]+", " "));
or without commas and brackets and removed trailing and leading whitespace
myMsg.setText(Arrays.toString(something).replaceAll("[,\\[\\]]+", " ").trim());
Hope it helps :)
I have created multiple set of checkbox(depending on server response ).Now i want to know which checkboxs are clicked,
What i have done is to create checkbox is
for( k = 0; k < stringList.size(); k++) {
cb[k] = new CheckBox(this);
cb[k].setText(stringList.get(k));
cb[k].setTag(feedbackdetails.get(i).getLabel());
cb[k].setId(k);
cb[k].setTextSize(ws.get_width());
ll.addView(cb[k]);
allcheckbox.add(cb[k]); }
i try to get the id on another button click
for(int i=0; i < allcheckbox.size(); i++){
stringcheckbox[i] = allcheckbox.get(i).getText().toString();
try {
if(cb[i].isChecked()){
checkid = cb[i].getId();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i am able to get id of last checkboxgroup.not getting any other check box id.
is the checkid, in checkid = cb[i].getId(); mean string???
if you declare the checkid is string, ofcourse you`ll only get the last id of checked checkbox. change the checkid to array string or array list. it can hold many value, so every checked checkbox id can be stored.
I am trying to colour text for my android app depending on a value entered into the SQLite database. I have set up 3 textviews and have different text colours for all of these.
The code looks like this
String arr[] = data.split("..\n\n");
for(int i = 0; i < arr.length; i++)
{
System.out.println("arr["+i+"] = " + arr[i].trim());
if(arr[i].contains("High Severity"))
{
// String highArr = arr[i];
textView.setVisibility(View.VISIBLE);
textView.setText(highArr+"\n");
textView.setTextColor(Color.RED);
}
else if(arr[i].contains("Low Severity"))
{
textView3.setVisibility(View.VISIBLE);
textView3.setText(arr[i]+"\n");
textView3.setTextColor(Color.GREEN);
}
else if(arr[i].contains("Medium Severity"))
{
textView2.setVisibility(View.VISIBLE);
textView2.setText(arr[i]+"\n");
textView2.setTextColor(Color.rgb(255, 136, 0));
}
}
I have parsed the string which has all the values for my database table, but when I try my for loop it only prints out the latest entered values.
It looks like it could be that your arr[] is only got one entry in it. You should use the debugger to see how many times your loop actually runs and what the actual lenght of the array is.
EDIT:
I just realized that you only have three textviews. Of course the textview is displaying the last one because each time your calling the settext method its reseting the text. There are a few ways to do what you are trying but Im not sure how your app is set up. You should try dynamically creating the textview, setting the text to it, then adding that textview to a linearlayout or relativelayout that you have set up. Or you could do this inside of a listview using a custom implementation of a baseadapter or any of the other adapters depending on your needs.
I would change my code like this:
String arr[] = data.split("..\n\n");
String Hseverity = "", Mseverity = "", Lseverity = "";
for(int i = 0; i < arr.length; i++)
{
System.out.println("arr["+i+"] = " + arr[i].trim());
if(arr[i].contains("High Severity"))
{
// String highArr = arr[i];
textView.setVisibility(View.VISIBLE);
Hseverity += arr[i] +"\n";
textView.setText(Hseverity);//do this to save each High Severity entry
//same for the other severity levels
textView.setTextColor(Color.RED);
}
else if(arr[i].contains("Low Severity"))
{
textView3.setVisibility(View.VISIBLE);
Lseverity += arr[i]+"\n";
textView3.setText(Lseverity);
textView3.setTextColor(Color.GREEN);
}
else if(arr[i].contains("Medium Severity"))
{
textView2.setVisibility(View.VISIBLE);
Mseverity += arr[i] + "\n";
textView2.setText(Mseverity);
textView2.setTextColor(Color.rgb(255, 136, 0));
}
}
My app has 4-5 EditTexts on each activity and I have 10 or so activities. I need to parse each of these to a double. So I figured I want to create a method in which I enter an EditText Array and it returns a double array with the parsed numbers.
One of the EditTexts will always be empty so I would need that specific position in the array of Doubles that are supposed to be returned to 0.
This is what I have been fiddling around with (without success so far).
public double[] parser(EditText[] editArray) {
EditText toBeParsed[] = null;
double parsed[] = null;
for (int i = 0; i < editArray.length; i++) {
try {
parsed[i] = Double.parseDouble(toBeParsed[i].getText().toString());
} catch (Exception e) {
parsed[i] = 0;}
}
Ho do i need to set this up?
Next in one of my activities I have this where I call the method (from a MiscMethods.java file)
EditText inputs[] = {i1,i2,i3,i4}
Double parsed[];
for (int i = 0; i < inputs.length; i++) {
parsed[i] = MiscMethods.parser(inputs);
}
But get a type mismatch... Why? the method returns an array of doubles and should put them into the double parsed[] array.?
It seems like you are using toBeParsed array in stead of your editArray
so
parsed[i] = Double.parseDouble(toBeParsed[i].getText().toString());
should be
parsed[i] = Double.parseDouble(editArray[i].getText().toString());
I have a array of words and I would like to print the array of words onto the screen in a text view after a button is clicked. i was using a for loop to go through the whole list and then set text, but i keep getting a error with that plus it will just replace the last value, so it wont print the whole array. If anybody could explain to me how to do like a g.drawSting() but android version that would be great. my code rt now is not with me but it something like:
-I'm a beginner to android btw, probably could tell by this question tho.
public void onCreate(Bundle savedInstanceState)
{
//code for a button just being pressed{
//goes to two methods to fix the private array{
for(int y=0; y<=array.size()-1; y++){
textArea.setText(aarray.get(y)); //prints all strings in the array
}
}
}
int arraySize = myArray.size();
for(int i = 0; i < arraySize; i++) {
myTextView.append(myArray[i]);
}
if you want to print one by one, then use \n
myTextView.append(myArray[i]);
myTextView.append("\n");
PS:
Whoever suggesting to change .size() to .length(), thanks for you suggestion.
FYI,
The questioner mentioned the variable name is array.size() in question, so the answer also having the same variable name, to make it easier for the questioner.
if your variable (myArray) is an Array use myArray.length(), if it is ArrayList use myArray.size()
You have to combine all text into a String before you can give it the TextView. Otherwise you overwrite the text all the time.
public void onCreate(Bundle savedInstanceState)
{
StringBuilder sb = new StringBuilder();
int size = array.size();
boolean appendSeparator = false;
for(int y=0; y < size; y++){
if (appendSeparator)
sb.append(','); // a comma
appendSeparator = true;
sb.append(array.get(y));
}
textArea.setText(sb.toString());
}
I use this no-index solution, just an easy to remember one liner:
for(File file:list) Log.d(TAG, "list: " + file.getPath());