Split a text file into blocks - java

I need to read a text file, and break the text into blocks of 6 characters (including spaces), pad zeroes to the end of text to meet the requirement.
I tried doing it and here is what I have done.
File file = new File("Sample.txt");
String line;
try {
Scanner sc = new Scanner(file);
while(sc.hasNext()){
line = sc.next();
int chunk = line.length();
int block_size=6;
if((chunk%block_size) != 0)
{
StringBuilder sb = new StringBuilder(line);
int val = chunk%block_size;
for(int i=0; i<val; i++){
sb.append(" ");
}
line = new String(sb.toString());
}
int group = line.length() / block_size;
String[] b = new String[group];
System.out.println(line);
System.out.println(chunk);
int j =0;
for(int i=0; i<group;i++){
b[i] = line.substring(j,j+block_size);
j += block_size;
}
System.out.println("String after spliting is: ");
for(int i=0; i<group;i++){
System.out.println(b[i]);
}
}
}
Now this works fine when the text in the input file has no spaces between words. But when I add spaces gives me a different output. I am stuck up at this point. Any suggestions on the same ?

I don't want to write the solution for you, but I'd advise you that what you're trying to accomplish might be easier to do using a BufferedReader with a FileReader and by using Reader.read(buf) where buf is a char[6];

Related

Trouble with printing from a text file with multiple lines of data

I have a text file with multiple lines of numbers like this:
0.0336 0.0243 0.0261
0.0075 0.1788 0.0669
I need to make a Java program to reformat them to one number per line:
0.0336
0.0243
0.0261
0.0075
0.1788
0.0669
Here is my code and it does not work:
while (scanner.hasNext())
{
String[] arr = scanner.nextLine().split("\\s+");
for(int i =0; i< arr.length; i++){
System.out.println(arr[i]);
}
}
This code results in an extra line whenever there is a new line, for example:
0.0336
0.0243
0.0261
//extra line here, which should be ignored
0.0075
0.1788
0.0669
Is there a way to ignore the line?
I tried the same you did it's working fine on my text file. I did this
BufferedReader source = new BufferedReader(new FileReader("/path/of/file/stack.txt"));
scanner = new Scanner(source);
while (scanner.hasNext()) {
String[] arr = scanner.nextLine().split("\\s+");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}

Java Processing input from a file

So I am doing this past sample final exam where the question asks to read input from a file and then process them into words. The end of a sentence is marked by any word that ends with one of the three characters . ? !
I was able to write a code for this however I can only split them into sentences using scanner class and using use.Delimiter. I want to process them into words and see if a word ends in the above sentence separator then I will just stop adding words into the sentence class.
Any help would be appreciated as I am learning this on my own and this is what I came up with. My code is here.
File file = new File("finalq4.txt");
Scanner scanner = new Scanner(file);
scanner.useDelimiter("[.?!]");
while(scanner.hasNext()){
sentCount++;
line = scanner.next();
line = line.replaceAll("\\r?\\n", " ");
line = line.trim();
StringTokenizer tokenizer = new StringTokenizer(line, " ");
wordsCount += tokenizer.countTokens();
sentences.add(new Sentence(line,wordsCount));
for(int i = 0; i < line.replaceAll(",|\\s+|'|-","").length(); i++){
currentChar = line.charAt(i);
if (Character.isDigit(currentChar)) {
}else{
lettersCount++;
}
}
}
What I am doing in this code is that I am splitting the input into sentences using the Delimiter method and then counting the words, letters of the entire file and storing the sentences in a sentence class.
If I want to split this into words, how can I do that without using the scanner class.
Some of the input from the file that I have to process is here:
Text that follows is based on the Wikipedia page on cryptography!
Cryptography is the practice and study of hiding information. In modern times,
cryptography is considered to be a branch of both mathematics and computer
science, and is affiliated closely with information theory, computer security, and
engineering. Cryptography is used in applications present in technologically
advanced societies; examples include the security of ATM cards, computer
passwords, and electronic commerce, which all depend on cryptography.....
I can further elaborate on this question if it needs explanation.
What I want to be able to do is to keep adding words to the sentence class and stop if the word ends in one of the above sentence separator. And then read another word and keep adding the words until I hit another separator.
The snippet below shall work
public static void main(String[] args) throws FileNotFoundException {
File file = new File("final.txt");
Scanner scanner = new Scanner(file);
scanner.useDelimiter("[.?!]");
int sentCount;
List<Sentence> sentences = new ArrayList<Sentence>();
while (scanner.hasNext()) {
String line = scanner.next();
if (!line.equals("")) { /// for the ... in the end
int wordsCount = 0;
String[] wordsOfLine = line.split(" ");
for (int i = 0; i < wordsOfLine.length; i++) {
wordsCount++;
}
Sentence sentence = new Sentence(line, wordsCount);
sentences.add(sentence);
}
}
}
public class Sentence {
String line = "";
int wordsCount = 0;
public Sentence(String line, int wordsCount) {
this.line = line;
this.wordsCount=wordsCount;
}
You can use a buffered reader to read every line of the file. Then split every line into a sentence with the split method and finally to get the words just split the sentence with the same method. In the end it would look something like this:
BufferedReader br;
try{
br = new BufferedReader(new File(fileName));
}catch (IOException e) {e.printStackTrace();}
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null){
sb.append(line);
}
String[] sentences = sb.toString().split("\\.");
for(String sentence:sentences){
String word = sentence.split(" ");
//Add word to sentence...
}
try{
br.close();
}catch(IOException e){
e.printStackTrace();
}
Okay so i have been solving this question through several techniques and one of the approach was above. however i was able to solve this with another approach as well which does not involve using Scanner class. This one was much more accurate and it gave me the exact output whereas in the above i was off by a few words and letters.
try {
input = new BufferedReader(new FileReader("file.txt"));
strLine = input.readLine();
while(strLine!= null){
String[] tokens = strLine.split("\\s+");
for (int i = 0; i < tokens.length; i++) {
if(strLine.isEmpty()){
continue;
}
String s = tokens[i];
wordsJoin += tokens[i] + " ";
wordCount += i;
int len = s.length();
String charString = s.replaceAll("[^a-zA-Z ]", "");
for(int k =0; k<charString.length(); k++){
currentChar = charString.charAt(k);
if(Character.isLetter(currentChar)){
lettersCount++;
}
}
if (s.charAt(len - 1) == '.' || s.charAt(len - 1) == '?' || s.charAt(len - 1) == '!') {
sentences.add(new Sentence(wordsJoin, wordCount));
sentCount++;
numOfWords += countWords(wordsJoin);
wordsJoin = "";
wordCount = 0;
}
}
strLine = input.readLine();
}
This might be useful for anyone doing the same problem or just need an idea of how to count letters, words and sentences from a text file.

How to iterate over String(char[])

I read the content of one file into String(char[]) content:
8264,1
28462,1
15836,1
Then I want to multiply second column by 5 and append another file with processed values:
8264,5
28462,5
15836,5
The problem is with iterating over content. If I iterate as shown below, then obviously I get chars like 8, 2, 6, 4, etc., but not lines 8264,5.
FileWriter fw = new FileWriter(filew.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
StringBuilder sb = new StringBuilder();
for (int k = 0; k < content.length(); k++)
{
char c = content.charAt(k);
//multiply second column by 5
}
If you want to iterate line by line I would recommend a Scanner.
PrintWriter pw = new PrintWriter(...file/stream...);
Scanner sc = new Scanner(...your file/stream/string...);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] parts = line.split(",");
int first = Integer.parseInt(parts[0]);
int second = Integer.parseInt(parts[1]);
second *= 5;
pw.println(first + "," + second);
}
sc.close();
pw.close();

How to read from a specific line of a file

I have a text file (called data.txt, which has 350 lines)
How can I find the text on a given line of the file? For example:
int imageVariable = 5;
String imageText = nthLineOfFile(imageVariable);
textView1.setText(imageText);
I'm trying to write the String nthLineOfFile(int image) function.
Thanks
You may try this:
String str = FileUtils.readLines(file).get(lineNumber);
or you may use the conventional way by using BufferedReader class:
BufferedReader r = new BufferedReader(new FileReader(file));
for (int i = 0; i < lineNumber - 1; i++)
{
r.readLine();
}
return r.readLine();
You can use Scanner:
Scanner fileIn = new Scanner(your file);
for (int i = 0; i < lineNum - 1; i++) {
fileIn.nextLine(); // ignore
}
nthLine = fileIn.nextLine();

Stdin reading multiple lines

I have the following code:
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String submittedString = "";
System.out.flush();
submittedString = stdin.readLine();
int numberofLines = Integer.parseInt(submittedString.split(" ")[0]);
for(int i = 0; i < numberofLines; i++)
submittedString += stdin.readLine();
zipfpuzzle mySolver = new zipfpuzzle();
mySolver.getTopSongs(submittedString);
However, despite the input being over multiple lines, this only reads the first.
Where is my mistake?
If it makes any difference, I am compiling on eclipse.
Cheers!
Dario
Just use an array and populate it within your for-loop:
String[] inputs = new String[numberofLines];
for (int i = 0; i < numberofLines; i++)
inputs[i] = stdin.readLine();
Extra Note:
If you want multiple lines with single String:
String submittedString = "";
for (int i = 0; i < numberofLines; i++)
submittedString += stdin.readLine() + System.getProperty("line.separator");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String line = "";
while ((line = stdin.readLine()) != null){
// Do something.
submittedString += line + '\n';
}
submittedString = stdin.readLine();
BufferedReaders readLine method will read System.in until it hits a new line, therefore if you're using the first line of the file to determine the number of lines to read then your data must be incorrect.

Categories

Resources