Fetching the last occurence in byte array without looping - java

I've been trying to fetch the last occurrence of '\n' from a byte array of fixed size(that can be defined by me), i.e '\' & 'n' occur together.
What I tried doing is
Looping the byte array from (0 to size) and (size to 0), but I need to avoid for loops since I will be processing a lot of arrays.
Convert the byte to char and processing on that
Is there a more better solution than this? I'm a beginner to data structures and doing this in Java.
Thanks in advance.

One can't find something unless one looks. You have to examine the array to find specific bytes within it. There is no getting around that. Remember that you can break out of the loop if you start from the tail, because the first "\\n" you find will be the last occurrence in the array.
Since you've edited your question, let me add this. You can loop two-by-two, and if the byte in question is either '\\' or 'n', then you can check the next byte or the previous byte for the other character. That might be more efficient.

Related

Java: Making comparisons within a text file

I am currently writing a program where there is a text file with several million digits in it, and I have to go through it looking for a random string of 6 numbers (entered by the user). There are several constraints to this, which is making it difficult.
Must used BufferedReader
Each character can only be read once (I got it working with a bunch of nested if statements, but the way I did it violated this rule)
Cannot use any methods from the string class (so I can't put the read characters together and compare to the original string with .equals()). I have already broken up the original string into the 6 individual characters.
Not allowed to store read characters into an array of any kind, only into character variables (of which there should be 6)
Once a match has been found, it is to report the location to the user (I just need to keep a count variable that I increment with every character read) and continue on until the end of the file is reached. There can be multiple matches in the file.
Any help with this would be great, I'm at a loss for what to do.
You have a haystack to search, say 98712365478932145697, and a needle to find, say 893.
How about:
use BufferedReader.read() to read from the haystack a character at a time
if the character is the first character in your needle, store it in the first character variable
if the next character is the second character in your needle, store it in the second character variable, else, if it's the first character in your needle, start over and store it in the first character variable
if the next character is the third character in your needle, store it in the third character variable, else, if it's the first character in your needle, start over
etc
if you fill the last character variable, you have found the needle in the haystack, you can stop here or start over and look for another occurrence
I won't write the code as it's fairly trivial and this sounds like homework, but that should give you a nudge.

how to make collapse number with hypen

I want to make my number in collapse format with hypen symbol.
Suppose my input is like "1,2,4,6,7,8,9,11,12,14,16,17,18,19"
and i want output as below
"1-2,4,6,7-9,11-12,14,16-19
This sounds like a homework question to me, but I'll at least point you in the correct direction.
Personally I would probably use StringTokenizer and split the string into an integer array. Then loop through the array and checking if the contents of the current position are related to the next position or not, then find a way to save the values and print them later. You might require a second loop to print later, however I think you could be creative and find a solution that would be faster with only a single loop. ;)
If you want to get fancy, I might take a look into recursion and seeing if you can do this using a single function and a string.
Hope you find this helpful and it sends you in the right direction.
One solution would be to store all the inputs in an array, check if the next number in the array is equal to one higher than the one before and keep doing so until it isn't, when it isn't, replace the numbers in between with a "-".
Look into the .replace() method that comes with the String-class

Java : How to find string patterns in a LARGE binary file?

I'm trying to write a program that will read a VERY LARGE binary file and try to find the occurrence of 2 different strings and then print the indexes that matches the patterns. For the example's sake let's assume the character sequences are [H,e,l,l,o] and [H,e,l,l,o, ,W,o,r,l,d].
I was able to code this for small binary files because I was reading each character as a byte and then saving it in an Arraylist. Then starting from the beginning of the Arraylist, I was comparing the byte arraylist(byte[] data) with the byte[] pattern.
I need to find a way to do the same but WITHOUT writing the entire binary file in memory for comparison. That means I should be able to compare while reading each character (I should not save the entire binary file in memory). Assume the binary file only contains characters.
Any suggestions on how this can be achieved ? Thank you all in advance.
Seems like you are really looking for Aho-Corasick string matching algorithm.
The algorithm builds an automaton from the given dictionary you have, and then allows you to find matches using a single scan of your input string.
The wikipedia article links to this java implementation
Google "finite state machine".
Or, read the file one byte at a time, if the byte just doesn't match the first character of the search term, go on to the next byte. If it does match, now you're looking for the next character in the sequence. I.e., your state has gone from 0, to 1. If your state equals (or passes) the length of the search string, you found it!
Implementation/debugging left to the reader.
There are specialised algorithms for this but let's try a simple one first.
You can start with making the comparison on the fly, always after reading the next byte. Once you do that, it's easy to spot that you don't need to keep any bytes that are from earlier than your longest pattern.
So you can just use a buffer that is as long as your longest pattern, put new bytes in at one end and drop them at the other.
As I said, there are algorithms more effective than this but it's a good start.
Use a FileInputStream wrapped in a BufferedInputStream and compare each byte. Keep a buffer the length of the sequence you're looking for so you backtrack if it doesn't match at some point. If the sequence you're looking for is too large, you could save the offset and re-open the file for reading.
Working with streams: http://docs.oracle.com/javase/tutorial/essential/io/
String matching algorithms: http://en.wikipedia.org/wiki/String_searching_algorithm
Or if you just want something to copy and paste you could look at this SO question.

Android - Clean char array quickly and effectively

Is it possible to clean a char array in Java (Android) quickly and effectively? Make an other loop on the treatment seem to be to much heavy to be an optimised solution, doesn't it ?
It's strange but it is impossible to find such a solution in Java on the internet without having to subscribe to a pay site ..
Thank's to your attention
You can use:
Arrays.fill(yourArray, ' ');// or any other char instead of ' '
Arrays class belongs to the java.util package. It uses generics so you can fill any kind of array.
Why do you need to clear a char array in the first place?
Most functions that want a char array to put some data in it (IO read, etc..) return the length of data that was put in, so you can disregard the rest of the data in char array.
Use a boolean to indicate that your array is invalid.
I would like to know whether predetermine the size of an array which will be completed by various data sizes, or let it size evolve according to the reception is the best way? This is intended to have the fastest processing.
Thank's
I think the correct answer to this question is to set each character of the char[] to null character by doing:
Arrays.fill(buffer, (char)0);
Or
Arrays.fill(buffer, '\0');
Doing Arrays.fill(buffer, '0'); will fill all the char[] with the character '0' instead of the null character.

Help converting input string into Unicode integers in Java

I've been set an assignment which requires me to capture the input of 5 strings, convert them to uppercase, output them as a string, convert them to their Unicode integers (using the getNumericValue method) and then manipulate the integers using some basic operators.
I get the first part but I am having trouble with the following:
Using the getNumericValue to convert
my single character literal strings
into their Unicode integer
counterparts.
Being able to assign these ints to
variables so I can further process s
them with operators, all the
examples I have seen have been
simple printing out the number and
not assigning it to a variable,
since I am a Java noob the syntax is
still a little confusing for me.
My code is here
If there is a cleaner way of doing what I want please suggest so but without the use of arrays or loops.
I don't understand why you don't want to do this without arrays or loops.
You can get the unicode values (as ints) making up the string via String.codePointAt(), or get the characters via charAt() followed by a getNumericValue() for each character. But regardless, you're going to have to iterate over the set of characters in the string via a loop, or perhaps recursion.
Yes, sounds like op needs to continue researching avenues of learning.
// difficult to code without interfering with
// instructor's prerogative
char ( array ) = String.getchars();
// Now what, unroll the loop?
...
As noted by Brian, loops are fundamental. I cannot imagine getChars being assigned before simple array techniques.

Categories