-1

When I run this program i get the error Exception in thread "main" java.lang.NumberFormatException: empty String. Is it possible to make this work?Could we import diff projects into a single project? When I run this program i get the error Exception in thread "main" java.lang.NumberFormatException: empty String. Is it possible to make this wor

package com.example.nav1;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public  class SgpaActivity extends Fragment  {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootview = inflater.inflate(R.layout.activity_sgpa, container, 
false);
 //       onButtonClick(view);

    EditText e1,e2,e3,e4,e5,e6 =null;
    e1 = e2 =e3 = e4 =e5 =e6;
    TextView t1 = null;
    float num1=0;
    e1 =  rootview.findViewById(R.id.e1);
    e2 = rootview.findViewById(R.id.e2);
    e3 = rootview.findViewById(R.id.e3);
    e4 = rootview.findViewById(R.id.e4);
    e5 = rootview.findViewById(R.id.e5);
    e6 = rootview.findViewById(R.id.e6);
    t1 = rootview.findViewById(R.id.result);
if(e1!=null)
    num1 = Float.parseFloat(e1.getText().toString());
    float num2 = Float.parseFloat(e2.getText().toString());
    float num3 = Float.parseFloat(e3.getText().toString());
    float num4 = Float.parseFloat(e4.getText().toString());
    float num5 = Float.parseFloat(e5.getText().toString());
    float num6 = Float.parseFloat(e6.getText().toString());
    float sum = 0;
    sum = (num1*25) + (num2*25) + (num3*25) + (num4*25) + (num5*23) + 
(num6*23) ;
    float cgpa = sum/(25+25+25+25+23+23);
    t1.setText(Float.toString(cgpa));

    return inflater.inflate(R.layout.activity_sgpa, container,false);
}

public void onButtonClick (View v)
{  // return inflater.inflate(R.layout.article_view, container, false);
}

}

I expect the output of 1 , 1, 1, 1, 1,1 be 1
Kaveri
  • 1,060
  • 2
  • 12
  • 21
  • 1
    Why can't you use a try/catch handler? – Tillson Apr 24 '19 at 02:30
  • 1
    in `onCreateView` are the `EditText` fields going to have any numeric value? Maybe you should be doing `num1 = Float.parseFloat(e1.getText().toString());` etc in `onButtonClick ` – Scary Wombat Apr 24 '19 at 02:35
  • You could use a Ternary Operator as your argument for all the **Float.parseFloat()** methods, for example: `num1 = Float.parseFloat((e1.getText().toString().matches("-?\\d+(\\.\\d+)?") ? e1.getText().toString() : "0.0"));`. If **e1** contains nothing (Null String) or contains anything other than a signed/unsigned Integer or decimal value then "0.0" is supplied as the argument for the **parseFloat()** method otherwise the actual numerical value in **e1** is supplied. – DevilsHnd - 退職した Apr 24 '19 at 02:53
  • Unclear what you're really asking here. Just catch the exception and substitute a default or error value. No need to pre-validate it @DevilsHnd: this is just wasted code, and possibly code that disagrees with what `parseFloat()` accepts. – user207421 Apr 24 '19 at 03:34
  • @user207421 - I thought the title of this post was quite clear: `without using any try and catch exception handler` – DevilsHnd - 退職した Apr 24 '19 at 03:47
  • have updated the answer , hope it helps to resolve the issue now. – Kaveri Apr 24 '19 at 08:34

4 Answers4

0

When you are using the android:onClick attribute for a button inside a fragment in that case you need to define the function inside the calling activity .

As suggested here : - onClick attribute in fragment

Or

Try implementing the OnClickListner for the button.

Inside the method , do all your validation before setting the answer.

As specified earlier in the comments , the exception is thrown if :-

NullPointerException - if the string is null

NumberFormatException - if the string does not contain a parsable float.

How to avoid it:-

  1. Either you use a regression check as specified here.
  2. If you can ok with double , then try this:- Doubles.tryParse(String) by Google's Guava library as answered here .
  3. Or rather than checking again and again ,you can directly try setting the digits to be allowed as input in the edittext by setting this:-

    android:digits="0123456789."

This link as few good answers for the issue.

Kaveri
  • 1,060
  • 2
  • 12
  • 21
0

First check if your string is not empty using TextUtils.isEmpty(), If string is not empty then check if it contains valid float data using regex.

if(!TextUtils.isEmpty(e1.getText().toString())){
    if (text.matches("[0-9]*\\.?[0-9]+") {
        num1 = Float.parseFloat(e1.getText().toString());
    } else {
        //Toast invalid number
    }
}
karan
  • 8,637
  • 3
  • 41
  • 78
0

I would surround it with a try and catch block . The reason for this is because the string might not be null , but it might be empty or you might find a character that is not a number . Therefore :

try{
   float something = Float.parseFloat(e1.getText().toString());
}catch(NumberFormatException e){
   e.printStackTrace();
}catch(NullPointerException e){
  e.printStackTrace();
}

That's because you don't need to ask if it is null , or if it is empty or if it doesn't contain characters .

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
-1

I think the error is

num1 = Float.parseFloat(e1.getText().toString());

because

e1.getText().toString() is empty.

Brennan Vincent
  • 10,736
  • 9
  • 32
  • 54
  • 1
    final String numStr = e1.getText().toString(); if (numStr.length() > 0) num1 = Float.parseFloat(numStr); – dujianchi Apr 24 '19 at 02:42
  • I tried using try and catch statements , they seem to work but not getting how to use onbuttonclick method in fragments. Keeps showing this error java.lang.IllegalStateException: Could not find method onButtonClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button' – Jaydeep Bhoite Apr 24 '19 at 02:54
  • You can use this Button btn = rootview.findViewById(R.id.your_button_id); btn.setOnClickListener(new OnClickListener()) – Summer-Yang Apr 24 '19 at 03:17