Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.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连接字符串数组中的两个项?_Java - Fatal编程技术网

如何使用java连接字符串数组中的两个项?

如何使用java连接字符串数组中的两个项?,java,Java,我有一个文本文件,我已经读过了,并在这个结构中返回了一个字符串“行”数组 {(1),text,(2),text,(3),text........} 我想把它重组为 {(1)text,(2)text,(3)text........} 这意味着将每个数字连接起来,比如(1)和下一个文本等等 public String[] openFile() throws IOException { FileInputStream inStream = new FileInputStream(

我有一个文本文件,我已经读过了,并在这个结构中返回了一个字符串“行”数组

{(1),text,(2),text,(3),text........}
我想把它重组为

{(1)text,(2)text,(3)text........}
这意味着将每个数字连接起来,比如(1)和下一个文本等等

public String[] openFile() throws IOException {

        FileInputStream inStream = new FileInputStream(path);
        InputStreamReader inReader = new InputStreamReader(inStream,"UTF-8");
        BufferedReader textReader = new BufferedReader(inReader);

        int numberOfLine = countLines();
        String[] textData = new String[numberOfLine];

        for (int i = 0; i < numberOfLine; i++) {
            // if (textReader.readLine()!= null) {
            textData[i] = textReader.readLine();
            //}

        }

        textReader.close();
        return textData;

    }
public String[]openFile()引发IOException{
FileInputStream inStream=新的FileInputStream(路径);
InputStreamReader inReader=新的InputStreamReader(流内,“UTF-8”);
BufferedReader textReader=新的BufferedReader(inReader);
int numberOfLine=countLines();
字符串[]文本数据=新字符串[numberOfLine];
对于(int i=0;i
我如何使用Java语言来完成它? 感谢您的帮助和意见

String[]newArray=newstring[textData.length/2];
String[] newArray = new String[textData.length / 2];

for (int i = 0; i < textData.length - 1; i+=2) {
  newArray[i / 2] = textData[i] + textData[i + 1];
}
对于(int i=0;i
但请确保
textData
的长度为偶数


将此代码段放在
return
语句之前,然后返回
newArray

似乎要省略的逗号前面总是有一个右括号。假设这是字符串中发生的唯一一次,您可以在for循环中执行一个简单的替换:

textData[i] = textData[i].replace("),", ")");
如果不是这样,那么您可以做的另一件事是根据要删除的逗号是字符串中的第一个:

    //Locate index of position of first comma in string
     int firstComma = x.indexOf(',');

     //Edit string by concatenating the bit of the string before the comma 
and the bit after it, stepping over the comma in the process
     textData[i] = (textData[i].substring(0, firstComma)).concat(textData[i].substring(firstComma + 1));

是否要删除:(1)或(2)或(num)之类的内容?当您提出问题时,右侧有一个大橙色的“如何设置格式”框,下方有一个预览区域,以及一个包含格式按钮的整个工具栏。花时间看事情进展顺利,这只是在寻求帮助时的礼貌。(看来这次“hewo”帮你解决了。)@RamyMohamed他希望数组中的每个项目都以“(LineNum)”开头。你能写出文件的实际内容吗。如果这是机密的话,改变价值观。因为我没有很好地理解你的问题,如果你的问题总是相同的格式,并且在你的文本中没有),你可以用替换所有)。我想这不是最漂亮的解决办法,但它会奏效。你确定这个答案与这个问题有关吗?是的,我是。。。他想要一个新的数组将两行连接在一起。。。看看这个例子