2012-03-01 42 views
0

我有一个包含名称与姓氏开始,然后一个文本文件名如:如何分割并重新排列我的字符串?

姓姓

我需要输出他们的名字和那么Surname.eg 姓姓

启动

我有的代码以正确的顺序输出它们。我怎样才能让他们倒过来呢?我的代码是:

public class SplitExample { 
    public static void main(String[] args) throws FileNotFoundException, IOException { 
     // TODO code application logic here 
     FileInputStream fs3 = new FileInputStream("D:/Test.txt"); 
     BufferedReader br3 = new BufferedReader(new InputStreamReader(fs3)); 
     for(int c=0; c< 0; c++){ 
      br3.readLine(); 
     } 
     String name = br3.readLine().trim(); 

     System.out.println(name); 
    } 
} 
+0

这是什么'for(int c = 0; c <0; C++)'? – anubhava 2012-03-01 18:20:21

回答

0

这将做到这一点,我做了一个有点冗长的可读性

FileInputStream fileInputStream = new FileInputStream("C:/test.txt"); 
Scanner scanner = new Scanner(fileInputStream); 
Scanner lineScanner; 

String surname = ""; 
String name = ""; 

while (scanner.hasNextLine()) { 
    String delimiterInFile = " "; 
    String lineInFile = scanner.nextLine(); 

    lineScanner = new Scanner(lineInFile).useDelimiter(delimiterInFile); 

    if(lineScanner.hasNext()){ 
     surname = lineScanner.next(); 
    } 
    if(lineScanner.hasNext()){ 
     name = lineScanner.next(); 
    } 

    System.out.println(String.format("%s %s", name, surname)); 
} 
1

只需用String#分裂这样的:

String[] arr = name.split(" ");` 
String revName = String.format("%s %s", arr[1], arr[0]);