Where it is getting wrong? [SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Where it is getting wrong? [SOLVED]

You want to plan your day and create your to do list. Complete the program to take the names of 3 tasks as input and write them down in the file "tasks.txt", each on a new line. Then use the readFile() method to output the tasks. Sample Code Workout Report Pool Sample Output Workout Report Pool https://code.sololearn.com/clj3MTPgBRUC/?ref=app

11th Mar 2021, 7:17 PM
Aditya Raj
Aditya Raj - avatar
8 Answers
+ 1
Aditya Raj Change this f.format("%s %s %s", "Workout","Report","Pool \r\n"); To String s = input.next(); f.format("%s", s);
11th Mar 2021, 7:33 PM
A͢J
A͢J - avatar
+ 7
import java.io.File; import java.util.Scanner; import java.util.Formatter; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U public class Main { public static void main(String[ ] args) { Scanner input = new Scanner(System.in); try { Formatter f = new Formatter("tasks.txt"); int count = 0; String data; while(count < 3) { data = input.nextLine(); f.format("%s", data+"\r\n"); count++; } f.close(); } catch (Exception e) { System.out.println("Error"); } readFile(); } public static void readFile() { try { File x = new File("tasks.txt"); Scanner sc = new Scanner(x); while(sc.hasNext()) { System.out.println(sc.next()); } sc.close(); } catch (Exception e) { System.out.println("Error"); } } }
5th Sep 2021, 8:12 AM
Fazal Haroon
Fazal Haroon - avatar
+ 1
Giorgos Dim It doesn't work. ☹️
11th Mar 2021, 7:26 PM
Aditya Raj
Aditya Raj - avatar
+ 1
//your code goes here String s = input.next(); f.format("%s \n", s);
24th Jul 2021, 3:35 PM
Yosua Philip Sirait
Yosua Philip Sirait - avatar
+ 1
Yosua Philip Sirait your code is correct ig
19th Jul 2022, 2:16 PM
Avani Somayaji
Avani Somayaji - avatar
0
If i understand correctly it asks u to ouput on a single line each, but ur format string "%s %s %s" seperates them by space instead, use "%s\n%s\n%s" to insert new lines between, \n is the new line character
11th Mar 2021, 7:24 PM
Giorgos
0
Aditya Raj Yes sorry, u must also read the input u get from scanner object, use sc.nextLine(), it gives u the next input line
11th Mar 2021, 7:29 PM
Giorgos
0
import java.io.File; import java.io.FileNotFoundException; import java.util.Formatter; import java.util.Scanner; public class FilesSoloLearn{ public static void main(String[]args) { Scanner stdin = new Scanner(System.in); int i=0; try { Formatter myfile = new Formatter("C:\\Users\\manou\\Documents\\testfile.txt"); System.out.println("Enter three names"); while ( i <3) { String input = stdin.nextLine(); myfile.format("%s %s ", input, "\n" ); i++; } myfile.close(); stdin.close(); File myfile1 = new File("C:\\\\Users\\\\manou\\\\Documents\\\\testfile.txt"); Scanner sc = new Scanner(myfile1); while(sc.hasNext()) { System.out.println(sc.next()); } } catch(FileNotFoundException e) { System.out.println("File does not exist"); } } }
4th Aug 2022, 7:08 PM
Ariaie M
Ariaie M - avatar