Get the even and odd letters from a text - android

class oddevens{
public static void main(String[] args){
String A, Even, Odd;
int a,x,y;
x=0;
y=1;
A="sample text here";
a=A.length();
Even="";
Odd="";
for(int c=0;c==a;c++) {
if(c%2==0){
Even=Even+A.substring(x,y);
x=x+1;
y=y+1;
}else{
Odd=Odd+A.substring(x,y);
x=x+1;
y=y+1;
}
}
System.out.println("Even: "+Even+", Odd: "+Odd);
}
}
With this code, I try to divide String A by taking the 'even and odd' letters, so the output should look like this:
Even:sml ethr, Odd: apetx ee
But is not showing anything.

In your for loop, you have put c==a as the condition. Since a for loop runs for as long as that condition is met, your loop does not do anything (since c starts of as 0 and a starts of as the length of A.
It should be for(int c =0; c<a; c++)
A shorter solution to your problem would be something like this:
for(int c = 0; c < A.length(); c++){
if(c % 2 == 0{
Even += A.charAt(c);
}else{
Odd += A.charAt(c);
}
}

Your code will never run
for(int c=0;c==a;c++) {
.....
}
should be
for(int c=0;c < a;c++) {
.....
}
Anyway, you did not even tell us your problem.

Related

Android Studio - App crash when using intent parameter

I have a function that 'crafts' products using two String parameters.
This is working fine when I put in hard coded strings like 'Wheel' & 'Car'.
But it makes my application crash if I try to put in the exact same strings but then provided by an intent.
I already tried to give in variable into the intent instead of a hard coded string. That did not work either.
Here is some part of the code. EDIT: Error log now included
productLeft = getIntent().getStringExtra("PRODUCT LEFT");
productRight = getIntent().getStringExtra("PRODUCT RIGHT");
public void craft(String product1, String product2) {
String[][] Products = factory.getProductList();
int i = 0;
while (finalProduct == "") {
int j;
for(j = 0; j < 3; j++){
if (product1 == Products[i][0] || product2 == Products[i][0]) {
if (product1 == Products[i][1] || product2 == Products[i][1]){
finalProduct = Products[i][2];
}
}
i++;
}
}
}
Problem is with the array index obviously. The array has only four elements and you are fetching index 4, probably in for loop with i variable. But then again I also do not see the role of j in that loop, can't tell without other parts of code.

String Multiline - Android

i got this issue and i don't know how to solve it. Here is the problem:
1 - i have a data in my database who i split into a strings[] and then i split this strings[] into another 2 strings[] (even and odd lines). Everything works fine but when i want to join all the lines into a single String i got a multi line string intead of a single line. Someone can help me?
data
abcdef//
123456//
ghijkl//
789012
code:
String text = "";
vec1 = data.split("//"); //split the data
int LE = 0;
for (int a = 0; a < vec1.length; a++) { //verify how many even and odds line the data have
if (a % 2 == 0) { //if 0, LE++
LE++;
}
}
resul1 = new String[LE];
int contA = 0, contB = 0;
for (int c = 0; c < resul1.length; c++) {
if (c % 2 != 0) {
text += " " + resul1[c].toLowerCase().replace("Á","a").replace("Ã","a").replace("ã","a").replace("â","a").replace("á","a").replace("é","e").replace("É","e")
.replace("ê","e").replace("í","i").replace("Í","i").replace("ó","o").replace("Ó","o").replace("õ","o").replace("Õ","o").replace("ô","o").replace("Ô", "o")
.replace("Ú","u").replace("ú","u").replace("ç","c").replace("_","").replace("<","").replace(">","");
contA++;
}
}
And the String looks like
abcdef
ghijkl
instead of
abcdefghijkl
You should use replaceAll() method.
text.replaceAll("\\r\\n|\\r|\\n", ""); // the method removes all newline characters

android spell checking

I am trying to check the spelling of a word in code using the api's for ics.
I have used the spell checker sample as a starting point and using this I can get a list of suggested words based on the query word, however I can't see how just to check that the word is spelled correctly.
I have the code below from the example and using this I can see and check the suggestions but not the original word.
#Override
public void onGetSuggestions(final SuggestionsInfo[] arg0) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < arg0.length; ++i) {
// Returned suggestions are contained in SuggestionsInfo
final int len = arg0[i].getSuggestionsCount();
sb.append('\n');
for (int j = 0; j < len; ++j) {
sb.append("," + arg0[i].getSuggestionAt(j));
}
sb.append(" (" + len + ")");
}
runOnUiThread(new Runnable(){
#Override
public void run(){
if(arg0[0].getSuggestionsAttributes()==SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY){
mMainView.append(sb.toString());
}
}
});
}
I have added the if statement that checks for RESULT_ATTR_IN_THE_DICTIONARY but I don't know if this is checking the original word or the first suggestion. (If I enter 'ton' as a query I get 'ten' returned however if I enter 'twn' as a query I get no words returned)
What I really need is for either a correct word or empty string to be returned to a query.
Any help would be appreciated,
Thanks
for (int j = 0; j < len; ++j) {
if(arg0[i].getSuggestionAt(j).toString().contains(string)){
sb.append("," + arg0[i].getSuggestionAt(j));
Log.e("check if", ""+arg0[i].getSuggestionAt(j));
}else{
sb.append("" );
Log.e("check", ""+arg0[i].getSuggestionAt(j));
}
}
Try with this code
and also remove
if(arg0[0].getSuggestionsAttributes()==SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY){
mMainView.append(sb.toString());
}
if condition just put mMainView.append(sb.toString());
hope it's useful to you.

Android how to print a array in text view or anything

I have a array of words and I would like to print the array of words onto the screen in a text view after a button is clicked. i was using a for loop to go through the whole list and then set text, but i keep getting a error with that plus it will just replace the last value, so it wont print the whole array. If anybody could explain to me how to do like a g.drawSting() but android version that would be great. my code rt now is not with me but it something like:
-I'm a beginner to android btw, probably could tell by this question tho.
public void onCreate(Bundle savedInstanceState)
{
//code for a button just being pressed{
//goes to two methods to fix the private array{
for(int y=0; y<=array.size()-1; y++){
textArea.setText(aarray.get(y)); //prints all strings in the array
}
}
}
int arraySize = myArray.size();
for(int i = 0; i < arraySize; i++) {
myTextView.append(myArray[i]);
}
if you want to print one by one, then use \n
myTextView.append(myArray[i]);
myTextView.append("\n");
PS:
Whoever suggesting to change .size() to .length(), thanks for you suggestion.
FYI,
The questioner mentioned the variable name is array.size() in question, so the answer also having the same variable name, to make it easier for the questioner.
if your variable (myArray) is an Array use myArray.length(), if it is ArrayList use myArray.size()
You have to combine all text into a String before you can give it the TextView. Otherwise you overwrite the text all the time.
public void onCreate(Bundle savedInstanceState)
{
StringBuilder sb = new StringBuilder();
int size = array.size();
boolean appendSeparator = false;
for(int y=0; y < size; y++){
if (appendSeparator)
sb.append(','); // a comma
appendSeparator = true;
sb.append(array.get(y));
}
textArea.setText(sb.toString());
}
I use this no-index solution, just an easy to remember one liner:
for(File file:list) Log.d(TAG, "list: " + file.getPath());

Array Being Overwritten with Last Index in Loop

I'm working on code that takes two arrays with strings (the strings are just sentences) and allocates them to classes which are held in another array (The Sentence class array shown below in the code).
So here's my problem. When popList() is called, the for loop runs through twice and works fine, putting the first index of addStrings and addTranslation into the first class in the array. However, when the loop indexes up and runs temp.sentence = addStrings[1] again, it OVERRIDES the first class's .sentence also. Then when temp.translations = addTranslations[1] runs again it OVERRIDES the first class's .translation.
So by the end of the loop, all of the arrays are filled with the same thing: the last index of addStrings and addTranslation. Every time it loops it overwrites all the indices before it with the index it's supposed to be putting in.
Anyone know what the problem is here? Thanks!
public class Sentence {
public String sentence;
public String translation;
Sentence() {
sentence = " ";
translation = " ";
}
}
private void popStrings() {
addStrings[0] = "我是你的朋友。"; addTranslations[0] = "I am your friend.";
addStrings[1] = "你可以帮助我吗?"; addTranslations[1] = "Could you help me?";
addStrings[2] = "我不想吃啊!"; addTranslations[2] = "I don't want to eat!";
}
//Fill Sentence array with string and translation arrays
private void popList() {
int i = 0;
Sentence temp = new Sentence();
for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
temp.sentence = addStrings[i];
temp.translation = addTranslations[i];
sentences[i] = temp;
}
}
You need to create new Sentence() inside the loop:
for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
Sentence temp = new Sentence();
temp.sentence = addStrings[i];
temp.translation = addTranslations[i];
sentences[i] = temp;
}
Otherwise you set sentence and translation continuously in the same object.

Categories

Resources