2014-02-22 119 views
2

我在一个文本文件反向词语的句子

5 
this is a test 
foobar 
all your base 
class 
pony along 

下面的数据在这里的第一个数字指出在我输入的行数。遵循数据线。这里我想要翻译一个句子的单词。

Eg: 
this is a test 

当我运行下面的代码

String input = "This is interview question."; 
String output = ""; 
String[] array = input.split(" "); 
for(int i = array.length-1; i >= 0; i--) 
{ 
    output =output+ array[i]; 
    if (i != 0) 
    { 
     output =output+ " "; 
    } 
} 
System.out.println(output); 

我得到正确的输出,但是从文件中读取时,它是别的东西被逆转为

test a is this 

我正在使用下面的代码来查看文本文件中的数据。

import java.io.File; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Dummy { 
    public static void main(String args[]) throws Exception { 
//  Scanner s = new Scanner(new File("D:/GC/Store Credit/A-small.in")); 
     Scanner s = new Scanner(new File("D:/GC/Reverse/B-small.in")); 

     while (s.hasNextLine()){ 
      System.out.println(s.nextLine()); 
     } 

     s.close(); 
    } 
} 

它根据我的文本文件给出输出。

而且同样当我尝试与状态运行上面的代码扭转字符串,它给我一个错误的输出

,我用的是

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Scanner; 


public class RevSent { 
    public static void main(String[] args) throws Exception { 
     File file=new File("D:/GC/Reverse/B-Small.txt"); 
     FileWriter fw=new FileWriter(file); 
     BufferedWriter bw=new BufferedWriter(fw); 
     Scanner sc=new Scanner(new File("D:/GC/Reverse/B-small.in")); 
     int testCaseCount = Integer.parseInt(sc.next()); 
     System.out.println("Test cases are:"+testCaseCount); 

     for(int i=0;i<testCaseCount;i++) 
     { 
      while (sc.hasNextLine()){ 
        String input=sc.nextLine(); 
        System.out.println(input); 
        String output=""; 
        String[] array=input.split(" "); 
        for(int j=array.length-1;j>0;j--){ 
         output+=array[j]; 
        } 
        System.out.println(output); 
       } 
      } 
     sc.close(); 
    } 
} 

代码和输出我得到的是

Test cases are:5 


this is a test 
testais 
foobar 

all your base 
baseyour 
class 

pony along 
along 

请让我知道我在哪里错了。谢谢你们所有我已经做出了改变,代码工作正常,但我遇到了另一种情况,我必须在结果前添加案例编号(测试案例编号),但在我的代码中,案例编号仍然是只有1.代码如下。

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Scanner; 


public class RevSent { 

    public static void main(String[] args) throws Exception { 
     File file=new File("D:/GC/Reverse/B-Small.txt"); 
     FileWriter fw=new FileWriter(file); 
     BufferedWriter bw=new BufferedWriter(fw); 
     Scanner sc=new Scanner(new File("D:/GC/Reverse/B-small.in")); 
     int testCaseCount = Integer.parseInt(sc.next()); 
//  System.out.println("Test cases are:"+testCaseCount); 

     for(int i=0;i<testCaseCount;i++) 
     { 
      String output=""; 
        String input=sc.nextLine(); 
        String[] array=input.split(" "); 
        for(int j=array.length-1;j>=0;j--){ 
         output+=array[j]+" "; 

        } 

        bw.write("Case #" + (i+1) + ": " + output + "\r\n"); 
      System.out.println("Case #" + (i+1) + output+"\r\n"); 
      } 
     bw.close(); 
     sc.close(); 
    } 
} 

和输出如下,也有一个额外的情况下没有结果。

Case #1 

Case #2test a is this 

Case #3foobar 

Case #4base your all 

Case #5class 

感谢

回答

3

编辑用最新的代码更新。请尝试以下操作:

public static void main(String[] args) throws Exception { 
    File file = new File("D:/GC/Reverse/B-Small.txt"); 
    FileWriter fw = new FileWriter(file); 
    BufferedWriter bw = new BufferedWriter(fw); 
    Scanner sc = new Scanner(new File("D:/GC/Reverse/B-small.in")); 
    int testCaseCount = Integer.parseInt(sc.next()); 
    //System.out.println("Test cases are:"+testCaseCount); 
    int i = 0; 

    while (sc.hasNextLine()) { 
     String input = sc.nextLine(); 
     //System.out.println(input); 
     String output = ""; 
     String[] array = input.split(" "); 
     for (int j = array.length - 1; j >= 0; j--) { 
      output += array[j] + " "; 
     } 
     if (!"".equals(output)){ 
      bw.write("Case #" + (i + 1) + ": " + output.trim() + "\r\n"); 
     } 

    } 

    bw.close(); 
    sc.close(); 
} 
+0

感谢您的解决方案,这个工作对我来说,只是想知道我错在哪里 – user2423959

+2

你忘了数组的第一个元素(索引= 0)。递增(j--)在循环后执行*。 – nikis

+2

也忘了把你的输出文字之间的分隔符(空格) – nikis

2

您在第二代码片段循环是错误的条件。它应该是:

for(int j=array.length-1; j >= 0; j--) { 

你也有一个空白追加到输出:

output += array[j] + " "; 

我也建议使用StringBuilder组装输出字符串。

2

这是您的固定程序。

你的主要问题是:
1)嵌套而内环for循环
2)的事实,你没有的话

作为一个侧面说明之间追加的空间,有一个很好的算法到
颠倒O(N)中的句子的单词。
Reverse the ordering of words in a string

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.util.Scanner; 

public class RevSent { 
    public static void main(String[] args) throws Exception { 
     File file = new File("D:/GC/Reverse/B-Small.txt"); 
     FileWriter fw = new FileWriter(file); 
     BufferedWriter bw = new BufferedWriter(fw); 
     Scanner sc = new Scanner(new File("D:/GC/Reverse/B-small.in")); 
     int testCaseCount = Integer.parseInt(sc.nextLine()); 
     System.out.println("Test cases are:" + testCaseCount); 

     for (int i = 0; i < testCaseCount; i++) { 
      String input = sc.nextLine(); 
      System.out.println(input); 
      String output = ""; 
      String[] array = input.split(" "); 
      for (int j = array.length - 1; j >= 1; j--) { 
       output += array[j] + " "; 
      } 
      output += array[0]; 
      System.out.println(output); 
      bw.write(output); 
     } 
     sc.close(); 
     bw.close(); 
    } 
} 
+0

请看我的问题,代码工作正常,但在我的案例部分有一些问题。谢谢 – user2423959