字符串使用运算符>和< java限制数字的数量

时间:2013-08-03 15:08:50

标签: java android operator-keyword

我是android和java的新手。我需要帮助。我需要使用String作为方法isValid,如下所示。但是字符串不能使用运算符>和<。我用它限制用户输入超过9个数字的无效数字。提前谢谢。

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;
}

}

联系人

 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 个答案:

答案 0 :(得分:3)

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

您可以使用Long.parseLong(String string)

来完成此操作
long phoneNumber = Long.parseLong(Phone);
if (phoneNumber > 0 && phoneNumber < 10000000)

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

String并不直接支持与<>与数字进行比较,这并不奇怪。它们是不同类型的,不能隐含地相互转换。

问题

  

"foobar"小于6141

没有任何意义。

答案 1 :(得分:2)

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

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

如果您需要数值,请执行

long phNum= Long.parseLong(phone);

然后使用phNum执行任何操作。不要忘记将其包装在try块中并捕获NumberFormatException

答案 2 :(得分:0)

您最初需要将字符串转换为int。

int foo = Integer.parseInt("1234");

答案 3 :(得分:0)