How to convert TextView to int? - string

I know that there are a lot of posts like this but I read them and my application does not work yet.
Im trying to convert TextView parameter into int.
I use this:
int MyScore;
TextView score = (TextView) findViewById(R.id.highscore);
MyScore = Integer.parseInt(score.toString());
When im launching the program and rich to the place this should work my program crushing becasue of the last line: MyScore = Integer.parseInt(score.toString());
How can I solve this?

Your last line should read
MyScore = Integer.parseInt(score.getText().toString());
The toString() method called on your score object describes the score object, it does not return the string entered into the score object. Please refer to the Oracle Java tutorials. It is a good idea to read through most of them.

Because you didn't specify the language used I cannot answer fully, but I can guess what the problem is. Most input fields have a value or text property, try parsing that.

Related

How to save a formatted string?

I was wondering if its possible to save a formatted string in python in any way?
For example, could I create an arbitrary string like this:
s = f"This is my string. This is a {variable}"
and then save it in a csv or an SQL database for later use given that variable would always be set before loading?
I have already tried this with a CSV document and in MySQL without much luck so I concluded that this wasn't possible. Google hasn't given much of a result either.
My specific problem is that I have a large file containing hundreds of symptoms. Each symptom is a class and inherits a parent class which contains 7 base questions regarding the symptom. I would like to load a formatted string in the parent class for the sublass to load(as it inherits the parent class). An example would be something along these lines:
In Parent class:
self.question = f"Do you have a {self.symptom}?"
In Headache class:
self.symptom = "headache"
would be parsed to the string: "Do you have a headache?" etc.
I would really like to load the questions from a database for maintainance purposes since maintaining a large .py file with large number of classes, each with a question in a string format would end up as a total nightmare.
Thanks!
Edit: spelling
I have a quick idea, but I wouldn't recommend it:
Storing it into a string, to execute it:
self.generatequestion = 'question1=f"Do you have a {self.symptom}?"'
in Headache:
self.symptom = "headache"
And then
exec(self.generatequestion1)
print(question1)

Converting Unicode in Swift

I currently have a string as follows which I received through an API call:
\n\nIt\U2019s a great place to discover Berlin and a comfortable place
to come home to.
And I want to convert it into something like this which is more readable:
It's a great place to discover Berlin and a comfortable place to come
home to.
I've taken a look at this post, but that's manually writing down every conversion, and there may be more of these unicode scalar characters introduced.
What I understand is \u{2019} is unicode scalar, but the format for this is \U2019 and I'm quite confused. Are there any built in methods to do this conversion?
This answer suggests using the NSString method stringByFoldingWithOptions.
The Swift String class has a concept called a "view" which lets you operate on the string under different encodings. It's pretty neat, and there are some views that might help you.
If you're dealing with strings in Swift, read this excellent post by Mike Ash. He discusses the idea of what a string really is with great detail and has some helpful hints for Swift 2.
Assuming you are already splitting the string and can get the offending format separately:
func convertFormat(stringOrig: String) -> Character {
let subString = String(stringOrig.characters.split("U").map({$0})[1])
let scalarValue = Int(subString)
let scalar = UnicodeScalar(scalarValue!)
return Character(scalar)
}
This will convert the String "\U2019" to the Character represented by "\u{2019}".

How to compare Strings and put it into another program?

i´ve got small problem and before I spend even more time in trying to solve it i´d like to know if what I want to do is even possible ( and maybe input on how to do it^^).
My problem:
I want to take some text and then split it into different strings at every whitespace (for example "Hello my name is whatever" into "Hello" "my" "name" "is" "whatever").
Then I want to set every string with it´s own variable so that I get something alike to a= "Hello" b= "my" and so on. Then I want to compare the strings with other strings (the idea is to get addresses from applications without having to search through them so I thought I could copy a telephone book to define names and so on) and set matching input to variables like Firstname , LastName and street.
Then, and here comes the "I´d like to know if it´s possible" part I want it to put it into our database, this means I want it to copy the string into a text field and then to go to the next field via tab. I´ve done something like this before with AutoIT but i´ve got no idea how to tell AutoIT whats inside the strings so I guess it must be done through the programm itself.
I´ve got a little bit of experience with c++, python and BATCH files so it would be nice if anyone could tell me if this can even be done using those languages (and I fear C++ can do it and I´m just to stupid to do so).
Thanks in advance.
Splitting a string is very simple, there is usually a built in method called .split() which will help you, the method varies from language to language.
When you've done a split, it will be assigned to an array, you can then use an index to get the variables, for example you'd have:
var str = "Hello, my name is Bob";
var split = str.split(" ");
print split[0]; // is "Hello,"
print split[1]; // is "my" etc
You can also use JSON to return data so you could have an output like
print split["LastName"];
What you're asking for is defiantly possible.
Some links that could be useful:
Split a string in C++?
https://code.google.com/p/cpp-json/

Replacing or substituting in a python string does not work

I could almost solve all of my python problems thanks to this great site, however, now I'm on a point where I need some more and specific help.
I have a string fetched from a database which looks like this:
u'\t\t\tcase <<<compute_type>>>:\n\t\t\t\t{\n\t\t\t\t\tif (curr_i <= 1) Messag...
the string is basically plain c code with unix line endings and supposed to be treated in a way that the values of some specific variables are replaced by something else gathered from a Qt UI.
I tried the following to do the replacing:
tmplt.replace(u"<<<compute_type>>>", str(led_coeffs.compute_type))
where 'led_coeffs' is a namedtuple and its value is an integer. I also tried this:
tmplt = Template(u'\t\t\tcase ${compute_type}:\n\t\t\t\t{\n\t\t\t\t\tif (curr_i <= 1) Messag...)
tmplt.substitute(compute_type = str(led_coeffs.compute_type))
however, both approaches do not work and I have no idea why. Finally I was hoping to get some input here. Maybe the whole approach is not right and any hint on how to achieve the replacing in a good manner is highly appreciated.
Thanks,
Ben
str.replace (and other string methods) don't work in-place (string in Python are immutable) - it returns a new string - you will need to assign the result back to the original name for the changes to take effect:
tmplt = tmplt.replace(u"<<<compute_type>>>", str(led_coeffs.compute_type))
You could also invent your own kind of templating:
import re
print re.sub('<<<(.*?)>>>', lambda L, nt=led_coeffs: str(getattr(nt, L.group(1))), your_string)
to automatically lookup attributes on your namedtuple...

Oreilly servlet String Value manupulation

I am trying to truncate a string using Oreilly servlet classes.
My problem is that I don't want to use the whole string but a part of it. paramPart.getStringValue()
will give the value uncut but I have a scenario which requires I get a part of the string.
for example: The String value could be "xxxyyyy ,,," while I only require to work with "xxxyyyy". I hope my problem is clear to that point.. Any help will be highly appreciated.
Pss
Am using com.oreilly.servlet classes on my jsp application.
I found a solution to my problem which worked just fine, using java's substring() method.
first I got the value by String value = paramPart.getStringValue();
then I went ahead and created another variable String valuetrucated = value.substring(0, value.length() - 2);
this will truncate the string value to the 3rd char from the end of the string...

Resources