0

This is one of the classes of my Android project. I am getting a NoSuchElementException.

When I comment out the two StringTokenizers (st and st1) along with st.nextToken() and st1.nextToken() the app runs fine.

package com.example.client_nic;

import java.util.Calendar;
import java.util.StringTokenizer;

import com.example.client_nic.DataFields.fields;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Meeting extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.clientmeeting, container,false);  //why false still unclear
        EditText nameev= (EditText)view.findViewById(R.id.nameev);
        final String name = nameev.getText().toString();
        if(name==null){
            Toast.makeText(getActivity(), "Name cannot be Empty", Toast.LENGTH_SHORT).show();
        }

        EditText contactev= (EditText)view.findViewById(R.id.contactev);
        final String contact = contactev.getText().toString();

        EditText addressev= (EditText)view.findViewById(R.id.Addressev);
        final String address = addressev.getText().toString();

        EditText remarksev= (EditText)view.findViewById(R.id.remarksev);
        final String remarks = remarksev.getText().toString();

        EditText dateev = (EditText)view.findViewById(R.id.dateev);
        final String date = dateev.getText().toString();
        StringTokenizer st = new StringTokenizer(date, "/");


        final int day = Integer.parseInt(st.nextToken());
        final int month = Integer.parseInt(st.nextToken());
        final int year = Integer.parseInt(st.nextToken());

        EditText timeev = (EditText)view.findViewById(R.id.timeev);
        final String time = timeev.getText().toString();

        StringTokenizer st1 = new StringTokenizer(time, ":");
        final int hourOfDay  = Integer.parseInt(st1.nextToken());
        final int minute = Integer.parseInt(st1.nextToken());

        Button button = (Button)view.findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Calendar calender = Calendar.getInstance();
                calender.set(year, month, day, hourOfDay, minute);
                long inputtime = calender.getTimeInMillis();


                long presenttime = System.currentTimeMillis();
                if(inputtime<presenttime){
                    Toast.makeText(getActivity(), "Time has already passed", Toast.LENGTH_SHORT).show();
                }
                else{
                    String insert = "INSERT INTO "+fields.table_name+" VALUES('"+name+"','"+contact+"','"+address+"','"+remarks+"','"+date+"','"+time+"' ) ";                   
                    Datastore ds = new Datastore(getActivity());
                    SQLiteDatabase sql = ds.getWritableDatabase();
                    sql.execSQL(insert);
                }
            }
        });

        return view;
    }
}
Peter Neyens
  • 9,770
  • 27
  • 33
abhishek gupta
  • 359
  • 1
  • 12
  • What strings are you trying to tokenize? – Joni Jul 26 '15 at 08:36
  • 1
    Side note: you shouldn't insert raw data directly in your DB. Look at [SQL Injection](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work) and [prepared statements with SQLite in Android](http://stackoverflow.com/questions/433392/how-do-i-use-prepared-statements-in-sqlite-in-android). – Peter Neyens Jul 26 '15 at 09:37

2 Answers2

1

You are using st1.nextToken() st.nextToken() while there are no tokens which results

NoSuchElementException

You just have to check if hasMoreTokens is true then you could use it:

while(token.hasMoreTokens())
{
//use st.nextToken()
}

Documentation including an example on how to use StringTokenizer!

Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56
0

This implies that either the date you entered in your EditText dateevdoes not contain enough / characters or time you entered in your EditText timeev does not contain enough : characters.

Instead of StringTokenizer you should probably use String.split() as suggested in the Class Overview of StringTokenizer. This of course in itself won't fix your problem.

So, to fix your problem, as you're taking text from the user, you should check the required number of elements are there rather than just assuming the text is in the correct format. So you'd do something like:

String[] dateStrings = date.split("/");

int day;
int month;
int year;
if (dateStrings.length == 3) {
    day = Integer.parseInt(dateStrings[0]);
    month = Integer.parseInt(dateStrings[1]);
    year = Integer.parseInt(dateStrings[2]);
}

And similarly for time.

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36