2014-09-25 34 views
0

這是一項家庭作業,我們需要從用戶那裏輸入他們的名字和姓氏,並輸入空格。在輸入這些數據後,用戶按下Enter鍵並被要求輸入他們的別名。一旦進入程序,輸出用戶輸入的名字和姓氏,並添加「又名」和用戶輸入的別名,並且還以小寫字母,大寫字母以及首先和最後人物顯示姓和名的首字母以全部小寫字母命名。我已經成功地獲得了程序顯示名字和別名,以及首字母。但我無法弄清楚如何讓最初的實例成爲「AB」和「ab」實例,以及如何獲取用戶輸入並將所有小寫字母都寫入。如何操縱一個字符串爲大寫和小寫

/* 
    * To change this license header, choose License Headers in Project Properties. 
    * To change this template file, choose Tools | Templates 
    * and open the template in the editor. 
    */ 

package aliassearch; 
import java.awt.*; 
import javax.swing.*; 
import java.util.Scanner; 
    /** 
    * 
    * @author students 
    */ 
public class Aliassearch { 
    public static int user_input; 
    /** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    // TODO code application logic here 

    Scanner user_input = new Scanner (System.in); 

    String first_name; 

    System.out.print("Please put in your first name followed by a space and a last name: "); 
    first_name = user_input.nextLine(); 



    String s = new String(first_name); 
    int slength = s.length();  
    //System.out.println(slength); // 


    int sindex = s.indexOf(" "); 
    // System.out.println(sindex); 


    //alias parts 
    String alias_name; 
    System.out.print("Enter your alias: "); 
    alias_name = user_input.next(); 

    // combines user input first name and alias name 
    String full_name; 
    full_name = first_name + " " + alias_name; 

    System.out.println(first_name + " aka " + alias_name ); 

    //converts to initials 
    for (int i = 1; i < first_name.length(); i++) { 
    char c1 = first_name.charAt(i); 

    if (c1 == ' ') { 
    System.out.print(first_name.charAt(i - 7)); 
    System.out.print(first_name.charAt(i + 1)); 
    System.out.print("."); 

    String result; 
    result = s.toLowerCase(); 


} 


} 

}} 
+2

你是否研究過? :)在一秒鐘內谷歌上有這麼多的答案。對於一種解決方案,請查看http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toLowerCase()。我會讓你考慮如何解決你的其他問題。提示,解決方案可能再次在'String'類中。 – nem035 2014-09-25 01:32:19

回答

0

從String類使用toUpperCase方法。 類似於

String aliasUpper = alias_name.toUpperCase();

相關問題