File - N Words Concatenate
Coding Problem Keys
File - N Words Concatenate
Problem Statement
N words are passed as the input to the program. The program must concatenate the N words into a single string and write it into a file called output.txt (whose location is same as the program execution location). Fill in the missing lines of code so that the program executes successfully.
Input Format
The first line contains N.
N lines contain the N words.
Output
output.txt file is created into which these N words are concatenated and
written into
Boundary Conditions
1 <= N <= 100
Example Input/Output 1
Input
3
good
morning
friend
Output File Content
goodmorningfriend
Note: Max Execution Time Limit: 3000 millisecs
Solution
Programming Language: Java Language
import java.io.*; import java.nio.file.*; import java.util.*; public class Hello { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in);
File name=new File("output.txt"); name.createNewFile(); FileWriter s=new FileWriter("output.txt"); int n=sc.nextInt(); for(int i=0;i<n;i++){ String text=sc.next(); s.write(text); } s.close();
//Published By PKJCODERSString content = new String(Files.readAllBytes(Paths.get("output.txt"))); System.out.println(content); } }
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments