I am trying to take a person's name from a text input field, send it to another Activity, and display that person's name in a sentence within a TextView.
My code is as follows:
This is in one of my Activities.
String meaningText = mCurrentColor.getMeaning();
meaningText = String.format(meaningText, mYourName);
mColorText.setText(meaningText);
I'm pulling the Meaning string from another class that would contain something along the lines of:
"Hey %1$s, you chose this which means blah blah....."
Then I want to insert that mYourName variable which is pulled from an EditText in a previous Activity and put that in the above string.
I found that formatting from a Team Treehouse tutorial and it worked in that project's code as I followed along.
I'm using the String.format which takes in a Locale.
I am not sure exactly what is wrong as I mirrored the code to a "T" and just changed some variable names.
Seeing as the String class' length() method returns an int value, the maximum length that would be returned by the method would be Integer.MAX_VALUE, which is 2^31 - 1 (or approximately 2 billion.)
So you can have a String of 2,147,483,647 characters, theoretically. I don't think you should need much more than that.
However, Android system limits your heap space, going as low as 16 MB. Therefore, you'll never practically be able to reach the theoretical limit, and will max out with a String somewhere in the 4-64 million character range.
so, why are you thinking of a longer string variable, when you can directly use your name variable along with a string variable of long length.
for Ex:-
String meaning_text = "the very long message you wanted to show..."+ mYourName + "again a long message if you want to show";
so my suggestion is to use your name variable directly with message you wanted to show.
EditText edt=(EditText)findViewbyId(R.id.edt);
edt.setText("Dipali");
now,getting value from edittext and Replace in TextView String resource.
String strEdtTextValue=edt.getText().toString();
TextView tV=(TextView)findViewById(R.id.txtView)
Set text in textview as xml Resource like as
dummy="Hey %1$s, you chose this which means blah blah....."
add string in resource file
<string name="dummy">Hey %1$s, you chose this which means blah blah.....</string>
Resources res = getResources();
String dumyText= (res.getString(R.string.dummy),strEdtTextValue);
tv.setText(dumyText);
Its helpful to you.
Related
I'm a beginner who's been desperately trying to understand how to achieve the following thing: I have 3 TextView displaying 3 numeric values (always integers) on screen as text, such as: 50, 100, 200. Then, after some event, I want these 3 strings to be put as values in my PieEntries
in the ArrayList. I can't just get the text from the TextView and put it as a value for the entry, so I tried to use Integer.parseInt to read the Strings as an Ints (since they are actually made of all numbers), or also Integer.valueOf but I only get java.lang.NumberFormatException: For input string: "First" error after the crash. This is the part I'm talking about:
ArrayList<PieEntry> myData(){
ArrayList<PieEntry> myArray = new ArrayList<PieEntry>();
arrayValue.add(new PieEntry(Integer.parseInt(TextView1.getText().toString()), "First"));
arrayValue.add(new PieEntry(Integer.parseInt(TextView2.getText().toString()), "Second"));
arrayValue.add(new PieEntry(Integer.parseInt(TextView3.getText().toString()), "Third"));
return graphValues;
}
I won't paste the whole code here since the graph actually works fine if I manually put values instead of trying to parse the integers from the TextViews.
Also: when I first start to type "new PieEntry", the hint clearly says that it's gonna require a "float value" (then a drawable or a string and so on). Is this where the troubles begin? Should I convert Strings into Integers (or into Floats?) in some way before? I guess there's just like some basic Java rule I'm ignoring, so I'd like to understand what is happening here and how to face this. Many thanks!
As you Explained, you are not getting the TextView text, it is because maybe you have written it somewhere you should not, i.e. provide a Button, when the user types and is done he will hit the button say submit and do this above code on button click.
Solved: I used SharedPreferences to store the new typed data as Integers and retrieve them whenever I need them:
Setting up my Preferences:
SharedPreferences myPreferences;
savedData = getSharedPreferences("myStoredData", Context.MODE_PRIVATE);
Saving an Integer for later:
SharedPreferences.Editor myEditor = savedData.edit();
myEditor.putInt("Label", int);
myEditor.apply();
myEditor.commit();
Retrieving and using the Integer:
savedData.getInt("Label", Default);
I am new to Java and using the JIRA MISC Custom Fields add-on and require some logic assistance to solve math functions between two drop down fields.
Field one is "User Cost"
This field contains four string selections with the user price posted at the end of the string.
sam costs .21
mitch costs .419
Lance costs 2.66
xmen costs 13.338
Field two is "Usage"
This field contains two string selections:
24 hours (unless maintenance)
12 Hours (7a-7p)
The argument should be invoked into a new field called "Total User Cost." This field would automatically display the correct price for user and usage amount.
The equation blueprint would be as follows:
Cost*31(calendar days)*usage(12 || 24)
I would want my form to update based on user input selection of these two fields and other variables in my equation.
This is what I have so far:
[
Thank you in advance for any feedback!
If I understand correctly, you first need to initialize issue to something. (It looks red in your images like that variable doesn't exist)
Then, you can do something like this
double costSam = 0.21;
String userSam = issue.get("customfield_10620");
Then, if you are needing to convert or otherwise do some math on userSam, then you need this
double samTotal = costSam * Double.parseDouble(userSam);
Some flaws with your code
You have to define types for your values like String or double.
If you have String x = "hello" on one line, then x = 0.4 on the next, that won't work because of incompatible types
If you did have compatible types on consecutive lines, then the first line is pointless unless using the value from the first, as the second line overwrites the value of the first one
I've been reading up on it, but every question I've found has asked for slightly different things, such as only wanting a single letter for their array, or in a different language (I'm new and only learning java at the moment), so here I am.
I want to set up an array that uses the user's input for their names.
What I have so far is this, I'm assuming this is the declaration line, where later I use an input line to define a value within the array (which I also am unsure how to do)
String[] array = {"name"};
But I don't know how to for example print.out the object or keep up with which name will be what value. I appreciate your time taken to teach me!
EDIT for further clarification. I'm trying to write up a small app that asks the user for numerous names, addresses, and phone numbers (Type name -> Type name's address -> type name's phone number, ask if they want to add another person, if yes then go back to asking for another name)
I am unsure how to set up a String array or how to use it throughout. However, thanks to your input and coming back after some fresh air, I have a better idea how to word it for google. Thank you guys for your help, even if it was just to gesture a better articulated question.
An array is a sequence of values. You have created an array of Strings that is one String long. To access the value at a specific of an array, use array subscript notation: the name of the array followed by a pair of square brackets ([]) with the index in between them.
String[] anArrayOfStrings = {"string0", "string1", "string2"};
anArrayOfStrings[0]; //the first element
System.out.println(anArrayOfStrings[1]); //print the second element
anArrayOfStrings[2] = "new string value"; //assign the third element to a new value
if (anArrayOfStrings[0].equals("string0") //evaluate the first element and call a method
{
//this block will execute anArrayOfStrings[0] is "string0"
}
anArrayOfStrings[3]; //error, index out of bounds
Simply declaring the array would be
String[] names;
In your code you both declare and assign it in the same line by using an initializer list.
To assign individual elements, use the [] notation. Note that once you initialized you list to be only one String long, it cannot become longer than without be re-assigned. To declare an array of any size, you can use:
String[] arrayWithInitialSize = new String[5]; //holds five strings, each null to begin with
I am making an app that generates random numbers for android (you can find it hat https://github.com/JXPheonix/RNumGen ) and I need some way for an xml string to be dynamic (of sorts); it needs to change every time it is viewed.
I want the string to invoke a method that generates a random number. The string in my xml file goes something like this:
<string name="number">Your number is</string>
and I want after the is for a method in java to be called upon, which would subsequently change the ending of the string. Any way to do this (whether or not it involves actually editing the strings.xml file?)
You're going about it a round-about way. Instead of somehow making your string in your XML file dynamic, just use your string and append to it in code. Something like:
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(getString(R.string.number) + yourRNGMethod());
Don't change the string, concatenate the constant string with your generated number in your code.
Alternatively, use the string as an input to String.format and use a placeholder for the number.
You don't want to use the strings.xml for strings which have changing values.
Its not clear what the scope for consumption of your random number is but you can always use the Math classes to generate a random number and then Integer.toString() the value to get the value as a string.
I would like get variable values from a file name.
For example, I have a folder of images with 2 sub folders called "photos_2011_mycamera" and "photos_2012_mycamera". I want to be able to read the year and then I will use it with the photos inside the folder. I was thinking in something like tokens where you can put
*$* and this would read the number in $ and take it as a variable (in this case the years 2011 and 2012).
I'm guessing that I will have problems with variables at the end of the path, not properly separated...
I use Java, but the problem would be the same in any other language, I guess.
I would appreciate any suggestion or tips of the best way to do this.
Thanks a lot!
Assuming you already have the file name in a variable called FileName you can use the split() method in Java to split up the string and add the different substrings to an array.
For example:
String FileName = "photos_2011_mycamera";
String type, year, name;
String[] dataArray = FileName.split("_") // Splits the string wherever a "_" occurs
type = dataArray[0];
year = dataArray[1];
name = dataArray[2];
System.out.println(type); //photos
System.out.println(year); //2011
System.out.println(name); //mycamera
Assuming your files will always be named in that format this method souhld work fine.