This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hey guys! I'm programming in Java 8 right now for college. I'm working on a pretty simple and common assignment. Basically, in the part I'm at now, I need to print the value from a while loop on a println. The value was incremented 3 times. When it's printed, it's printed as 0, but I need it to print as 3. I'll paste the assignment, the error, and my code. I've tried moving the println after the while loop to be inside of the while loop. However, nothing at all prints if i do it. Keep in mind, this class is very bare-bones. We haven't touched arrays yet, try, etc.
----------------------------------------------------------------------------------------------------------------------------------------------------
ASSIGNMENT:
Write a program that reads a file and makes a copy of that file, but without the comma’s, and writes it back out to a second file. It should print to the console window, at the end, the number of comma’s removed.
The program needs to:
- Prompt the user for the name of the file to read.
- Reads file
- Write the non-comma characters to output.txt, including all spaces.
- When done reading the input file, write the total number of comma’s removed to the console window.
For example, if the input file contains 3 ,2 = 5m, 7%,6 =1 hello
Then the output.txt file should contain:
3 2=5m 7%6=1 hello
And the console window should print “Removed 3 commas”.
Input Format
Name of a file
Constraints
none
Output Format
An output file with no commas in it.
-----------------------------------------------------------------------------------------------------------------------------------------------------
ERROR:
Your code did not pass this test case.
Input (stdin)
in.txt
Your Output (stdout)
Enter input file name
Detected 0 commas.
Expected Output
Enter input file name
Detected 3 commas.
Compiler Message
Wrong Answer
-----------------------------------------------------------------------------------------------------------------------------------------------------
CODE:
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
PrintWriter createFile = new PrintWriter("in.txt");
createFile.println("3 ,2 = 5m,");
createFile.println(" 7%,6 =1 hello");
createFile.close();
Scanner scan = new Scanner(System.in);
System.out.println("Enter input file name");
String filename = scan.next();
int commas = 0;
while(scan.hasNext()) {
String s = scan.nextLine();
for (int i = 0; i < s.length(); i ) {
if(s.charAt(i) == ',')
commas ;
}
String replaceString;
replaceString=filename.replaceAll(","," ");
}
System.out.println("Detected " commas " commas.");
}
}
Subreddit
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/javahelp/co...