Tag: 分隔符

如何确定文本是否具有平衡分隔符?

我有这个问题, 编写一个函数来确定文本是否具有平衡分隔符。 有效分隔符对是(),[],{}和。 它们可能是嵌套的。 此外,确定文本分隔符’和’正确匹配。 我顺便在java编码.. 对于每个测试行,如果它具有平衡分隔符,则输出为“1”,否则为“0”。 下面的例子, 4 — 0 {123} — 1 {qweqwe{sdad} — 0 问题是,如何在java代码中编写,检查这对有效分隔符是否匹配? 对不起,我对分隔符知之甚少。 以下是我的代码.. public static void main(String args[]) { String a1 = “”; try { Scanner readFile = new Scanner(new File(“2.in.txt”)); while (readFile.hasNextLine()) { a1 = readFile.nextLine(); System.out.println(a1); if (a1.equals(“18”)) { System.out.println(“0”); } else { System.out.println(“1”); } } […]

你怎么保持scanner.next()不包括换行符?

我试图简单地使用带有分隔符等于“”的scanner .next()读取文本文件中的单词,但扫描程序包含带有令牌的换行符/回车符。 我已经搜索了互联网,试图找到这个问题的一个很好的例子,但没有找到它所以我在这里发布它。 我在SO上发现了另一个类似的问题。 我还查看了有关扫描仪和模式的文档( http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html )但我仍然无法找到解决方法这个。 文本文件: 这是一个测试 看看if1这个,是否有效 好! 码: int i = 0; String string; try(Scanner scanner = new Scanner(new File(filename))) { scanner.useDelimiter(” “); while(scanner.hasNext()) { string = scanner.next(); System.out.println(i++ + “: ” + string); } }catch(IOException io_error) { System.out.println(io_error); } 输出: 0:这个 1:是 2:a 3:测试 至 4:看 5:if1 6:这个, 7:是 8:工作 […]

在java中为分隔符拆分字符串

我的问题是我想用分隔符^在java中拆分字符串。 我使用的语法是: readBuf.split(“^”); 但这并没有拆分string.Infact这适用于所有其他分隔符,但不适用于^ 。

他们为什么用; 结束Java / C ++中的语句

他们不应该使用。 结束声明。 他们可以使用 – >来调用方法。 这只是一个疏忽吗? 或者有更深入的发展?

如何指定小数分隔符

我有一个相当简单的问题。 我得到像6.03这样的实数输入,但这给了我错误。 如果我将它改为6,03 ,那没关系。 但是,我无法更改我需要处理的输入,因此如何告诉Java使用. 作为分隔符而不是, ? Scanner sc = new Scanner(System.in); double gX = sc.nextDouble(); // Getting errors 谢谢

从print语句中删除最后一个分隔符

这是一个排序整数数组的方法。 如何从输出中删除最后一个分隔符? public void Sort(int[] sort) { for (int a:sort) { System.out.print(a+ “, “); } } 产量 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 期望的输出 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

如何分隔字符串java中的特定元素

我想做的例子: 如果你传入”abc|xyz”作为第一个参数和”|” 作为第二个参数,该方法返回List(“abc”,”xyz”) public List splitIt(String string, String delimiter){ //create and init arraylist. List list = new ArrayList(); //create and init newString. String newString=””; //add string to arraylist ‘list’. list.add(string); //loops through string. for(int i=0;i<string.length();i++){ //stores each character from string in newString. newString += string.charAt(i); } newString.replace(delimiter, ""); //remove string from arraylist 'list'. list.remove(string); //add […]

需要帮助确定扫描仪的行结束

我的程序中有一个扫描程序,它读取部分文件并将其格式化为HTML。 当我正在阅读我的文件时,我需要知道如何让扫描仪知道它在一行的末尾并开始写入下一行。 这是我的代码的相关部分,如果我遗漏了任何内容,请告诉我: //scanner object to read the input file Scanner sc = new Scanner(file); //filewriter object for writing to the output file FileWriter fWrite = new FileWriter(outFile); //Reads in the input file 1 word at a time and decides how to ////add it to the output file while (sc.hasNext() == true) { String tempString […]

如何在第一个逗号之前拆分字符串?

我有一个使用String的重写方法,它返回String格式为: “abc,cde,def,fgh” 我想将字符串内容分成两部分: 第一个逗号和之前的字符串 第一个逗号后的字符串 我的首要方法是: @Override protected void onPostExecute(String addressText) { placeTitle.setText(addressText); } 现在我如何将字符串分成两部分,以便我可以使用它们在两个不同的TextView设置文本?

在读取文件时使用分隔符

我几乎没有使用分隔符的经验,我需要读取一个文本文件,该文件存储多个对象,其数据以逗号(“,”)分隔的单行存储。 然后使用单独的字符串来创建添加到arraylist的新对象。 Amadeus,Drama,160 Mins.,1984,14.83 As Good As It Gets,Drama,139 Mins.,1998,11.3 Batman,Action,126 Mins.,1989,10.15 Billy Elliot,Drama,111 Mins.,2001,10.23 Blade Runner,Science Fiction,117 Mins.,1982,11.98 Shadowlands,Drama,133 Mins.,1993,9.89 Shrek,Animation,93 Mins,2001,15.99 Snatch,Action,103 Mins,2001,20.67 The Lord of the Rings,Fantasy,178 Mins,2001,25.87 我正在使用Scanner读取文件,但是我发现没有找到行错误,整个文件存储在一个字符串中: Scanner read = new Scanner (new File(“datafile.txt”)); read.useDelimiter(“,”); String title, category, runningTime, year, price; while (read.hasNext()) { title = read.nextLine(); category = read.nextLine(); […]