1. 程式人生 > >JAVA 郵箱格式檢測

JAVA 郵箱格式檢測

import java.util.Scanner;

public class mailtest {
 private final static String  NORMAL_LETTER= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 private static String mail;
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  mailtest mt = new mailtest();
        if(mt.testmail()){
         int i = mail.indexOf("@");
         String username =  mail.substring(0,i);
         String domain_name = mail.substring( i + 1);
         System.out.println("使用者名稱為:" + username);
         System.out.println("域名為:" + domain_name);
        }
        else{
package com.work;

import java.util.Arrays;
import java.util.LinkedList;

/**
 * @author tmj
 * @version 1.00, 13/7/24
 * @see java.lang.Class
 * @since JDK1.6
 */

public class DoubleColorBall {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DoubleColorBall ball = new DoubleColorBall();
		ball.method();
	}

	public void method() {
		LinkedList<Integer> ball = new LinkedList<Integer>();
		int balls = 12;
		for (int a = 1; a <= balls; a++) {
			ball.add(a);
		}
		
		System.out.print("隨機生成的號碼為:");
		
/**
 * 
 */
package com.work;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author tmj
 *
 */
public class FindMaxLengthWords {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner  sc = new Scanner(System.in);
		String sentence =  sc.nextLine();
		//String sentence = "The quick brown fox jumps over the lazy dog";
		FindMaxLengthWords  fmlw = new FindMaxLengthWords();
		LinkedList<String> words = fmlw.Find(sentence);
		fmlw.MaxOne(words);
	}
	
	//把一個句子分割成一個單詞集合
	public  LinkedList<String>  Find(String sentence){
		LinkedList<String> words = new LinkedList<String>();
		String[] ni = sentence.split(" ");
		
		for(String e:ni){
			words.add(e);
		}//新增 空格 和 單詞 到LinkedList中
		
		for( int a = 0; a < words.size(); a++){
			if(words.get(a) == " "){
				words.remove(a);
			}
		}
		
	    return words;
	}
	
	public void MaxOne(LinkedList<String> words ){
		ArrayList<String> Max = new ArrayList<String>();
		Max.add(words.get(0)); 
		for(int a = 1; a < words.size(); a++){
			if(words.get(a).length() > Max.get(0).length()){
				Max.set(0, words.get(a));
				for(int b = 1; b < Max.size(); b++ ){
					Max.remove(b);
				}
			}
			
			else if(words.get(a).length() == Max.get(0).length()){
				Max.add(words.get(a));
				}
		}
		System.out.print("最長的單詞有:");
		for( String e: Max){
			System.out.print(e+"		");
		}
		
	} 
}

int[] select = new int[2];for (int b = 1; b <= 2; b++) { select[b-1] = (int) (Math.floor(Math.random() * balls) + 1);// 判斷是否存在該球,若不存在則重新生成。while (ball.indexOf(select[b-1]) == -1) {select[b-1] = (int) (Math.floor(Math.random() * balls) + 1);}ball.remove(ball.indexOf(select[b-1]));// 刪除該"球"}Arrays.sort(select);for( int e: select){System.out.print(e + " ");}}//排序 亂序} System.out.println("您輸入的不是合法郵箱"); } } public boolean testmail(){ Scanner input = new Scanner(System.in); mail = input.nextLine(); if(mail.indexOf("@") == -1 || mail.indexOf(".") == -1){ return false; }else if(mail.lastIndexOf("@") != mail.indexOf("@") || mail.lastIndexOf(".") != mail.indexOf(".")){//貌似有問題 return false; }else if(mail.indexOf(".") < mail.indexOf("@") ){ return false; }else{ for( int i = 0; i < mail.indexOf("@"); i++){ if(NORMAL_LETTER.indexOf(mail.substring(i, i)) == -1){ return false; } } } return true; }}