2013-10-20 133 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()