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
8 odpowiedzi
+ 1
Aditya Raj
Change this
f.format("%s %s %s", "Workout","Report","Pool \r\n");
To
String s = input.next();
f.format("%s", s);
+ 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");
}
}
}
+ 1
Giorgos Dim It doesn't work. ☹️
+ 1
//your code goes here
String s = input.next();
f.format("%s \n", s);
+ 1
Yosua Philip Sirait your code is correct ig
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
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
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");
}
}
}