Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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_Arrays_Split - Fatal编程技术网

Java 一旦文本文件中的行位于数组中,如何从该行中删除元素?

Java 一旦文本文件中的行位于数组中,如何从该行中删除元素?,java,arrays,split,Java,Arrays,Split,所以,我有一个文本文件,看起来就像这样: <sgkljsd>::=<sdfasfda> <sadfasf> <lol>::=<dgs> <pdja> <l>|<np> <or>::hello|howdy <sdfas>::=<saf>|<sdf> <adlp> <needd>::=huge|massive|big|tall :=

所以,我有一个文本文件,看起来就像这样:

<sgkljsd>::=<sdfasfda> <sadfasf>
<lol>::=<dgs> <pdja> <l>|<np>
<or>::hello|howdy
<sdfas>::=<saf>|<sdf> <adlp>
<needd>::=huge|massive|big|tall
:=
::=  |
::你好
::=| 
::=巨大的、巨大的、大的、高的
所以,在这个文本文件中,不需要第1行和第2行,所以我跳过它们。但是,我需要第3行和第5行中的单词。我当前的代码在“|”处拆分第三行,因此我要么得到“你好”,要么得到:“你好”。那么有没有办法删除一行中的元素呢?在第3行和第5行中,我只需要在“|”处拆分单词。我想删除“<>”中的项目

我目前的代码如下:

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a file name: ");
        String fileName = scan.nextLine();
        File infile = new File(fileName);
        Scanner readIt = new Scanner(infile);

        // removes first line of file
        String junkLine1 = readIt.nextLine();

        // removes second line of file
        String junkLine2 = readIt.nextLine();

        //gets random <word> from text file
        String word = readIt.nextLine();
        // breaks it into "<or>hello" and "howdy"
        String[]word1 = word.split("\\|");
        int rnd = r.nextInt(word1.length);
        String rnd_word = (word1[rndDp]);
        System.out.println(rnd_word);
Scanner scan=新的扫描仪(System.in);
System.out.print(“输入文件名:”);
字符串文件名=scan.nextLine();
文件填充=新文件(文件名);
扫描仪读取=新扫描仪(填充);
//删除文件的第一行
字符串junkLine1=readIt.nextLine();
//删除文件的第二行
字符串junkLine2=readIt.nextLine();
//从文本文件中获取随机值
String word=readIt.nextLine();
//分为“你好”和“你好”
String[]word1=word.split(“\\\\”);
int rnd=r.nextInt(字长1);
字符串rnd_word=(word1[rndDp]);
System.out.println(rnd_word);

所以,我想做的是随机选择数组中的单词并随机打印一个问候语,但我似乎不知道如何删除不必要的文本。感谢您提供有关如何修复或处理此问题的任何想法。

您可以摆脱<>用正则表达式替换它们:

"<needd>::=huge|massive|big|tall".replaceAll("<.+?>", "");
或者您也可以拆分:

"<needd>::=huge|massive|big|tall".split("<.+?>|:+|\\||=");

您可以在此处尝试上面的组合:

String word=readIt.nextLine()
word=word.substring(word.indexOf(“:”)+2)
这将修剪
word
以开始于
@Bethanyluise谢谢,这部分起作用了。但是,第一个单词会被等号抓住。还有什么方法可以删除它吗?然后可以使用
if(word.contains(“=”){word=word.substring(word.indexOf(“=”)+1);}
为什么用这个来破坏这个问题edit@bmarkham不知道发生了什么。谢谢你修理。我是在重新打字的过程中,有没有办法得到它,所以它是巨大的巨大的?不带::=replaceAll(“:::=”,“”)的Like;非常感谢。工作完美,完全符合我的需要Hey@PeteTheProgrammer,我的答案解决了你的问题,你能接受它作为问题的答案吗?
huge|massive|big|tall
"<needd>::=huge|massive|big|tall".split("<.+?>|:+|\\||=");
{huge, massive, big, tall}