Wrong Number of Characters (String Munipulation from File) -- Java - java

I am currently testing out a program who's purpose is to import a file and find the number of characters in that file using string manipulation.
I am currently using System.out.println(fileone.length()); to do so. However each time I try and run the program it reads the number of characters wrong, in fact it gives the same wrong number every time. Down below is the output of the program.
My name is Sam Tyler 54
As you can see the words in the file are displayed and next to that is the character number which is obviously wrong, as I counted myself, it should be 19, including spaces.
Here is the code that is reading the file and giving the character number.
Can you see any problems?
Any help is much appreciated.
Scanner ourScanner = new Scanner(new File(fileone));
System.out.println();
while(ourScanner.hasNextLine())
{
System.out.print (ourScanner.nextLine() + "\t");
count++;
if (count%4 == 0)
System.out.println();
}
}
//System.out.println ("\n\n" + count + " \ntotal strings found.");
System.out.println(fileone.length());

You are checking the length of the String representing the file path (fileone), not the actual file scanned, so it will be invariant given the context.
If you're counting the number of lines, just print your count variable.
You also have a number of ways to check for file length from a File object , or for specific character length (since the file system may allocate more space than actually used for the file), you can count them through iteration with a FileReader, etc.

fileone.length() gives you the length of the path of file.
But new File(fileone).length() will give you the file size in number of bytes (assuming one character is one byte)

Try this example:
1: read all the lines of the file
2: convert to a string
3: remove the white spaces
4: display the length in byte for the resulting string
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("C:\\Users\\xxx\\test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer fileContents = new StringBuffer();
String line = br.readLine();
while (line != null) {
fileContents.append(line);
line = br.readLine();
}
String sValue = fileContents.toString();
//remove all whitespace
sValue = sValue.replaceAll("\\s+","");
System.out.println(sValue.length());
}
}
Test:
test.txt content: My name is Sam Tyler 54 --> 18 characters
result:
18

Related

Java search text file for string and replace or remove a character within the string

I'm working with a large text file that has thousands of lines of text that contain some information. Sometimes the software will randomly add a character in place which will fail our upload. I'm trying to create a program that finds a string that is hard coded and then searches within the line and replaces or removes the invalided character. Here is some of the contents of the text file.
MTR17000386001000000000000000RA 124359
00010000004000000040000000 000NN NNE 000 N
RDG17000386
KWHL000000000R00000100000059534000405162019075929N000400000010N8486
000010500R 00000010010000059226
RFF1700038652126007 ERT
0000000952.0062500070014051620190759290005953476Type 7 0000N 6
MTR17000386001000000000000000RA 114818
00010000005000000050000000 000NN NNE 000 N
RDG17000386
DMDL000000000R000001000.0072666035305162019112344N000100000010N8486
005180500R 00000010010000072666
RFF1700038611861733 ERT
0000000952.0062500070000051620191123440007266680Type 7 0000N 6
On the line of RDG17000386 DMD you can see that there is a period. The period is not supposed to be there and needs to be replaced or removed from the file.
In my current code I"m searching each line that start with "RDG" and it works find, but I want to limit the search with only the lines that include "DMD" within the line and I've tried changing the RDG to DMD, which didn't work because the line doesn't start with DMD. I'm not entirely sure how to remove or replace the period from the file. Here is what my code looks like so far.
import java.io.*;
public class ReadLineAndReplace {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("FilePath"));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
if (line.startsWith("RDG")) {
//assuming the replace would be inserted here
System.out.println(line);
}
}
}
}
Use. matches() with a regular expression
line.matches("^RDG.*DMD.*")
to replace the dot with a zero
if (line.matches("^RDG.*DMD.*")) {
line = line.replace('.', '0')
}

Result of Java split() is varies when working with string of numbers

Why does Java String.split() generate different results when working with string defined in code versus string read from a file when numbers are involved? Specifically I have a file called "test.txt" that contains chars and numbers separated by spaces:
G H 5 4
The split method does not split on spaces as expected. But if a string variable is created within code with same chars and numbers separated by spaces then the result of split() is four individual strings, one for char and number. The code below demonstrates this difference:
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
public class SplitNumber {
//Read first line of text file
public static void main(String[] args) {
try {
File file = new File("test.txt");
FileReader fr = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fr);
String firstLine;
if ((firstLine = bufferedReader.readLine()) != null) {
String[] firstLineNumbers = firstLine.split("\\s+");
System.out.println("First line array length: " + firstLineNumbers.length);
for (int i=0; i<firstLineNumbers.length; i++) {
System.out.println(firstLineNumbers[i]);
}
}
bufferedReader.close();
String numberString = "G H 5 4";
String[] numbers = numberString.split("\\s+");
System.out.println("Numbers array length: " + numbers.length);
for (int i=0; i<numbers.length; i++) {
System.out.println(numbers[i]);
}
} catch(Exception exception) {
System.out.println("IOException occured");
exception.printStackTrace();
}
}
}
The result is:
First line array length: 3
G
H
5 4
Numbers array length: 4
G
H
5
4
Why do the numbers from the file not get parsed the same as the same string defined within code?
Based on feedback I changed the regex to split("[\\s\\h]+") which resolved the issue; the numbers for the file were properly split which clearly indicated that I had a different whitespace-like character in the text file that I was using. I then replaced the contents of the file (using notepad) and reverted back to split("\\s+") and found that it worked correctly this time. So at some point I must have introduced different white-space like characters in the file (maybe a copy/paste issue). In the end the take away is I should use split("[\\s\\h]+") when reading from a file where I want to split on spaces as it will cover more scenarios that may not be immediately obvious.
Thanks to all for helping me find the root cause of my issue.

Read specific characters from specific line of text in java

I am working on a program that involves me having to search a specific line in a .txt file and convert the string inside of it into something else.
For example, the string is actually made of numbers which I suppose I can convert into ints. The main thing is that for example, on line 2, there are 5 digits for zip code stored. I need to convert that into certain outputs, depending on the numbers. In other words, I need variables from digits 0-9 and depending on each digit, output a specific output.
Right now here is the code I have to prompt the user for information that is stored in the file, and can read and print all of the information that was just typed, but I'm unsure how to go about the rest.
import java.io.*;
import java.util.*;
import java.io.FileReader;
import java.io.IOException;
public class ObjectTest2 {
public static void main(String [] args) throws FileNotFoundException, IOException {
// The name of the file to open.
String fileName = "information.txt";
Scanner myScanner = new Scanner(System.in);
try {
// Assume default encoding.
FileWriter fileWriter =
new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
// append a newline character.
//This shit here prompts the user for information and stores it in seperate lines to be
//called on by the later section.
System.out.print("What is your name? ");
bufferedWriter.write(myScanner.nextLine());
bufferedWriter.newLine();
System.out.print("What is your 5 digit zip code?");
bufferedWriter.write(myScanner.nextLine());
bufferedWriter.newLine();
System.out.print("What is your +4 digit zip? ");
bufferedWriter.write(myScanner.nextLine());
bufferedWriter.newLine();
System.out.print("What is your address? ");
bufferedWriter.write(myScanner.nextLine());
// Always close files.
bufferedWriter.close();
//reads the information file and prints what is typed
BufferedReader reader = new BufferedReader(new FileReader("information.txt")); {
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
}
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
You are left with no choice but to iterate over each line of the file and search for the String. If you want to get a line of string from the file based on line number, consider creating a method. If the operation is required to be performed several times on the same file and if the contents of the file do not change, use a map to cache the file contents based on the line number.

Copy part of line from 1 text to 2nd text file

I am working on project where I need to copy some part of each line from 1st text file to other. In the first text each data is separated by splitter --# (which i have used).
I want to get the 1st two parts of actual 4 parts total of 3 splitters Ex:
Hello--#StackOverflow--#BMWCar--#Bye.
I just want to fetch 1st 2 parts .ie.
Hello--#StackOverflow
from all the lines of first text file to second text file. I have tried everything and could not get it to work. Please help me out of this. :)
I am little late, but below code will work as well :
String str = "Hello--#StackOverflow--#BMWCar--#Bye.";
String strResult = str.split("(?<!\\G\\w+)(?:--#)")[0];
System.out.println(strResult);
\G is previous match, (?<!regex) is negative lookbehind.
[Update]
In your case, can we use below code? The solution is based on the file you provided
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test{
public static void main (String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new FileReader("abc.txt"));
String line;
while((line = in.readLine()) != null)
{
System.out.println(line.substring(0,line.lastIndexOf("--#")));
}
in.close();
}
}
For each line you read do the following:
String[] sp = line.split("--#",2);
String result = sp[0]+"--#"+sp[1];
If you're expecting names with dashes, it seems to me the best thing to use is String.split:
String test = "Hel-lo--#StackOverflow--#BMWCar--#Bye";
String[] sp = test.split("--#");
for(String name : Arrays.copyOfRange(sp, 0, 2))
{
System.out.println(name);
}
First, to get all the lines in the text file, try using a while loop. As you are reading in from one file, output to the second file. Split each line based on the conditional.
String inLine = in.nextLine();
String[] parts = inLine.split("--#");
String toWrite = parts[0] + " " + parts[1];
outLine.write(toWrite);
Should be easy enough to figure out.

Reading data from a table & parsing it

My code is designed to read the contents of a text file and check if the contents are entered in a format that is as follows:
john : martin : 2 : 1
and if that format is followed then it will output it in the format:
john [2] | martin [1]
or else it will be counted as an invalid result and the total numbers will not be added to it whereas if the results are in the format then they will get added to the total so with the example it would display the number of vaild results as 1, invalid as 0 and total number as 3.
My question is that my code doesn't work properly as I get this error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at reader.main(reader.java:33)
So how would I go about fixing this and reading and displaying the data in thee way that I want? Thanks in advance.
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.Scanner;
public class reader {
/**
* #param args
* #throws FileNotFoundException
* #throws FileNotFoundException
* #throws FileNotFoundException when the file cannot be loaded
*/
public static void main(String[] args) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
Scanner s = new Scanner(new BufferedReader(new FileReader("results2.txt"))).useDelimiter(":");
// create a scanner which scans from a file and splits at each colon
while ( s.hasNext() ) {
hteam = s.next(); // read the home team from the file
ateam = s.next(); // read the away team from the file
hscore = s.nextInt(); //read the home team score from the file
ascore = s.nextInt(); //read the away team score from the file
System.out.print(hteam); // output the line of text to the console
System.out.print(hscore);
System.out.print(ateam);
System.out.println(ascore);
}
System.out.println("\nEOF"); // Output and End Of File message.
}
}
You're looking for s.next() instead of s.nextLine().
hteam = s.nextLine() reads the entire line "john : martin : 2 : 1", leaving nothing left for ateam.
Edit:
As you've said this still isn't working, I'd guess that you have an extra newline at the end of your input file, which is causing s.hasNext() to evaluate to true. This would cause the Scanner to trip up when it's getting the next input line.
Try Scanner s = new Scanner(System.in).useDelimiter("\\s*:\\s*|\\s*\\n\\s*"); to read multiple lines.
See implementation: http://ideone.com/yfiR2S
To verify that a line is in the correct format, I'd (with inspiration from osoblanco's answer) check that there are 4 words and that the last two are integers:
public static boolean verifyFormat(String[] words) {
// see endnote for isInteger()
return words.length == 4 && /*isInteger(words[2]) && isInteger(words[3])*/;
}
public static void main(String[] args) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
Scanner s = new Scanner(new BufferedReader(
new FileReader("results2.txt"))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
while (s.hasNext()) {
String line = s.nextLine();
String[] words = line.split("\\s*:\\s*");
if(verifyFormat(words)) {
hteam = words[0]; // read the home team
ateam = words[1]; // read the away team
hscore = Integer.parseInt(words[2]); //read the home team score
ascore = Integer.parseInt(words[3]); //read the away team score
System.out.print(hteam); // output the line of text to the console
System.out.print(hscore);
System.out.print(ateam);
System.out.println(ascore);
}
}
System.out.println("EOF");
}
isInteger() can be found here.
I think scanning isn't quite what you want here. I would just use a BufferedReader and do ReadLine to handle 1 line each time through the for loop.
Then verify each line by the following:
1) String.split(":") and verify 4 pieces.
String [] linePieces = nextLine.split(":");
if(linePieces.length!=4)
{
//mark invalid, continue loop
}
2) Trim each piece
for(int i =0; i<4; i++)
linePieces[i] = linePieces[i].trim();
3) Verify piece 3 and piece 4 are numbers, Integer.parseInt with try/catch. In the catch block, count that the line is invalid.
try
{
name1=linePieces[0];
name2=linePieces[1];
score1=Integer.parseInt(linePieces[2]);
score2=Integer.parseInt(linePieces[3]);
//count as success and do logic
}catch(NumberFormatException e){
//invalid line
}

Categories