How To Recursively Find Given String In A File
Oct 19, 2014
I have to recursively find a given string in a file. I HAVE to use the LineNumberReader class, and the output would be like so:
Line#Found : the string of the whole line
This is the code I've written:
public String findGivenString(String givenString, int currentLineNumber) {
LineNumberReader lnr = null;
try {
lnr = new LineNumberReader(new FileReader(getFile()), 4096);
lnr.setLineNumber(currentLineNumber);
String s = lnr.readLine().toLowerCase();
[Code] ....
I messed around with a bit, and it doesn't change to the new set line. Though the line number is incrementing! So it just keeps checking the first line of the file over and over again, which is why it can't find the given string. Which also throws the StackOverFlow exception I'm getting.
Here's the output if I remove the comment from the System.out...:
String @ that Line# 1: package banking;
String @ that Line# 2: package banking;
String @ that Line# 3: package banking;
String @ that Line# 4: package banking;
....
So you see it keeps checking the same line even though the line number IS incrementing.
View Replies
ADVERTISEMENT
May 25, 2015
Trying to find cheapest path recursively, given a grid a integers trying to recursively find the cheapest path from the top to the bottom. cheapest being the sum of the path. I think I've made my code over complicated. recursive things are usually much more elegant
import java.io.FileInputStream;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class trial {
public void readFile(String fileName)
[Code] ....
and here is the grid
27 19 18 85 32 11 18 24 22 98
60 83 52 61 18 64 74 33 95 42
56 27 71 56 65 70 18 78 35 74
15 89 19 92 61 76 92 42 57 26
88 28 78 45 21 98 11 72 82 97
49 54 88 79 16 43 27 78 52 71
17 18 60 40 72 39 70 52 96 11
62 79 25 50 73 40 98 64 44 72
25 79 72 25 64 35 29 16 77 96
12 93 49 64 61 34 83 87 34 36
View Replies
View Related
May 8, 2015
I have a case: i have to make fast searching of a file in to all file systems(Linux or Windows). I use search directories recursively for file. Is there a quick way?
View Replies
View Related
May 29, 2014
Java Code:
if(Directory.getText().matches("")){
JOptionPane.showMessageDialog(null, "Please search for the directory");
}
else if(fileName.getText().matches("")){
[Code].....
This is actually my question:
Java Code:
1. billing-20140527[09].txt has
a)XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
b)XGMS,2034-05-27 30:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
2. billing-20140527[10].txt has
a)XCGS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
b)HELO
[B]I try to find the number 1 in both text files, if lets say I input the text file name is billing, I can find the number 1 in both text file and output them:[/B]
a) XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
b) XCGS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
[B]However, if I specify the text file name: billing-20140527[09].txt and find the number 1 inside the text file, it will only output:[/B]
a) XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040. mh_sh_highlight_all('java');
So far, I can't get this program to run,
View Replies
View Related
May 4, 2014
I have included split() to put a string read from a given file into indexed array. Looking for a word position (not char position number in addition to the line number I have already written. Line number works fine, however word position isn't quite right.Below is my code:
import java.io.*;
public class Word implements Comparable, TreeComparable{
String word;
int count;
int wordpos;
ObjectList lines;
private SuperOutput so;
[code]....
View Replies
View Related
May 5, 2014
My requirement is to find the line number using multiline string. Here I need to extract the string between FROM and where clause(from the below string) and need to find the line number in the file
SELECT HL.LOCATION_ID,HPS.PARTY_SITE_ID,HCAS.CUST_ACCT_SITE_ID
INTO LN_SITE_LOCATION_ID,LN_LOC_PARTY_SITE_ID,LN_CUST_ACCT_SITE_ID
FROM HZ_LOCATIONS HL,
HZ_PARTY_SITES HPS,
[Code]....
View Replies
View Related
Oct 17, 2014
i tried everything but its giving me errors. i tried the for loop but its giving me something else.
this is what i have to do Write a recursive method that prints out the data elements of a linked list in reverse order.
Your method should take in as a parameter the head reference to a linked list. Then, write a recursive method that returns a count of the number of elements greater than a given threshold. You may assume that the data elements in the linked lists are ints. The parameters of your method are a reference to the linked list and a int value representing the threshold.
public class recursion3
{
public static void main(String [] args) {
char a [] = {'A', 'B','C','D','E'};
System.out.println(a);
}
public static String reverseString(String s) {
if (s.length() <= 1) {
[code]....
View Replies
View Related
Nov 1, 2014
I just can't seem to figure it out how to solve this inherently recursive assignment.The task is to check a directory path and read the files within that path; also, if there are more directories within it, we have to go deeper into those directories to read the files within them - if any. What you're looking at is my skeleton of the assignment:
public class SearchingForStrings {
public static void main(String[] args) {
String path = "."; // Default
File sf = new File(path);
String mainDirectory = args[0]; // These two are just
String keyString = args[1]; // command-line arguments
countLinesWithString(sf, mysteriesDirectory, keyString);
[code]....
View Replies
View Related
Jan 10, 2015
I need to make some operations with a file. I struggle to understand why Apache is throwing this error:
(The system cannot find the file specified)
My file is located in the root directory of the project.
And here is how I indicate the path to it:
String filePath = "default.jpg";
InputStream inputStream = new FileInputStream(new File(filePath));
If I put the file in the Eclipse folder, it works... But I need it to work if I put it in my project's root folder.
View Replies
View Related
Apr 30, 2015
It was quite recently that Data Structures was introduced to me, so I started out writing some iterative programs recursively.I found some strange output which shouldn't have come out but if you take a look at these three codes
long factorial(long n)
{
if(n == 1)
{
return 1;
}
else
{
result = n*factorial(n-1);
[Code] ....
These are three versions of the code, achieving the same objective of obtaining a given number and returning the factorial, but in spite of the changes made to the code, they produce the same result. I needed a reason as to why it is so? I tried to dry run all the codes but at some point or the other I got confused, and had to start all over again and couldn't come up with a proper result.
View Replies
View Related
Mar 23, 2015
My program is supposed to use recursion and iteration to sum and reverse elements in an array of Longs. The array is supposed to be 1000000 cells and I am supposed to compute the average times of 99999 trials. When I try to run the program, it keeps saying running.... but doesnt ever do anything.
public class PA2Delegate {
private long[] start;
private long[] end;
private Long reversal;
private Long sum = (long)(int)0;
[Code] .....
View Replies
View Related
Feb 28, 2015
I want to reverse a String that is input
This is what I have tried so far
public class ReverseStringTest {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter String to replace:");
String temp=in.next();
System.out.println("Reverse String is:"+reverseString(temp));
[code]...
But its not working it is return same string that is Input.
View Replies
View Related
Apr 8, 2014
like i have String s="11222ddeefs"
so here i want program output like 1=2
2=3
d=2
e=2
f=1
and s=1
it has to show no of duplicates in each character in string
View Replies
View Related
Jun 17, 2014
Writing a Java program to find sum of numbers in a random string.
(Input=abc235^%$q!12&3 output=250)
If a number has - sign then u should consider it as a negative number.(Input=abc-12t%^&$dcf22 Output=10)
View Replies
View Related
Sep 17, 2014
i need to find common String between to Strings :
Example 1:
Customer Name 1: Dr. Joe Smith
Customer Name 2: Joseph Smith, MD.
I need to search the string for a match, in this example "Smith"
Example 2:
Customer Name1: New York Market Place
Customer Name 2: NY Marketplace
I need to search the string for a match, in this example Market place
Example 3:
Customer Name1: The Deli on the Corner
Customer Name 2: Corner Deli
I need to search the string for a match, in this example Deli Corner
View Replies
View Related
Nov 21, 2014
SimpleDotComTestDrive.java:8: error: cannot find symbol
String result = dot.checkYourself(userGuess);
^
symbol: method checkYourself(String)
location: variable dot of type SimpleDotCom
1 error
Code:
public class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;
public void setLocationCells(int[] locs) { locationCells = locs; }
public String checkYourSelf(String stringGuess)
{ int guess = Integer.parseInt(stringGuess);
String result = "miss"; for (int cell : locationCells) {
[Code] .....
View Replies
View Related
Apr 10, 2014
How can find any string length without using any library method??
View Replies
View Related
Nov 29, 2014
I have got a pretty good framework working for my hangman game so far, however I am quite stumped on how to find multiple instances of the same letter in a single string. I am using indexOf to find the instance of a character in a string, but it only returns the first instance. So if the words(s) contain(s) multiple instances it doesn't register the rest.
public static void main(String[] args) {
String word = "crazy horse";
String answer="";
String space=" ";
String dash="-";
[code]....
View Replies
View Related
Apr 17, 2014
I'm getting this -file not found- error despite the fact that I have saved the file with appropriate permissions in the src folder of the given application in netbeans. The program prompts for a listings.txt file to read and write data to a new file that counts and sums the info in a overview.txt report. Here is the stacktrace:
run:
Type in the name of the file: listings
There was a problem:java.io.FileNotFoundException: listings.txt (The system cannot find the file specified)
java.io.FileNotFoundException: listings.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:131)
at java.io.FileInputStream.<init>(FileInputStream.java:87)
at java.io.FileReader.<init>(FileReader.java:58)
at kettask2a.KetTask2a.main(KetTask2a.java:48)
[code]....
View Replies
View Related
Feb 21, 2014
I'm having issues with my bottom loop trying to read in a large *.txt* file but how can I do a check to see if its at the end of the document?
import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
[code]...
View Replies
View Related
Feb 2, 2015
I'm trying to find a specific file that already exists on the computer. I want to find that file and zip it. don't get sidetracked with whether or not the file exists, because I'm sure it does on my local machine and TDD will address that what-if scenario later.
My code and JUnit is below. Since its not finding the file, I assume I must be formatting 'path' wrong. I wonder if I'm even on the right track logically.
private ZipParameters zp = new ZipParameters();
private ZipFile zipFile;
public ZipFile createZipFile(File f, String path) throws ZipException{
zipFile = new ZipFile(path);
zp.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zp.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
[Code] ....
View Replies
View Related
Sep 2, 2014
I'm trying to read a .xlsx file using Apache Poi on Eclipse and store the data into an ArrayList.
My code :
import org.apache.poi.ss.usermodel.*;
import java.io.*;
import java.util.*;
public class parseReadFiles
{
public static void main(String[] args) {
[Code] ....
I keep getting a FileNotFoundException: Users/Divjot/Desktop/first.xlsx (No such file or directory). I've tried different combinations of file names and paths but can't get the program to find it. Should the file be stored in a special place or should the class path be different?
View Replies
View Related
Jun 24, 2014
First of all, i am using ubuntu and jdk8. My problem: displaying the current path of a file in my system Approach: I have a file called dummy.txt in a given directory which have enough permissions and i did the following:
File file=new File("dummy.txt");
System.out.println(file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("/")));
I expected to see displayed the current path of the file without the name of the file but it is showing a different path. I just want to display the current path of the file without the name.
View Replies
View Related
Aug 28, 2014
I am trying to figure out how to report the high and low scores with names from a file. The format is like:
6352 J@me$$
663843 BOBBBB1
etc...
This is my code so far.
import java.io.*;
import java.util.*;
public class Assignment1
{
public static void main(String[] args)throws IOException
[code]....
View Replies
View Related
Feb 13, 2014
I am working through a text and I am supposed to complete the following tasks.
Your ReadFiles.java class requires the following methods:
Method: check to see if the file exists
Method: find number of rows in csv file
Method: Converts the csv file to a mutli-dimensional array
Method: PrintArray
Method: Return array using a get method
Create a file DataAnalyzer.java. This file will be used to call the methods in ReadFiles.java. Be sure to demonstrate that all of your methods work through DataAnalyzer.java.
The problem is that it does not really provide any information on how to go about reading a file into an array. I am stuck at the third task of converting the file to an array and I have tried several ways to do this unsuccessfully. I thought that I would at least try to get things to print out (line 87) to see if I could get that to work, but all that prints in null over and over again.
Java Code:
public class DataAnalyzer {
public static void main (String[] args) {
ReadFiles aReadFiles = new ReadFiles();
aReadFiles.fileCheck();
aReadFiles.findRows();
aReadFiles.convertFile();
[Code] .....
View Replies
View Related
Oct 9, 2013
class mmm {
public static void main(String[] args) {
OcrEngine ocr = new OcrEngine();
ocr.setImage(ImageStream.fromFile("pp.tif"));
// ocr.setImage((IImageStream) new File("pp.tif"));
[Code] ....
In above code giving error .
Exception in thread "main" com.aspose.ms.System.IO.FileNotFoundException: Can't find file: pp.tiff.
Where i am wrong. I am trying to pass all formt(.png,.gif,.jpg) images. I am giving proper path ....
View Replies
View Related