-1

I am new to android and java. I need help. I need to use String for a method isValid as shown below. But strings can't use the operator > and <. I am using it to restrict the user from putting invalid number which is more than 9 numbers. Thanks in advance.

package com.Elson.ProjectVersion;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; 

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EnterContactsActivity extends Activity {

private Button saveButton;
private EditText NameEditText;
private EditText PhoneEditText;
private Button ExitButton;
private EditText EmailEditText;
private TextView date;


private int month;//private within class
private int day;
private int year;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_addcontacts);

    setUpViews();

    Calendar calendar =Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);

    Date today = calendar.getTime();
    DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
    String cs = df.format(today);
    date.setText(cs);
}


public void saveClickHandler(View v){

    String ContactsScore;
    ContactsScore= NameEditText.getText().toString();
    String name = String.format(ContactsScore);
    ContactsScore= PhoneEditText.getText().toString();
    String Phone = String.format(ContactsScore);


    Log.d("EnterContacts" , "I hear the Save Button");

    if ( isValid(Phone) ){
        Contacts contacts;
        Date dateofGames= new GregorianCalendar(year,month,day).getTime();
        contacts = new Contacts (name , Phone ,  dateofGames);

        ContactsActivityApplication app  = (ContactsActivityApplication) getApplication();
        //might be wrong

        Log.d("DeBUGGING", "app is this type: " + app.getClass().getName());
        //need add the function addBowlingScores
        app.addallContacts(contacts);

        Toast.makeText(getApplicationContext(), "Your Contact has been Saved!", Toast.LENGTH_SHORT).show();

}
    if( name == null)  {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Please enter your name")
               .setMessage("Phone numbers cannot have more than 8 numbers")
               .setCancelable(false)
               .setPositiveButton("OK", 

                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel(); 
                    }
                });

        AlertDialog alert = builder.create();
        alert.show();
    }



    }



//error is here
private boolean isValid() {

    if (Phone > 0 && Phone < 100000000)
        return true;
    return false;
    // TODO Auto-generated method stub
}





private void setUpViews()
{
    ExitButton = (Button) findViewById(R.id.BtnExit);
    saveButton =(Button) findViewById(R.id.BtnSave);
    NameEditText= (EditText) findViewById(R.id.NameEditText);
    PhoneEditText= (EditText) findViewById(R.id.PhoneEditText);
    EmailEditText= (EditText) findViewById(R.id.EmailEditText);
    date = (TextView) findViewById(R.id.DateTextView);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.addcontacts, menu);
    return true;
}

}

Contacts

 package com.Elson.ProjectVersion;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;


public class Contacts implements Comparable<Contacts> {

    private long id;
    private String name;
    private String Phone;
    private int Email;
    private Date date;
    private double runningAverage;




    public Contacts(String name, String Phone,  Date date) {
        this.name = name;
        this.Phone = Phone;
        this.date = null;
    }

    public Contacts(long id, String name,String Phone) {
        this.id=id;
        this.Phone=Phone;
        this.name= (name);

    }


    public long getId() {

        return id;
    }
    public void setId(long id) {
        this.id = id;
    }

    public String getPhone() {
        return Phone;
    }
    public void setPhone(String Phone) {
        this.Phone = Phone;
    }
    public String getname() {
        return name;
    }
    public void setname(String name) {
        this.name = name;
    }
    public Date getDate() {
        return null;
    }

    public long getDateEpoch(){
        return date.getTime()/1000;
    }
    public void setDateEpoch(long seconds){
        date= new Date (seconds*1000);
    }
    public void setDate(Date date) {
        this.date = date;
    }




    public void setRunningAverage(double runningAverage) {
        this.runningAverage = runningAverage;
    }
    public boolean equals(Object that){
        Contacts bs = (Contacts) that;

        return this.date.equals(bs.date);
      }


    @Override



    public String toString() {
        String result;

        if(date != null) {
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
            result = df.format(date) + "" + name + "" + Phone ; 
    }
    else {
          result = name + "" + Phone ; 
    }
        return result;
    }





    @Override
    public int compareTo(Contacts another) {    
        // TODO Auto-generated method stub
        return 0;
    }

}

4

4 回答 4

3

您需要将 String 转换为支持与int: 另一个数字进行比较的表示。

你可以通过使用来做到这一点Long.parseLong(String string)

long phoneNumber = Long.parseLong(Phone);
if (phoneNumber > 0 && phoneNumber < 10000000)

请注意,NumberFormatException如果您尝试解析不是数字表示的字符串,这可能会抛出 a,因此您应该将代码包含在一个try/catch块中,如果抛出异常则返回 false。

Stringa不直接支持与<和与数字的比较这一事实并不奇怪>。它们是不同的类型,不能相互隐式转换。

问题

"foobar"小于6141?_

没有任何意义。

于 2013-08-03T15:12:47.210 回答
2

不要使用比较,使用正则表达式:

if(phone.matches("[0-9]{1,9}")){
     //Do soemthing
}

如果您需要一个数值,请执行

long phNum= Long.parseLong(phone);

然后做任何你需要的phNum。不要忘记将其包装在 try 块中并 catch NumberFormatException

于 2013-08-03T15:12:50.387 回答
0

您只需首先将字符串转换为 int。

int foo = Integer.parseInt("1234");
于 2013-08-03T15:12:25.770 回答
0

您可以Integer.parseInt(String s)http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)使用

于 2013-08-03T15:12:36.317 回答