I'm searching for numbers in my app, but it does not work, where is the problem please ?!
numrat is int[] and fint is a number from fytyratint[]...
int fint = fytyratint[nrRendor];
int nse = Arrays.binarySearch(numrat, fint);
if (nse <0 ){
pS++;
tvS.setText("Sakt: "+ Integer.toString(pS));
}
else
{
pG++;
tvG.setText("Gabimet: " + Integer.toString(pG));
}
So nse must be negative if number does not exist in int[] numrat and must be positive if fint exists on int[] numrat..
This is what I read on internet..
but in my example it is always negative.. ??!!
If you want Arrays.binarySearch() to work you should sort your array like written here:
binarySearch() ...
Performs a binary search for value in the ascending sorted array
array. Searching in an unsorted array has an undefined result. It's
also undefined which element is found if there are multiple
occurrences of the same element.
You may do it like this:
Arrays.sort(numrat);
Arrays.binarySearch(numrat, fint);
Related
So I have this array List:
private ArrayList<String> output;
Which look like this normally once items are added looks like this:
["ready", "30 min", "5.2 mi", "stop"]
But sometimes the order can change on how the information comes in for example:
["30 min", "stop", "5.2 mi", "ready"]
What would be the best solution to get the number that is in front of mi and min since the ArrayList can change I can't use list.get(1) for example cause the index might be something different.
I have tried to use list.getIndexOf("mi"); but that didn't work, kept coming back as -1 which I would think cause it wasn't an exact match.
Use this .. to get no before "mi"
for(int i =0; i<list.size();i++)
{
if(list.get(i).contains("mi"))
{
String[] splitT =
list.get(i).split(" ");
System.out
.println("before mi is" + splitT[0]);}
}
Note splitT[0] is the answer what you want.. like this you can find for "min" too..
If I understood your question correctly, you want to get an element whith a certain pattern to further work with that information. In that case you could loop through the ArrayList and check for element whether or not it matches a certain regexp. Something like this:
for (String s : arraylist) {
if (s.matches("[0-9]+ mi(n)?")) //would match any number, followed by a whitespace and mi/min {
//do something
}
}
You can do something likes this.
for (String item:list){
if (item.contains("mi")&&!item.contains("min")){
Log.d("item",item.substring(0,3));
}
}
You can do similarly for "min".
You can use this code:
for (int i=0 ; i<output.size() ; i++){
if (output.get(i).endsWith("mi")) // index i contains "mi"
else if (output.get(i).endsWith("min")) //index i contains min not mi
}
i have following code in my project
ImageView lifesimage, good;
TextView lifes1;
int gamelifes=6, gamehints=6, index=0;
int [] heartimage={R.drawable.lifessix,
R.drawable.lifesfive,
R.drawable.lifesfour,
R.drawable.lifesthree,
R.drawable.lifestwo,
R.drawable.lifesone,
R.drawable.lifesno,
};
good=(ImageView)findViewById(R.id.youwin);
lifesimage =(ImageView)findViewById(R.id.lifes);
lifesimage.setImageResource(heartimage[0]);
lifes1 =(TextView)findViewById(R.id.lifestext);
lifes1.setTextColor(Color.RED);
lifes1.setText(String.valueOf(gamelifes));
So if the answer is correct or wrong Then the code is
if(you.equalsIgnoreCase(answer[index]))
{
good.setVisibility(View.VISIBLE);
}
else
{
heartimage++;
gamelifes--;
lifes1.setText(String.valueOf(gamelifes));
}
but I found some error on heartimage++; such as Type mismatch: cannot convert from int[] to int
how to fix this code? is there a function that can make the image appear sequential ?
'heartimage' in your code is an integer array, and you are trying to add 1 to it... not going to work. Create a new variable 'index' (or whatever you would like to name it) that will store the current index of the heart value that you want.
Then lifesimage.setImageResource(heartimage[index]);
I'm using the speech recognizer to get a voice input from the user, it returns an array of 5 strings which I pass to this method
public int analyzeTag(ArrayList<String> voiceResults,Editor editor, Context context){
for (String match : voiceResults) {
Log.d(TAG, match);
if (match.equalsIgnoreCase(context.getResources().getString(R.string.first_tag))){
editor.append(context.getResources().getString(R.string.first_tag));
return 1;
}
else if (match.equalsIgnoreCase(context.getResources().getString(R.string.second_tag))){
editor.append(context.getResources().getString(R.string.second_tag));
return 1;
}
//etc....(huge list of tags)
//Some tags might also have acceptable variations, example:
else if (match.equalsIgnoreCase("img") || match.equalsIgnoreCase("image")
{
editor.append("img"); //the string to append is always taken from the first variation
}
}
return 0;
}
This method compares the results with a list of tags, the tag list will be pretty big with hundreds of tags so I would like to find the most efficient way to do this operation.
I need help with:
1.Is my way of comparing results the most efficient? Is there a better way? (from the user experience perspective, I don't want users waiting a long time to get a result).
The voice input will be a big part of my app so this method will be called quite often
2.I have a long list of tags, obviously the if(), elseIf() route is gonna be quite repetitive, is there a way to iterate this? Considering the fact that some tags might have variations (even more than 1)and that the variation 1 ("img") will be the same for everyone, but other variations will be locale/language sensitive example: "image" for english users "immagini" for italian users etc.
Text appended to the editor will be always taken from the first variation
How about puting tags in a StringArray and then iterate though the array ?
String[] tags = context.getResources().getStringArray(R.array.tags);
for (String match : voiceResults) {
for (int index = 0; index < tags.length; index++ ) {
if (match.equalsIgnoreCase(tags[index]) {
editor.append(tags[index]);
}
}
}
Here's the doc on StringArray
I've seen many people do similar to this in order to get the last word of a String:
String test = "This is a sentence";
String lastWord = test.substring(test.lastIndexOf(" ")+1);
I would like to do similar but get the last few words after the last int, it can't be hard coded as the number could be anything and the amount of words after the last int could also be unlimited. I'm wondering whether there is a simple way to do this as I want to avoid using Patterns and Matchers again due to using them earlier on in this method to receive a similar effect.
Thanks in advance.
I would like to get the last few words after the last int.... as the number could be anything and the amount of words after the last int could also be unlimited.
Here's a possible suggestion. Using Array#split
String str = "This is 1 and 2 and 3 some more words .... foo bar baz";
String[] parts = str.split("\\d+(?!.*\\d)\\s+");
And now parts[1] holds all words after the last number in the string.
some more words .... foo bar baz
What about this one:
String test = "a string with a large number 1312398741 and some words";
String[] parts = test.split();
for (int i = 1; i < parts.length; i++)
{
try
{
Integer.parseInt(parts[i])
}
catch (Exception e)
{
// this part is not a number, so lets go on...
continue;
}
// when parsing succeeds, the number was reached and continue has
// not been called. Everything behind 'i' is what you are looking for
// DO YOUR STUFF with parts[i+1] to parts[parts.length] here
}
What I'm trying to do is find a way I can take the word "camel" for example from a EditText field and make for instance c=2 a=1 m=4 e=5 l=3. Is there anyway I can pull the individual characters from a string and convert them to numbers?
I've tried using "split" to separate each character into an array but I can't figure out how to convert the letters into numbers
so I can do something like:
a=1
b=2
c=3
int temp = (int)(array[1]+array[2]+array[3]+etc...)
using the example of "camel" would equal 15
This is what I have so far:
String name = inputarea.getText().toString();
String[] array = name.split("");
for(int i =0; i < array.length ; i++)
The biggest problem I keep having is if I try to pull from the 7th position in the array and nothing is there. (camel only has 5 characters) then I get a nice big error.
Thank you for any help that can be provided.
Edit: I figured it out after a few hours of playing with it here is my working code:
String firstname = inputarea.getText().toString();
char[] array = firstname.toCharArray();
final char[] array2 = new char[15];
System.arraycopy(array, 0, array2, 0, array.length);
if (array2[0] == 'A' ) {
array2[0] = '1';
}
suggestion:
first, need define all letter, from a-z (A-Z), the ASCII code 'a' to 'z' is 97 to 122, if you want support the upper letter, you need add A-Z.
then, get the letter in the string, u can use this:
for(int i=0;i<string.length();i++){
int number = string.charAt(i);
}
when you get the number size, you can reduce to the base number('a' is 97), you will get the individual number
Does String.charAt() works for you?
As for converting to number, if the numbers are consecutive you can define a fixed string with all the characters you want to map and use String.indexOf(). If not, you can have a parallel array with ints or use a Map.