Java: Overwrite one character while copying a textfile - java

My current program reads a file and copies it to another directory, but I want it to change one single character to x, which is given by two ints for the number of the line and the number of the character in the line.
For example if int line = 5 and int char = 4, the fourth character in line five is changed to an x, the rest remains.
How can I add this to my program?
import java.io.*;
import java.util.Scanner;
public class copytest {
public static void main(String[] args) throws Exception {
readFile();
}
public static void readFile() throws Exception {
// Location of file to read
File file = new File("Old.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//System.out.println(line);
writeFile(line);
}
scanner.close();
System.out.println("File Copied");
}
public static void writeFile(String copyText) throws Exception {
String newLine = System.getProperty("line.separator");
// Location of file to output
Writer output = null;
File file = new File("New.txt");
output = new BufferedWriter(new FileWriter(file, true));
output.write(copyText);
output.write(newLine);
output.close();
}
}

Change your loop to:
int i=0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (i == lineNumber) {
if (line.length() >= charNumber) {
line = line.substring(0,charNumber) + wantedChar +
line.substring(charNumber);
}
}
writeFile(line);
i++;
}
Note that it will replace the char only if the line long enogth.

Ran Eldan has answered your Question, but I want to point out a couple of other major problems with your code:
You are violating Java's identifier style rules. Java class names should be "camel case" and the first character should be an uppercase letter; i.e.
public class copytest {
should be
public class CopyTest {
This is not just a random nit-pick. If you ignore these style rules, you are liable to get yourself into problem when one of your class names collides with a member or package name defined by your ... or someone else's code. The errors can be very hard to spot.
And of course, if you flout the style rules, you will get continual flak from other programmers when they need to read your code.
Your writeFile method is horribly inefficient. Each time you call it, you open the file, write a line and close it again. This results in at least 3 system calls for each line written. Syscall overheads are significant.
And in addition to being inefficient, you have the problem of dealing with existing output files when the program is run multiple times.
What you should do is open the file once at the start of the run, and use the same BufferedWriter throughout.

Related

Using BufferedWriter to get input from the user and write it to a file

I just started using Java, so sorry if I ask some very simple questions. I basically have to get the user to continuously integers, and once they enter a negative number, the loop will exit. The code I have written so far does not seem to be able to write the input I get from the user to the file I created, morescores. When I try opening the file or calling it from the main method, it's blank. I've tried searching it up on google, youtube, and on stackoverflow but nothing seems to be working. I"ll appreciate any help I can get :)
package bufferedreader;
import java.io.*;
import java.util.Scanner;
public class BufferedReader {
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
writeToFile("morescores");
processFile("morescores");
}
public static void writeToFile (String filename) throws IOException {
BufferedWriter outputWriter = new BufferedWriter (new FileWriter ("morescores.txt"));
int score = 1;
while (true) {
Scanner reader = new Scanner(System.in);
System.out.print ("Enter a number: ");
score = reader.nextInt();
if (score < 0) {
break;
} else {
outputWriter.write(score);
outputWriter.newLine();
}
}
outputWriter.flush();
outputWriter.close();
}
public static void processFile2 (String filename) throws IOException {
java.io.BufferedReader inputReader = new java.io.BufferedReader(new InputStreamReader (new FileInputStream ("morescores.txt")));
String line;
while ((line = inputReader.readLine()) != null) {
System.out.println (line);
}
inputReader.close();
}
}
UPDATE: I fixed the problem haha turns out I was trying to print an integer when it could only be a string. I actually have a followup question, I also need to find the average of all the numbers the user inputs. How would I do that? How can I write a code so that the program knows how many times the user inputs a value I actually have a followup question, I also need to find the average of all the numbers the user inputs. How would I do that? How can I write a code so that the program knows how many times the user inputs a value
Convert your int to a String before writing to your file so
outputWriter.write(score);
would be:
outputWriter.write(String.valueOf(score));
if you are wondering why it has to be converted first look at the doc:
https://docs.oracle.com/javase/7/docs/api/java/io/Writer.html#write(int)
you'll see that it doesn't write the int but the character represented by the int
If you pass int value to the write method of BufferedWriter class, then it will be consider as character instead of number, therefore with the current code you have written whatever positive number you are providing will get converted to valid char value and then it will be written into the file.
In order to achieve what you are asking for you need to convert your int value to string before writing it to file and to do that you can use any of the below methods :
String.valueOf(score)
Integer.toString(score)

Java Using Scanner to Read File and Then Read Line

I'm trying to make a scanner that reads a file and deletes the spaces between each word. I can get this much but I can't get it to where they stay on the same line. I can't get the program to read a line, delete the spaces, and then go to the next line. This is the text from my practice project:
four score and
seven years ago our
fathers brought forth
on this continent
a new
nation
I'm currently only getting the first line
and this is my code:
import java.util.*;
import java.io.*;
public class CollapseSpace {
public static void main (String[] args) throws FileNotFoundException{
Scanner fileInput = new Scanner(new File ("textwithspaces.txt"));
String nextLine = fileInput.nextLine();
Scanner lineInput = new Scanner(nextLine);
while(fileInput.hasNext()){
nextLine = fileInput.nextLine();
while(lineInput.hasNext()){
System.out.print(lineInput.next() + " "); // I tried to add a fileInput.NextLine() to consume the line but it isn't working properly
}
System.out.println();
}
}
}
If you only need to iterate line by line and remove spaces between words then you only need one loop, sample code below should do the trick
public static void main (String[] args) throws FileNotFoundException{
final Scanner fileInput = new Scanner(new File ("src/main/resources/textwithspaces.txt"));
while(fileInput.hasNext()){
final String nextLine = fileInput.nextLine();
// remove all spaces
final String lineWithOutSpaces = nextLine.replaceAll("\\s+","");
System.out.println(lineWithOutSpaces);
}
}
First of all, you shouldn't be using * to import classes. It is generally thought of as "bad practice" since it can interfere with your own classes, also it is not very explicit.
You need to loop the nextLine method inside your own loop. And also using a replaceAll method of the string would be good.
I have shown an example below:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
class Main {
public static void main(String[] args) throws FileNotFoundException {
// Create an object to represent a text file
File file = new File("textwithspaces.txt");
// Create a scanner with the text file as argument
Scanner scannerWithFile = new Scanner(file);
// Continue as long as it has a next line
do {
// Replace strings
String thisLine = scannerWithFile.nextLine();
// Only print the line out if not empty
if (!thisLine.isEmpty()) {
// Replace all spaces
thisLine = thisLine.replaceAll(" ", "");
// Print
System.out.println(thisLine);
}
} while (scannerWithFile.hasNext());
}
}
I also switched your while loop to a do while loop, this is so you can just instantly go into the loop without having to check for a condition first, it is done before next iteration.
Your biggest problem is that you declared nextLine = fileInput.nextLine(); outside of the loop, and then used that in Scanner lineInput = new Scanner(nextLine); so it becomes the first line of the text, but then never changes.
I also agree with the other comment that says you shouldn't be using *, it's considered bad practice to import broadly like that, as you're importing a whole lot of stuff you won't be using.
I reconstructed your code to make it work.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Main {
public static void main (String[] args) throws FileNotFoundException{
Scanner fileInput = new Scanner(new File ("textwithspaces.txt"));
while(fileInput.hasNext()){
String nextLine = fileInput.nextLine();
Scanner lineInput = new Scanner(nextLine);
while(lineInput.hasNext()){
System.out.print(lineInput.next() + " ");
}
System.out.println();
}
}
}

Comparing one data structure against another resulting in run time of over 50 mins

I'm writing code which reads in a text file (each line a tweet) and goes through each tweet comparing it against a list of English words to see if the word is misspelled.
So the list of English words is read in from a text file as well, this is then stored in a List. When I run the code for this alone, it operates in less than one second. When I run the code for storing each word in the tweet file (without checking for spelling) for the 1,000,000 tweets, it stores each word and its frequency in a HashMap<String, Integer> in around 20-30sec.
But when I add the line to check if the word is spelled correctly, it causes a ridiculous run time increase, to the point where I could almost watch a movie before it finished running.
The simple aspect of invoking isSpelledCorrectly(X) (which just invokes list.contains(x), which has a worst case run-time of O(n)), yet it seems quite confounding that it causes the code to go from a 30 sec runtime to a 50 min runtime?
Code:
Spelling:
static List<String> spellCheck = new ArrayList<String>();
public AssignTwo() throws IOException{
spellCheck = initCorrectSpelling("C:\\Users\\Gregs\\InfoRetrieval\\src\\english-words");
}
public static List<String> initCorrectSpelling(String filename) throws IOException { //store correct spelling of words in list
Scanner scanner = new Scanner(new FileInputStream(filename));
try{
while(scanner.hasNextLine()){
String next = scanner.nextLine();
spellCheck.add(next);
}
}
finally{
scanner.close();
}
return spellCheck;
}
public static boolean isSpelledCorrectly(String word){ //check if any given word is spelled correctly by seeing if it is
boolean output = false; //contained within the spellCheck list
if(spellCheck.contains(word)) output = true;
return output;
}
Code storing Tweets:
public static HashMap<String, Integer> misSpell;
public AssignOne() throws IOException { //read in file from path, test functions
index("C:\\Users\\Gregs\\InfoRetrieval\\src\\tweets");
}
public static void index(String filename) throws IOException {
misSpell = new HashMap<String, Integer>();
Scanner scanner = new Scanner(new FileInputStream(filename));
try{
while(scanner.hasNextLine()){
String line = scanner.nextLine();
String[] lineArr = line.split(" ");
for(int i=3; i<lineArr.length; i++){
int count=1;
lineArr[i] = lineArr[i].replaceAll("[^a-zA-Z0-9]", "");
//if(!AssignTwo.isSpelledCorrectly(lineArr[i].toLowerCase())){ //with this line commented out, runtime <30sec, with line >50mins
if(misSpell.containsKey(lineArr[i].toLowerCase())){
count = 1 + misSpell.get(lineArr[i].toLowerCase());
}
misSpell.put(lineArr[i].toLowerCase(), count);
//}
}
}
}
finally{
scanner.close();
}
}
Any suggestion on where to improve code or how to make the comparisons more efficient? Is there a faster data structure for correctly spelled words?
List.contains() is O(N), N being the number of words in the dictionary.
Use a HashSet, where contains() is O(1).
Using A buffered reader would also speed things up. And avoiding to call toLowerCase() three times on each word would too.

Java end of file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner line = new Scanner(System.in);
int counter = 1;
while (line.hasNextLine()) {
String line = line.nextLine();
System.out.println(counter + " " + line);
counter++;
}
}
}
Task: Each line will contain a non-empty string. Read until EOF.
For each line, print the line number followed by a single space and the line content.
Sample Input:
Hello world
I am a file
Read me until end-of-file.
Sample Output:
1 Hello world
2 I am a file
3 Read me until end-of-file.
If you'd like to scan from a file, you can use the below code.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(new File("input.txt"));
int counter = 1;
while (scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println(counter + " " + line);
counter++;
}
}
}
Scanner line = new Scanner(); // <-- YOUR ERROR - there is no constructor for the Scanner object that takes 0 arguments.
// You need to specify the environment in which you wish to 'scan'. Is it the IDE? A file? You need to specify that.
Since you said EOF, I'm assuming there is a File associated with this task.
Create a File object, toss that into the Scanner constructor.
File readFile = new File(PATH_TO_FILE); // where PATH_TO_FILE is the String path to the location of the file
// Set Scanner to readFile
Scanner scanner = new Scanner(readFile);
You also have a duplicate local variable named: line
I suggest you do more reading to get a grasp of how variables and objects work rather than guess or be spoonfed code you don't understand. That's how you become a strong programmer.
Documentation states that you need to pass Source to Scanner, so that it can scan from it.
To get user input then you need to use Scanner(InputStream source) constructor.
Scanner line = new Scanner(System.in);
public static void main(String[] args) {
Scanner line = new Scanner(System.in); // Added source parameter in constructor.
int counter = 1; // Initialization of counter is done outside while loop, otherwise it will always get initialized by 1 in while loop
while (line.hasNextLine()) {
String lineStr = line.nextLine(); // changed variable name to lineStr, because 2 variable can't be declared with the same name in a method.
System.out.println(counter + " " + lineStr);
counter++;
}
}
Note: Make sure you break your while loop, otherwise it will go into infinite loop.
You can't have multiple variable without the same name. You must rename one of your line variables.
When creating a scanner, you need to send in the input stream you want it to read from. System.in can server as this stream and it will read from your console. Your question though seems to indicate that you want to read from a file. If you indeed want to read from a file, you need to create the file you want to read from and send that file into the scanner to allow the scanner to read from that file.
Try:
public class Solution {
public static void main(String[] args) {
//create the File
File file = new File(filename);
//send the file into Scanner so it can read from the file
Scanner scanner = new Scanner(file);
//initialize the counter variable
int counter = 1;
//read in the file line by line
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(counter +" "+ line);
counter++;
}
}
}
To read user input, you need to use System.in in your declaration of the line object in your code:
Scanner line = new Scanner(System.in);
int counter = 0; // Initialized out of loop.
while (line.hasNextLine()) {
String ln = line.nextLine();
System.out.println(counter +" "+ln);
counter++;
}

Reading two lines from an input file using Scanner

Hi I'm in a programming class over the summer and am required to create a program that reads input from a file. The input file includes DNA sequences ATCGAGG etc and the first line in the file states how many pairs of sequences need to be compared. The rest are pairs of sequences. In class we use the Scanner method to input lines from a file, (I read about bufferedReader but we have not covered it in class so not to familiar with it) but am lost on how to write the code on how to compare two lines from the Scanner method simultaneously.
My attempt:
public static void main (String [] args) throws IOException
{
File inFile = new File ("dna.txt");
Scanner sc = new Scanner (inFile);
while (sc.hasNextLine())
{
int pairs = sc.nextLine();
String DNA1 = sc.nextLine();
String DNA2 = sc.nextLine();
comparison(DNA1,DNA2);
}
sc.close();
}
Where the comparison method would take a pair of sequences and output if they had common any common characters. Also how would I proceed to input the next pair, any insight would be helpful.. Just stumped and google confused me even further. Thanks!
EDIT:
Here's the sample input
7
atgcatgcatgc
AtgcgAtgc
GGcaAtt
ggcaatt
GcT
gatt
aaaaaGTCAcccctccccc
GTCAaaaaccccgccccc
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
gctagtacACCT
gctattacGcct
First why you are doing:
while (sc.hasNextLine())
{
int pairs = sc.nextLine();
While you have pairs only in one line not pairs and two lines of input, but number of lines once? Move reading pairs from that while looop and parse it to int, then it does not matter but you could use it to stop reading lines if you know how many lines are there.
Second:
throws IOException
Might be irrelevant but, really you don't know how to do try catch and let's say skip if you do not care about exceptions?
Comparision, if you read strings then string has method "equals" with which you can compare two strings.
Google will not help you with those problems, you just don't know it all, but if you want to know then search for basic stuff like type in google "string comparision java" and do not think that you can find solution typing "Reading two lines from an input file using Scanner" into google, you have to go step by step and cut problem into smaller pieces, that is the way software devs are doing it.
Ok I have progz that somehow wokrked for me, just finds the lines that have something and then prints them out even if I have part, so it is brute force which is ok for such thing:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class program
{
public static void main (String [] args) throws IOException
{
File inFile = new File ("c:\\dna.txt");
Scanner sc = new Scanner (inFile);
int pairs = Integer.parseInt(sc.nextLine());
for (int i = 0; i< pairs-1; i++)
{
//ok we have 7 pairs so we do not compare everything that is one under another
String DNA1 = sc.nextLine();
String DNA2 = sc.nextLine();
Boolean compareResult = comparison(DNA1,DNA2);
if (compareResult){
System.out.println("found the match in:" + DNA1 + " and " + DNA2) ;
}
}
sc.close();
}
public static Boolean comparison(String dna1, String dna2){
Boolean contains = false;
for (int i = 0; i< dna1.length(); i++)
{
if (dna2.contains(dna1.subSequence(0, i)))
{
contains = true;
break;
}
if (dna2.contains(dna1.subSequence(dna1.length()-i,dna1.length()-1 )))
{
contains = true;
break;
}
}
return contains;
}
}

Categories