2013-10-20 140 views
-3

//我的問題是,在方法cancelOrder中字符串[] waitingList []至少沒有被看到,這是我的想法。比較ArrayList到字符串

public static String[] canceledOrder(String[] waitingList,String[] waitingList1,String []waitingList2,String[] waitingList3){//I've decided to pass these string [] hoping the string from the other methods will now be seen in canceledOrder(); 
      Scanner in = new Scanner (System.in); 
      int option; 
      System.out.println("Select the event you want to cancel :\n"); 
      events(); 
      option= in.nextInt(); 
      in.nextLine(); 
      System.out.println("Person on wait list is " + waitingList[name]); 
      switch (option){ 

      case 1: 
       System.out.println("Please enter your name:\n"); 
       canceledname = in.nextLine(); 
       System.out.println("name:" + canceledname); 



       for (String s : myStringList) { 
        if(s.equals(canceledname)){ 

         s = waitingList[name]; 

         System.out.println("The new name is\n" + s); 

         name++; 
        } 
        return s; // I want it to now return waitingList[name] 
       } 

       break; 
+0

你說的比較是什麼意思?當然,'ArrayList'不能等於'String'嗎? –

+0

你的意思是檢查ArrayList是否包含字符串? – allejo

+0

我想你想比較'myStringList'裏面的元素,對不對?或者如果你想比較整個'myStringList'單個'字符串變量'這是不可能的..更好的方法是比較一個接一個元素'字符串變量' –

回答

0

我可能是錯的,但是你要遍歷數組列表,看看列表中的任何值等於cancelledName

如果是的話,你可能會想是這樣的

for (String s : myStringList) { 
    if(s.equals(canceledname)){ 

     k = waitingList[name]; 

     System.out.println("The new name is" + k); 

    name++; 
    } 
} 

編輯:可能出現的問題的解決方案

// declare array of Attendees, capacity is 20 
// I would rather use ArrayList, but it seems you're trying to use array 
String attendees[] = new String[20]; 

// delcare wait list 
String waitList[] = new String[2]; 

// declare int for tickets sold 
int ticketsSold = 0; 
// declare waitlist 
int waitlistCount = 0; 

// prompt buyers for purchase of tickets 
// ticketsSold = ticketsSold + 1 or 2 depending on tickets 
// if ticketsSold is 20 do not sell tickets and send to waiting list 
// Do this until full 

// if ticketsSold = 20 
// prompt user to go on wait list 
// if yes, ask how many tickets 
// if tickets requested for waitlist exceeds current waitlist + tickets requested 
     // System.out.println("Sorry, waitlist is full"); 
// else prompt for user name 
String name = in.nextLine(); 
waitlist[waitListCount] = name; 

// here's the part I think you're having problems with 
// if person is deleted from attendees 
// get name of person not attending 
int i = 0; 
while (!cancelledName != attendees[i]) { 
    i++ 
} 

// replace attendees index with waitlist Name 
attendees[i] = waitlist[0]; 

// move the waitlist forward 
waitlist[0] = waitlist[1]; 
waitlist[1] = null; 

這是我能想到的,只要邏輯是最好的。如果你的邏輯是聲音,看起來和我的相似,只關注底部,我認爲你遇到的問題最多

+0

您的建議對我最有意義,而是打印存儲在waitingList [name]中的名稱;它打印出null。我知道編譯器應該能夠取出存儲在waitingList [name]中的信息;因爲我將它設置爲靜態。 – user2899504

+0

Where /你如何聲明和初始化'waitList []'? 'waitingList [name];'中的'name'必須是對'int'的引用。 'name'必須是'index'的值,而不是'String',如果這就是'name'是 –

+0

一步一步解釋你實際嘗試做什麼,因爲我很不清楚。我所知道的是你正在試圖將'canceledName'與'myStringList'中的值匹配。但那又如何? –

0

你要做的就是......你試圖找出如果ArrayList等於String。這從來都不是真的,因爲它們不是同一類型的。

我想你想要的是找出ArrayList的字符串表示是否等於字符串canceledname

myStringList.toString().equals(canceledname) 
0

我想你想找出是否存在myStringListcanceledname,對於你必須穿越myStringList - 沒有辦法繞過它。

for(String name : myStringList){ 
    if(name.equals(canceledname){ 
      // continue... 
      k = waitingList[name]; 
      System.out.println("The new name is" + k); 
      i++; 
      name++; 
    } 
} 

對於那種比較會更好的名字在Set保存例如:

HashSet<String> names = new HashSet<String>(); 
// here you'll add the names to the hash-set 
// using: names.add("whatever"); 
// ... 

//and now: 
if(names.get(canceledname) != null){ 
     // continue ... 
} 
0

你不能比較ArrayList和String,而是使用contains方法來查找元素是否存在於數組列表。

boolean isElementExists = myStringList.contains(canceledname); 

if(isElementExists) { 
    // other codes here 
} 

見API:ArrayList#contains()