2014-11-09 23 views
0

我必須:更新帳戶信息: 程序應首先要求管理員輸入要更新的帳號:輸入帳號:15 然後,應顯示一個菜單,顯示管理員全部舊信息,然後逐步調查以改變特定領域。如果他留下一個空白字段,則傳播舊信息,即先前的信息保持不變。如何在我的if語句中使用,檢查並比較'enter'鍵作爲用戶的輸入。使用Java

問題:當輸入鍵被按下時,將字段留空意味着什麼。我應該如何實現這一目標? 我試過使用字符'10'/'13'和'\ n',但他們不能比較不同的類型,如字符串到char和int到char。

Heres my code: 
import java.util.*; 
class Customers 
{ 

    private String name; 
    private int accountno; 
    private String status; 
    private String type; 
    private int currbal; 
    private String Login; 
    private int Pin; 

    public Customers() 
    { 

     this.name = " "; 
     this.accountno = accountno; 
     this.currbal = currbal; 
    } 
    public Customers(String name1, int A, int Bal) 
    { 
     name = name1; 
     accountno = A; 
     currbal = Bal;  
    } 
    public Customers (String name1,int accountno1, String status1, String type1, int curr, String Login1, int Pin1) 
    { 
     name = name1; 
     accountno = accountno1; 
     status = status1; 
     type = type1; 
     currbal = curr; 
     Login = Login1; 
     Pin = Pin1; 
    } 
    public String getHolderName() 
    { 
     return name; 
    } 
    public int getaccountno() 
    { 
     return accountno; 
    } 
    public String getStatus() 
    { 
     return status; 
    } 
    public String getType() 
    { 
     return type; 
    } 
    public int getCurrentBalance() 
    { 
     return currbal; 
    } 
    public String getLogin() 
    { 
     return Login; 
    } 
    public int getPin() 
    { 
     return Pin; 
    } 

    public void setName(String N) 
    { 
     name = N; 
    } 
    public void setAccountno(int account) 
    { 
     accountno = account; 
    } 
    public void setStatus(String S) 
    { 
     status = S; 
    } 
    public void setType(String T) 
    { 
     type = T; 
    } 
    public void setCurrentBalance(int Bal) 
    { 
     currbal = Bal; 
    } 
    public void setLogin(String Log) 
    { 
     Login = log; 
    } 
    public void setPin(int Pinn) 
    { 
     Pin = Pinn; 
    } 
} 

class ATMmain 
{ 
    public static void main(String [] args) 
    { 
     Customers [] Carray = new Customers [100]; 
     int count = 0; 
     System.out.println ("Begin session (Y/N)? "); 
     char ch = 'Y'; 
     Scanner S = new Scanner(System.in); 
     ch = S.next().charAt(0); 


     while (ch == 'Y' || ch == 'y') 
     { 
     System.out.println(" 1----Create New Account."); 
     System.out.println("2----Delete Existing Account"); 
     System.out.println("3----Update Account Information. "); 
     System.out.println(" 4----Search for Account."); 
     System.out.println(" 5----View Reports"); 
     System.out.println(" 6----Exit"); 

     //Scanner S = new Scanner(System.in); 
     int input = S.nextInt(); 

     if (input == 1) 
     { 

     //Customers [] C1 = new Customers[1]; 
     //Customers [] C2 = new Customers[1]; 
     System.out.println(createNewAccount(count, Carray)); 
     count++; 


     } 
     if (input == 2) 
     { 
      System.out.println(DeleteExistingAcc(count, Carray)); 
      count--; 

     } 
     System.out.println("Do you wish to do some action"); 
     ch = S.next().charAt(0); 
     if (ch != 'Y' || ch != 'y') 
     { 
      System.out.println ("Thank you for your time"); 
     } 
     } 
    } 

    public static int createNewAccount(int count, Customers [] Carray) 
    { 
     //Customers [] Carray = new Customers[10]; 
     System.out.println("Enter account information"); 
     Scanner S = new Scanner (System.in); 
     System.out.println("Login:"); 
     String log = S.next(); 
     System.out.println("Pin Code:"); 
     int Pin = S.nextInt(); 
     System.out.println("Holder's name: "); 
     String holderN = S.next(); 
     System.out.println("Type:"); 
     String type = S.next(); 
     if (!type.equals("Savings") && !type.equals("Current")) 
     { 
      System.out.println("error type! please Re-enter"); 
      System.out.println("Type:"); 
      type = S.next(); 
     } 
     System.out.println("Starting Balance:"); 
     int SBal = S.nextInt(); 
     System.out.println("Status:"); 
     String Stat = S.next(); 
     Customers ACC = new Customers(holderN, count,Stat, type,SBal,log, Pin); 
     Carray[count] = ACC; 

     System.out.println("Account Successfully created!"); 
     System.out.println("account number is" +count); 
     //count++; 
     return 1; 
    } 

    public static int DeleteExistingAcc(int count, Customers [] Carray) 
    { 
     System.out.println ("Enter Account Number: "); 
     Scanner S = new Scanner(System.in); 
     int acc = S.nextInt(); 

     System.out.println("Are you sure you want to delete this Account:" +acc); 
     String x = Carray[acc].getHolderName(); 
     System.out.println("You wish to delete the account held by: " + x); 
     System.out.println("If this information is correct, please re-enter account number: "); 
     acc = S.nextInt(); 
     int accnew = acc + 1; 
     Carray[acc] = Carray[accnew]; 
     System.out.println ("Account successfully deleted"); 

     //Carray.splice(acc,1, 0); 
     return 0; 

    } 
    public static void display(int acc, Customer [] arr) 
    { 
     arr[acc].getHolderName(); 
     arr[acc].getLogin(); 
     arr[acc].getCurrentBalance(); 
     arr[acc].getPin(); 
     arr[acc].getStatus(); 
     arr[acc].getType(); 
     arr[acc].getaccountno(); 
    } 


    public static int UpdateAccountInfo(Customers [] Carray) 
    { 
     System.out.println("Enter Account Number:"); 
     Scanner S = new Scanner(System.in); 
     int acc = S.nextInt(); 

     display(acc, Carray); 
     char ent = '/n'; 
     int ent1 = '/n'; 

     Carray[acc].getHolderName(); 
     String Str = S.next(); 
     if (Str != '\n')     //See here 
     { 
      Carray[acc].setName(Str); 
     } 
     Carray[acc].getaccountno(); 
     int account = S.nextInt(); 
     if(account != '10')     //here 
     { 
      Carray[acc].setAccountno(account); 
     } 
     Carray[acc].getType(); 
     String type1 = S.next(); 
     if (type1 != '/n')     //here 
     { 
      Carray[acc].setType(type1); 
     } 

     Carray[acc].getCurrentBalance(); 
     int bal = S.nextInt(); 
     if (bal != '13')    //here 
     { 
      Carray[acc].setCurrentBalance(bal); 
     } 
     Carray[acc].getStatus(); 
     String stat = S.next(); 
     if (stat != '13')     //here 
     { 
      Carray[acc].setStatus(stat); 
     } 

     System.out.println("Your account has been successfully updated"); 
    } 

    } 

回答

0

您可以S.nextLine()替代S.next(),並檢測一個空行(回車鍵)有:

String str = S.nextLine(); 
if (str.equals("") { 
    // empty line 
} 
+0

怎麼樣與int比較? :/ – SHa 2014-11-09 07:48:11

+0

@SHa你是什麼意思? – Eran 2014-11-09 07:50:55

+0

我的意思是這不會適用於 int x = S.nextLine(); 應該嗎? – SHa 2014-11-09 08:17:25

0

我可以在你的代碼中看到的是,在UpdateAccountInfo()方法中,你有比較字符串對象和int數據對char字符。

在這裏,你已經比較了字符串與字符。因此,該表達式會導致錯誤。

Carray[acc].getHolderName(); 
    String Str = S.next(); 
    if (Str != '\n')     //See here 
    { 
     Carray[acc].setName(Str); 
    } 

這裏,int對char。

Carray[acc].getaccountno(); 
    int account = S.nextInt(); 
    if(account != '10')     //here 
    { 
     Carray[acc].setAccountno(account); 
    } 

在這裏,再一次對char字符串。

Carray[acc].getType(); 
    String type1 = S.next(); 
    if (type1 != '/n')     //here 
    { 
     Carray[acc].setType(type1); 
    } 

這裏,int對char。

Carray[acc].getCurrentBalance(); 
    int bal = S.nextInt(); 
    if (bal != '13')    //here 
    { 
     Carray[acc].setCurrentBalance(bal); 
    } 

在這裏,再次對char字符串。

Carray[acc].getStatus(); 
    String stat = S.next(); 
    if (stat != '13')     //here 
    { 
     Carray[acc].setStatus(stat); 
    } 

嘗試採取數據比較的方式與您進行yes/no和菜單選項選擇相同。

那麼,更好的選擇是讀完整一行並檢查它是否爲空(零長度)。

Scanner S = new Scanner(System.in); 
String input = S.nextLine(); 
if (input != null || input.trim().length() > 0) { 
    //do the update task 
} 
//otherwise you need not update the value 
+0

如果我使用S.nextline();它根本沒有等待輸入:/ – SHa 2014-11-09 07:47:27

相關問題