Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList的输入_Java_Input_Arraylist - Fatal编程技术网

Java ArrayList的输入

Java ArrayList的输入,java,input,arraylist,Java,Input,Arraylist,我在将输入字符串格式化为ArrayList时遇到问题。我把我试过的一些东西注释掉了。最初,我尝试将输入放入字符串,然后添加到arrayList 输入是一个长字符串: (A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500), (A,Name3,800), (A,Name4,150), (A,Name5,850), (A,Name6,750), (A,Name7,950), (A,Name8,250), (R

我在将输入字符串格式化为ArrayList时遇到问题。我把我试过的一些东西注释掉了。最初,我尝试将输入放入字符串,然后添加到arrayList

输入是一个长字符串:

(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500), (A,Name3,800), (A,Name4,150), (A,Name5,850), (A,Name6,750), (A,Name7,950), (A,Name8,250), (R,Name6,750), (A,Name10,450), (A,Name11,1000)*emphasized text*
到目前为止,我所拥有的:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author Joshua
 */
public class HighScore {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
/**


    Scanner sc = new Scanner(System.in);
    String scores = sc.nextLine();

    scores = scores.replace('(', '"');
    scores = scores.replace('(', '"');

    StringBuffer str = new StringBuffer(scores);

   for (int i = 0; i < scores.length(); i++)

   {
    if (scores.charAt(i) == ',')

    {  
       str.insert(i, 'H');
    }
   }
      System.out.println(str);

*/
      ArrayList<Score> list = new ArrayList<Score>();
      ArrayList <Score> topscore = new ArrayList<Score>();

/**      
      list.add (new Score("A","1111",99999));
      list.add (new Score("A","2222",88888));
      list.add (new Score("A","3333",77777));
      list.add (new Score("A","4444",66666));
      list.add (new Score("A","5555",55555));
      list.add (new Score("A","6666",44444));
      list.add (new Score("R","4444",66666));
      list.add (new Score("A","7777",22222));
      list.add (new Score("A","8888",11111));

*/
      Collections.sort(list);



//For loop to add all the high scores with 'A' as the command to the ArrayList topscore

       for(Score addScore : list)
       {
           if (addScore.getCommand().equalsIgnoreCase("A"))
           topscore.add(addScore);
       }

//For loop to remove all the high scores with 'R' as the command from the ArrayList topscore

       for(Score remScore : list)
       {
           if (remScore.getCommand().equalsIgnoreCase("R"))
                  for (int i = 0; i < topscore.size(); i++)
                  {
                   if (remScore.getName().equals(topscore.get(i).getName()))
                       if(remScore.getScoreValue() == topscore.get(i).getScoreValue())
                            topscore.remove(i);
                  }
       }


//Prints the finished finalScore list

      for(Score finalScore : topscore)
       {
        System.out.println(finalScore);
       }





    /**

    String s[]=new String[100];
    for(int i=0;i<s.length;i++)
        {
        s = scores.substring(1, scores.length()-1).split("\\), \\(");
        }

*/

 sc.close();
 }
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.Scanner;
/**
*
*@作者约书亚
*/
公共课高分{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)
{
/**
扫描仪sc=新的扫描仪(System.in);
字符串分数=sc.nextLine();
分数=分数。替换(“(”,“”);
分数=分数。替换(“(”,“”);
StringBuffer str=新的StringBuffer(分数);
对于(int i=0;i对于(inti=0;i,将其用作伪代码

public static void main(String[] args) {
    String input = "(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500)";
    String[] scoreSets = input.split("\\),");
    List<Score> scoreList = new ArrayList<Score>();
    for (String score : scoreSets) {
        score = score.trim().substring(1);
        int closeBrace = score.indexOf(')');
        if (closeBrace == score.length() - 1) {
            score = score.substring(0, score.length() - 1);
        }
        String[] tokens = score.split(",");
        scoreList.add(new Score(tokens[0],tokens[1],Double.valueOf(tokens[2])));
    }

}
publicstaticvoidmain(字符串[]args){
字符串输入=“(A,name1200),(A,name1200),(R,name1200),(A,name2900),(A,name2500)”;
String[]scoreSets=input.split(“\\),”;
List scoreList=新的ArrayList();
for(字符串分数:分数集){
score=score.trim()子字符串(1);
int closeBrace=score.indexOf(');
if(closeBrace==score.length()-1){
score=score.substring(0,score.length()-1);
}
String[]tokens=score.split(“,”);
scoreList.add(新分数(令牌[0],令牌[1],Double.valueOf(令牌[2]));
}
}

将其用作伪代码

public static void main(String[] args) {
    String input = "(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500)";
    String[] scoreSets = input.split("\\),");
    List<Score> scoreList = new ArrayList<Score>();
    for (String score : scoreSets) {
        score = score.trim().substring(1);
        int closeBrace = score.indexOf(')');
        if (closeBrace == score.length() - 1) {
            score = score.substring(0, score.length() - 1);
        }
        String[] tokens = score.split(",");
        scoreList.add(new Score(tokens[0],tokens[1],Double.valueOf(tokens[2])));
    }

}
publicstaticvoidmain(字符串[]args){
字符串输入=“(A,name1200),(A,name1200),(R,name1200),(A,name2900),(A,name2500)”;
String[]scoreSets=input.split(“\\),”;
List scoreList=新的ArrayList();
for(字符串分数:分数集){
score=score.trim()子字符串(1);
int closeBrace=score.indexOf(');
if(closeBrace==score.length()-1){
score=score.substring(0,score.length()-1);
}
String[]tokens=score.split(“,”);
scoreList.add(新分数(令牌[0],令牌[1],Double.valueOf(令牌[2]));
}
}

您还可以使用正则表达式和:

publicstaticvoidmain(字符串[]args){
字符串输入=“(A,name1200),(A,name1200),(R,name1200),(A,name2900),(A,name2500)”;
模式p=Pattern.compile(\\([A,R]?),(\\w+?),(\\d+?)\\);
匹配器m=p.Matcher(输入);
ArrayList分数=新建ArrayList();
ArrayList topScores=新的ArrayList();
while(m.find()){
串动作=m组(1);
字符串名称=m.group(2);
double scoreVal=double.valueOf(m组(3));
分数=新分数(名称,scoreVal);
分数。添加(分数);
如果(“A”。相等信号情况(动作)){
上部核心。添加(分数);
}else{//删除
分数(上核:上核){
//确保在Score类中实现了equals()
if(上核等于(分数)){
表层土。移除(得分);
}
}
}
}
//打印完成的最终核心列表
分数(最终得分:上分){
System.out.println(finalScore);
}
}

您还可以使用正则表达式和:

publicstaticvoidmain(字符串[]args){
字符串输入=“(A,name1200),(A,name1200),(R,name1200),(A,name2900),(A,name2500)”;
模式p=Pattern.compile(\\([A,R]?),(\\w+?),(\\d+?)\\);
匹配器m=p.Matcher(输入);
ArrayList分数=新建ArrayList();
ArrayList topScores=新的ArrayList();
while(m.find()){
串动作=m组(1);
字符串名称=m.group(2);
double scoreVal=double.valueOf(m组(3));
分数=新分数(名称,scoreVal);
分数。添加(分数);
如果(“A”。相等信号情况(动作)){
上部核心。添加(分数);
}else{//删除
分数(上核:上核){
//确保在Score类中实现了equals()
if(上核等于(分数)){
表层土。移除(得分);
}
}
}
}
//打印完成的最终核心列表
分数(最终得分:上分){
System.out.println(finalScore);
}
}

您的问题具体是什么?我正在尝试格式化输入:(A,name1100),(A,name1200),(A,name1200),