Reputation: 834
I've the below data in a text file
5
this is a test
foobar
all your base
class
pony along
here the first number states the number of lines in my input. followed the lines of data. here i want to reverse the words of a sentence.
Eg:
this is a test
to be reversed as
test a is this
when i run the below code
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);
i get the correct output, but when reading from a file it is something else.
I'm using the below code to see the data in text file.
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();
}
}
and it is giving the output as per my text file.
And similarly when i try to run the above code with the condition to reverse the string, it is giving me a wrong output
The code that i use is
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();
}
}
and the output that i get is
Test cases are:5
this is a test
testais
foobar
all your base
baseyour
class
pony along
along
please let me know where am i going wrong. Thanks you all i have made the change and the code is working fine, but i came across another situation where in I've to add Case number(the test case number) before the result but here in my code the case number remains to be only 1. the code is as below.
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();
}
}
and the output is as below and also there is an extra case with no result.
Case #1
Case #2test a is this
Case #3foobar
Case #4base your all
Case #5class
Thanks
Upvotes: 2
Views: 938
Reputation: 11244
EDIT Updated with the latest code. Please try the following:
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();
}
Upvotes: 3
Reputation: 39477
Here is your fixed program.
Your main problems were:
1) the nested while loop inside the for loop
2) the fact that you didn't append space between the words
As a side note, there is a nice algorithm to
reverse the words of a sentence in O(N) in place.
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();
}
}
Upvotes: 2
Reputation: 2575
The condition of your for loop in the second code snippet is wrong. It should be:
for(int j=array.length-1; j >= 0; j--) {
You also have to append a blank to the output:
output += array[j] + " ";
I would also suggest to use a StringBuilder
to assemble the output string.
Upvotes: 2