setText method not working properly? - java

I am trying to execute the settext method on my textview but it doesn't seem to work. I learned that "%s" in the first argument of the string.format method should return the given string of the second argument but it somehow it doesn't work.
SPCalories = Double.longBitsToDouble(sharedPreferences.getLong("Calories", Double.doubleToLongBits(0)));
d_c = SPCalories;
dc_text = Double.toString(d_c);
Calories_text.setText(R.string.Calories + String.format("%s", dc_text));
R.string.Calories:
<string name="Calories">Calories: </string>
Am I doing something wrong with SharedPreferences maybe?

You should do:
<string name="Calories">Calories: %s</string>
And:
Calories_text.setText(String.format(getString(R.string.Calories), dc_text));

You omitted getString method:
Calories_text.setText(getString(R.string.Calories) + String.format("%s", dc_text));
If you wold like to learn more about formatting strings check:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling.

Related

Substract specific parameter from url java

I need to substract an specific parameter (urlReturn) from a URL like this :
http://somedomain.mx/th=6048000&campus=A&matric=L01425785&serv=GRAD&count=2&per=TEST&passwd=69786e720c0f0e&mount=1000.00&urlReturn=http://somedomain.mx:7003/SP/app/login.xhtml?id=1234&mat=2323&fh=05012014124755&store=TESO
My final string should look like this:
String urlReturn = http://somedomain.mx:7003/SP/app/login.xhtml?id=1234&mat=2323;
And the rest of the string should look like this:
String urlReturn2 = http://somedomain.mx/th=6048000&campus=A&matric=L01425785&serv=GRAD&count=2&per=TEST&passwd=69786e720c0f0e&mount=1000.00&fh=05012014124755&store=TESO
I currently have this :
String string = string.toString().split("\\&")[0];
But the urlReturn parameter should always come as the first one.
Try this (s is your original String):
urlReturn = s.substring(0, s.indexOf("&urlReturn=")).replace("&urlReturn=", "");
urlReturn2 = s.substring(s.indexOf("&urlReturn=")).replace("&urlReturn=", "");
Definitely not elegant at all, but working. I really need some sleep now so take my anser carefully :) You may alswo wanto to check if the parameters is in the String s via the contains method to avoid index out of bounds exceptions.
use string#split
url.split("\\?")[1];

Strings.xml and append

I am playing a bit around with Android after doing a number of tutorials.
I have a textview in which I want to print various lines of text that I have written in the strings.xml file:
<string name="welcome">Welcome! What is your name?</string>
<string name="welcome2">Welcome!</string>
In my main I have:
gamehistory = (TextView)findViewById(R.id.textView2);
gamehistory.setText(R.string.welcome);
This works fine and it displays the text. I want to use the textbox as a "log" so that a bit later in my code it prints welcome2 in the next line.
gamehistory.append("\n" + R.string.welcome2);
Unfortunately, when I use append, it turns the string into a number.
Is there a way to avoid this?
To append i think will not be there but yes you can concatenate like this
String outStr = getString(R.string.first) +
" " + getString(R.string.second);
For Refrence Link to refrence
String welcomestr = Context.getResources().getString(R.string.welcome2)
gamehistory = (TextView)findViewById(R.id.textView2);
gamehistory.setText(welcomestr);
Use the Context.getResources().getString(id) for that.
getReources().getString(R.string.welcome2); // Since you're calling this in your activity itself.
because R.string.welcome2 is an auto generated integer value for your string resource.
Try adding carriage return.
gamehistory.append("\r\n" + R.string.welcome2);
Or add these lines to TextView part of xml:
android:maxLines="2"
android:minLines="1"

Split a String In Java against the split rule?

I have a string like this:
String str="\"myValue\".\"Folder\".\"FolderCentury\"";
Is it possible to split the above string by . but instead of getting three resulting strings only two like:
columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";
Or do I have to use an other java method to get it done?
Try this.
String s = "myValue.Folder.FolderCentury";
String[] a = s.split(java.util.regex.Pattern.quote("."));
Hi programmer/Yannish,
First of all the split(".") will not work and this will not return any result. I think java String split method not work for . delimiter, so please try java.util.regex.Pattern.quote(".") instead of split(".")
As I posted on the original Post (here), the next code:
String input = "myValue.Folder.FolderCentury";
String regex = "(?!(.+\\.))\\.";
String[] result=input.split(regex);
System.out.println("result: "+Arrays.toString(result));
Produces the required output (an array with two values):
result: [myValue.Folder, FolderCentury]
If the problem you're trying to solve is really that specific, you could do it even without using regular expression matches at all:
int lastDot = str.lastIndexOf(".");
columnArray[0] = str.substring(0, lastDot);
columnArray[1] = str.substring(lastDot + 1);

validating a string with RegEx

I am trying to validate a RegEx string in Java but i cant get it to work properly. I am attempting to make sure the the following string "AB10XY" is always contained with the textField when performing a search.
I have the following line of code which ensures that AB and XY are in the textField but not the number:
boolean checkChar = ((textField.getText().contains("AB")) && (textField.getText().contains("XY")));
I would prefer to have something like :
boolean checkChar = ((textField.getText().contains("AB[\\d]{2}XY")));
You can try this way
boolean checkChar = ((textField.getText().matches(".*?AB[\\d]{2}XY.*")));
I'm guessing the number isn't alway 10, so you should use something like this:
boolean checkChar = textField.getText().matches(".*AB[\\d]{2}XY.*");
Use package java.util.regex to handle RegEx
boolean checkChar = java.util.regex.Pattern.compile("AB[\\d]{2}XY")
.matcher(textField.getText()).find();
Contains method doesn't support regex..You can use matches method
textField.getText().matches(".*AB\\d{2}XY.*");

Android - String

Probably a noob question but, after some search I could not find the answer.
I've receive this String like the following one: "Distribui\u00e7\u00e3o Alimentar", and I want to set it as text of a EditText. How can I "replace" the \u00e7 and \u00e3o to "ç" and "ã"?
Thanks in advance.
With the string replace function
String newString = stringName.replaceAll("\u007", "c');
String newString2 = stringName.replaceAll("\u00e7", "a");
and you could concatenate the two with the concat method

Categories

Resources