2014年5月17日星期六

On the windows of the carriage return and line breaks java system problems

here to Scanner class useDelimiter (pattern: String) [ Returns: A Case Study of the Scanner object ]
File file txt.:
q w e
qq ww ee
rrr rrr ttt

Scanner input=new Scanner(File);
if(input.useDelimiter("\n"))
ArrayList<String> arr=new ArrayList<String>();
while(input.hasNext()){
arr.add(input.next());
}
The first use
\ n:
printed on the console arr:
results:
[q w e
, qq ww ee
, rrr rrr ttt]
Question 1: Why to \ n as a delimiter to get results
line n \ r, ( a space ) n +1 th row

The second use \ r only useDelimiter () to change the place, do not go into the next
In the console print arr:
[q w e,
qq ww ee,
rrr rrr ttt]
Question 2: Why to \ r effect is obtained as a delimiter
line n , ( a space ) \ r n +1 th row
The third use \ n \ r:
In the console print arr:
[q w e
qq ww ee
rrr rrr ttt]
Question 3: Why to \ n \ r effect is obtained as a delimiter
line n \ r n +1 th row
fourth use \ r \ n:
In the console print arr:
[q w e, qq ww ee, rrr rrr ttt]
Question 4: Why to \ r \ n as a delimiter to get results
line n , ( a space ) n +1 th row
fifth use [\ n \ r] or [\ r \ n]
In the console print arr:
[q w e, qq ww ee, rrr rrr ttt]
Question 5 : Why to [ \ n \ r ] as a separator to get the effect of
line n , ( a space ) , ( a space ) n +1 th row

I also know that the windows system by pressing Enter, generate \ r \ n two characters , but if it is to generate \ r \ n can not explain the above problem,
feel more like the form r \ \ n # 's [Note : # represents a character ] , but on the \ n \ r when the separator can not be interpreted as a problem . ( I also thought the # as \ r, but does not meet the problem 5 ) , seeking expert answers .
------ Solution ---------------------------------------- ----

    public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }

------ For reference only ---------------------------- -----------
file newline is \ r \ n
\ r and \ n output effect is newline
1.qwe \ r, qqwwee \ r, rrrtt \ r
2.qwe \ n, qqwwwe \ n, rrrtt \ n
3.qwe \ r \ nqqwwee \ r \ nrrrtt \ r \ n
4.qwe, qqwwee, rrrrtt
5. use \ n \ r is not split , the entire string as a string
------ For reference only ----------------- ----------------------
not quite understand , I want to know questions 1-5 ArrayList input to each element in the spaces and line breaks how to produce.
------ For reference only -------------------------------------- -

element spaces that file space, newline is \ r or \ n or \ r \ n generated
------ For reference only ------ ---------------------------------



  
element spaces that file space, newline is \ r or \ n or \ r \ n generated  

Xiongtai can try the code yourself. Space is by no means any original documents

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;
/**此题使判本流中的回车概念的精确了解
 * 于5月11日写完
 *\040表示空格*/
public class Execrise9_17 {
public static void main(String[] args) throws FileNotFoundException{
File f=new File("9_17.txt");
//System.out.println(f.exists());
//System.out.print("\nss");

Scanner input =new Scanner(f);
System.out.print("\040nihao");
System.out.println("char "+countAllChar(f));
System.out.println("word "+countAllWord(f));
System.out.println("rows "+countRows(f));
}



public static int countRows(File f) throws FileNotFoundException {
Scanner input =new Scanner(f);
Properties pp = System.getProperties();
//String newLine = pp.getProperty("line.separator");
//input.useDelimiter(newLine);
                 input.useDelimiter("\n");
                 /**将这里的\n换成\r \n\r \r\n [\n|\r]即可知道我提问的东西*/
ArrayList<String> arr=new ArrayList<String>();

while(input.hasNext()){

arr.add(input.next());
}
System.out.println("在控制台打印arr:");
System.out.println(arr);
return arr.size();
}


private static String usenr(File f) throws FileNotFoundException{
Scanner input =new Scanner(f);

input.useDelimiter("[\n\r]");
ArrayList<String> arr=new ArrayList<String>();
StringBuilder sb=new StringBuilder();
while(input.hasNext()){
sb.append(input.next());
//arr.add(input.next());
}
System.out.println(sb);
return sb.toString();
}
public static int countAllChar(File f) throws FileNotFoundException{
return usenr(f).length();
}

public static int countAllWord(File f) throws FileNotFoundException {
int count=0;
Scanner input =new Scanner(f);

input.useDelimiter("[\n||\r]");
ArrayList<String> arr=new ArrayList<String>();
StringBuilder sb=new StringBuilder();
while(input.hasNext()){
arr.add(input.next());
}
System.out.println("利用n||r");
System.out.println(arr);
for(int i=0;i<arr.size();i++){
String[] s=arr.get(i).toString().split(" ");
count+=s.length;
}
return count;



}




}

------ For reference only ----------------------------------- ----

What you say is blank spaces ?
q w e
not qwe between ?
------ For reference only ----------------------------- ----------
is the space after the comma ? was the ArrayList toString plus the
------ For reference only ----------- ----------------------------
\ r represents a return to our first column , \ n is the newline character , two one is different.
\ r \ n represents a carriage return, a carriage return is actually a combination of two actions , that is, return to the first character then wrap.
For now , \ n direct newline characters in the next line without a default case back to the first character , so in many cases \ n Enter operate directly instead .
However, in some ancient editor, does not represent a carriage return line feed , it will not return to the line , but in the forefront of the next line of business , and some will use tab completion , and some will use the space completion.
------ For reference only -------------------------------------- -

ah , I said that the space after the comma


java source code is in it?
bothers me the space was originally produced here , thank you, I quote the knot .
------ For reference only -------------------------------------- -

Thank you , my understanding over , waynexuan has helped me solve the problem.
------ For reference only -------------------------------------- -

java.util.AbstractCollection.toString ()

没有评论:

发表评论