Java automatic filling of ArrayList, searching for better options - java

I'm using this code here to automatically fill a string array list with the directory path of obj files for later use in animations, but there's a small problem with this:
private List<String> bunny_WalkCycle = new ArrayList<String>();
private int bunny_getWalkFrame = 0;
private void prepare_Bunny_WalkCycle() {
String bunny_walkFrame = "/bunnyAnimation/bunnyFrame0.obj";
while(bunny_WalkCycle.size() != 30) { // 30 obj files to loop through
if(bunny_getWalkFrame != 0) {
bunny_walkFrame =
"/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_"+bunny_getWalkFrame+".obj";
}
bunny_WalkCycle.add(bunny_getWalkFrame);
bunny_getWalkFrame++;
}
}
Now the problem is that the naming convention in blender for animations has zeros before the actual numbers, so something like this:
bunnyWalkFrame_000001.obj
bunnyWalkFrame_000002.obj
bunnyWalkFrame_000003.obj
...
bunnyWalkFrame_000030.obj
With my prepare_Bunny_WalkCycle method I cannot account for the zeros so I would change the names and get rid of the zeros.. This may be okay for not so many frames but once I hit 100 frames it would get painfull.. So there's my question:
What would be an intelligent way to account for the zeros in the code instead of having to rename every file manually and remove them?

I think you can solve your problem with "String.format":
String blenderNumber = String.format("%06d", bunny_getWalkFrame);
Explanation:
0 -> to put leading zeros
6 -> "width" of them / amount of them
And so this would be your new bunny_walkFrame:
bunny_walkFrame = "/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_" + blenderNumber + ".obj";

You can use String.format() to pad your numbers with zeros:
String.format("%05d", yournumber);

Here are two options. First, you can use a string formatter to create your filenames with leading zeros:
bunny_WalkCycle.add("/bunnyAnimation/bunnyFrame0.obj");
for (int frame = 1; frame <= 30; frame++) {
bunny_WalkCycle.add(
String.format("/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_%06s.obj", frame));
}
The second option is, if you already have all the required files in the directory, you can get them all in one go:
bunny_WalkCycle.add("/bunnyAnimation/bunnyFrame0.obj");
bunny_WalkCycle.addAll(Arrays.asList(new File("/bunnyAnimation/bunnyWalkAnim").list()));

There are two ways you could do that:
Appending the right number of leading zeroes, or using a String formatter.
bunny_walkFrame = "/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_" + String.format("%05d", bunny_getWalkFrame) + ".obj";
OR
bunny_walkFrame = "/bunnyAnimation/bunnyWalkAnim/bunnyWalkFrame_" + getLeadingZeroes(bunny_getWalkFrame) + String.valueOf(bunny_getWalkFrame) + ".obj";
where
private String getLeadingZeroes(int walk) {
String zeroes = "";
int countDigits = 0;
while (walk > 0) {
countDigits++;
walk /= 10;
}
for (int i = 1; i <= (nZeroes - countDigits); i++) {
zeroes += "0";
}
return zeroes;
}

Here ya go:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
Just specify how many digits you want. Set it to one. If it has to it will push over (so it won't cut digits off)

Related

trying to add substring to array every third character

In short, I'm trying to take a string then divide it every three characters, adding those three characters to an array as it progresses.
The initial input to the function (rawData) look something like this:
ATGCCACTATGGTAG
but can vary in length.
I'm trying to convert the above data (representing nucleotides) into the individual codons like so:
[ATG,CCA,CTA,TGG,TAG]
note that every chunk of 3 is now indexed into an array.
This is my code:
public static void codonList(String rawData) {
int previous = 0;
String[] codons = new String[rawData.length() / 3];
for (int i = 0; i < rawData.length(); i++) {
previous++;
// goes through each
// split at every third then append to end of codon string
if (previous % 3 == 0) {
String chunk = rawData.substring(previous - 3, previous);
codons[i] = chunk;
System.out.println(Arrays.toString(codons));
}
}
}
and its output:
[null,null,AGT,null,null]
I'm 90% sure it's a simple fix but can't seem to get it figured out. If someone can provide some insight that would be greatly appreciated!
You have several issues.
you are iterating through the whole string, you only want to create rawData.length()/3 codons.
As Kevin hinted, get 3 character chucks: String chunk = rawData.substring(i*3, i*3+3);
only print the array when you are done processing the string.
public static void codonList(String rawData) {
String[] codons = new String[rawData.length() / 3];
for (int i = 0; i < rawData.length() / 3; i++) {
// goes through each
// split at every third then append to end of codon string
String chunk = rawData.substring(i*3, i*3+3);
codons[i] = chunk;
}
System.out.println(Arrays.toString(codons));
}
Output:
[ATG, CCA, CTA, TGG, TAG]

Editing Unicode within a String

I wanted to have a List with Unicode Strings, but I wondered if I could use a for loop instead of adding 9 variables by hand. I tried the following code, but it didn't work.
List<String> reactions = new ArrayList<>();
for (int i = 1; i < 10; i++) {
reactions.add("\u003" + i + "\u20E3");
}
My IDEA gives me an 'illegal unicode escape' error.
Is there an other way to accomplish this?
The easiest way to convert a number to a character within a string is probably using a Formatter, via String.format:
List<String> reactions = new ArrayList<>();
for (int i = 1; i < 10; i++) {
reactions.add(String.format("%c\u20e3", 0x0030 + i));
}
Assuming you want to display the character \u003i, with ifrom 1 to 9, and \u20E3, remember a character is like a number and can be used in mathematical operation.
get the character \u0030: '\u0030'
add i : '\u0030' + i
concatenate the new character with the other one (as a string)
Then print the result:
System.out.println((char)('\u0030' + i) + "\u20E3");

Reducing run time in java

Below Java code produces the valid output but it takes more time to execute. Code works fine in eclipse, but it do not work in an online compiler like hackerrank or hackerearth since it takes more time for execution.Someone help me to find the solution for my time complexity problem.
I have tried to find the solution of the problem but i wasn't able to fix the performance by reducing the time..
Scanner scan = new Scanner(System. in );
String s = "aab";
String s1 = "";
String s2 = "";
int n1 = 0;
int length = 0;
long n = 882787;
long count = 0;
while (s1.length() < n) {
s1 = s1 + s;
}
if (s1.length() > n) {
count = s1.length() - n;
n1 = (int) count;
}
for (int i = 0; i < s1.length() - n1; i++) {
if (s1.charAt(i) == 'a') {
length += 1;
}
}
System.out.println(length);
Explanation of the above program:
I have a string s,in lowercase English letters that .I have repeat the string for n times and I store it in the new string.
I have to find the number of occurrences of 'a' in my new string
How do i actually reduce the time complexity for the above program
Thanks in advance
I would use a regular expression to create a String based on the initial input consisting of only letter 'a'(s). Take the length of that String and multiply it by n. That is one line that looks like
System.out.println(s.replaceAll("[^a]+", "").length() * n);
You are going to add s to the string n/s.length() times, call this N:
int N = n / s.length();
Each time you add s to the string you are going to append the number of As in s:
int a = 0;
for (int i = 0; i < s.length(); ++i) {
a += s.charAt(i) == 'a' ? 1 : 0;
}
// Or int a = s.replaceAll("[^a]", "").length();
So multiple these together:
int length = a * N;
String is immutable. Modification of a string is in fact create a new String object and put both old and new String into Java String constant poom
If you don't want to change your algorithm, I'd suggest to use StringBuilder to improve the speed of the execution. Note that StringBuilder is not thread safe
String s="aab";
int n1 = 0;
StringBuilder sb1 = new StringBuilder();
int length=0;
long n=882787;
long count=0;
while(sb1.length() < n) {
sb1.append(s);
}
if(sb1.length()>n) {
count =sb1.length()-n;
n1=(int)count;
}
for(int i=0;i<sb1.length()- n1;i++) {
if(sb1.charAt(i)=='a') {
length+=1;
}
}
System.out.println(length);
From here
When to use which one :
If a string is going to remain constant throughout the program, then
use String class object because a String object is immutable. If a
string can change (example: lots of logic and operations in the
construction of the string) and will only be accessed from a single
thread, using a StringBuilder is good enough. If a string can change,
and will be accessed from multiple threads, use a StringBuffer because
StringBuffer is synchronous so you have thread-safety.
I see multiple possible optimizations:
a) One pattern that is not that good is creating lots of Strings through repeated string concatenation. Each "s1 = s1 + s;" creates a new instance of String which will be obsolet the next time the command runs (It increases the load, because the String instances will be additional work for the Garbage Collector).
b) Generally: If you find, that your algorithm takes too long, then you should think about a complete new way to solve the issue. So a different solution could be:
- You know the length you want to have (n) and the length of the small string (s1) that you use to create the big string. So you can calculate: How often will the small string be inside the target string? How many characters are left?
==> You can simply check the small string for the character you are looking for. That multiplied by the number how often the small string will be inside the big string is the first result that you get.
==> Now you need to check the substring of the small string that are missing.
Example: n=10, s1="aab", Looking for "a":
So first we check how often the s1 will fit into a new string of n Characters n/length(s1) => 3
So we check how often the "a" is inside "aab" -> 2
First result is 3*2 = 6
But we checked for 3*3 = 9 characters so far, but we want 10 characters. So we need to check n % length(s1) = 1 character of s1 and in this substring ("a"), wie have 1 a, so we have to add 1.
So the result is 7 which we got without building a big string (which is not required at all!)
Just check how many times the char occurs in the original and multiple it by n. Here's a simple way to do so without even using regex:
// take these as function input or w/e
String s = "aab";
String find = "a";
long n = 882787;
int count = s.length() - s.replaceAll(find, "").length();
System.out.println(count * n);

about splitting a number into thousands

I think I did something wrong with my algorithm that I was trying to split my
number 150000 into thousands with for loop. I want the output to be like 150,000.
I was just having trouble coming up with a nice way doing it.
Here are my codes:
public class testing {
public static void main(String[] args) {
// TODO Auto-generated method stub
String l= "150000";
for(int i=l.length();i<0;i--){
if ((i/4)==0){
l=","+l.substring(i, l.length()-1);
}
}
System.out.println(l);
}
}
I want the output to be like 150,000. I was just having trouble coming
up with a nice way doing it.
Use DecimalFormat and set the grouping separator of DecimalFormatSymbols:
DecimalFormatSymbols symbol = new DecimalFormatSymbols();
symbol.setGroupingSeparator(',');
DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(symbol);
System.out.println(format.format(150000));//print 150,000
Edit
As per your comments, if you really want to use a loop, here one that would work :
String l = "150000";
String result = "";
for (int i = 0; i < l.length(); i++) {
if (i != 0 && i % 3 == 0)
result = result + "," + l.charAt(i);
else
result = result + l.charAt(i);
}
System.out.println(result);
Which would print 150,000.
Your actual loop was never reached, the condition was i < 0 but i was starting at l.length() which can never be less then 0. The i/4 is also wrong, you want to use modulo instead. I also believe it should be i % 3 instead of i % 4. You will also need to check if this is the beginning of the string else it will put a comma at begining.

Remove trailing zero in Java

I have Strings (from DB), which may contain numeric values. If it contains numeric values, I'd like to remove trailing zeros such as:
10.0000
10.234000
str.replaceAll("\\.0*$", ""), works on the first one, but not the second one.
A lot of the answers point to use BigDecimal, but the String I get may not be numeric. So I think a better solution probably is through the Regex.
there are possibilities:
1000 -> 1000
10.000 -> 10 (without point in result)
10.0100 -> 10.01
10.1234 -> 10.1234
I am lazy and stupid, just
s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
Same solution using contains instead of indexOf as mentioned in some of the comments for easy understanding
s = s.contains(".") ? s.replaceAll("0*$","").replaceAll("\\.$","") : s
Use DecimalFormat, its cleanest way
String s = "10.1200";
DecimalFormat decimalFormat = new DecimalFormat("0.#####");
String result = decimalFormat.format(Double.valueOf(s));
System.out.println(result);
Kent's string manipulation answer magically works and also caters for precision loss, But here's a cleaner solution using BigDecimal
String value = "10.234000";
BigDecimal stripedVal = new BigDecimal(value).stripTrailingZeros();
You can then convert to other types
String stringValue = stripedVal.toPlainString();
double doubleValue = stripedVal.doubleValue();
long longValue = stripedVal.longValue();
If precision loss is an ultimate concern for you, then obtain the exact primitive value. This would throw ArithmeticException if there'll be any precision loss for the primitive. See below
int intValue = stripedVal.intValueExact();
String value = "10.010"
String s = new DecimalFormat("0.####").format(Double.parseDouble(value));
System.out.println(s);
Output:
10.01
I find all the other solution too complicated. Simply
s.replaceFirst("\\.0*$|(\\.\\d*?)0+$", "$1");
does the job. It tries the first alternative first, so that dot followed by all zeros gets replaced by nothing (as the group doesn't get set). Otherwise, if it finds a dot followed by some digits (as few as possible due to the lazy quantifier *?) followed by some zeros, the zeros get discarded as they're not included in the group. It works.
Warning
My code relies on my assumption that appending a unmatched group does nothing. This is true for the Oracle implementation, but not for others, including Android, which seem to append the string "null". I'd call the such implementations broken as it just may no sense, but they're correct according to the Javadoc.
The following works for all the following examples:
"1" -> "1"
"1.0" -> "1"
"1.01500" -> "1.015"
"1.103" -> "1.103"
s = s.replaceAll("()\\.0+$|(\\..+?)0+$", "$2");
What about replacing
(\d*\.\d*)0*$
by
\1
?
You could replace with:
String result = (str.indexOf(".")>=0?str.replaceAll("\\.?0+$",""):str);
To keep the Regex as simple as possible. (And account for inputs like 1000 as pointed out in comments)
My implementation with possibility to select numbers of digits after divider:
public static String removeTrailingZero(String number, int minPrecise, char divider) {
int dividerIndex = number.indexOf(divider);
if (dividerIndex == -1) {
return number;
}
int removeCount = 0;
for (int i = dividerIndex + 1; i < number.length(); i++) {
if (number.charAt(i) == '0') {
removeCount++;
} else {
removeCount = 0;
}
}
int fracLen = number.length() - dividerIndex - 1;
if (fracLen - removeCount < minPrecise) {
removeCount = fracLen - minPrecise;
}
if (removeCount < 0) {
return number;
}
String result = number.substring(0, number.length() - removeCount);
if (result.endsWith(String.valueOf(divider))) {
return result.substring(0, result.length() - 1);
}
return result;
}
In addition to Kent's answer.
Be careful with regex in Kotlin. You have to manually write Regex() constructor instead of a simple string!
s = if (s.contains("."))
s.replace(Regex("0*\$"),"").replace(Regex("\\.\$"),"")
else s
Try to use this code:
DecimalFormat df = new DecimalFormat("#0.#####");
String value1 = df.format(101.00000);
String value2 = df.format(102.02000);
String value3 = df.format(103.20000);
String value4 = df.format(104.30020);
Output:
101
102.02
103.2
104.3002
Separate out the fraction part first. Then you can use the below logic.
BigDecimal value = BigDecimal.valueOf(345000);
BigDecimal div = new BigDecimal(10).pow(Integer.numberOfTrailingZeros(value.intValue()));
System.out.println(value.divide(div).intValue());

Categories