• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in pattern matching android

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i have written an code for pattern matching in android. there is no compilation or run time error. i have a string like this " hi how are you" and if it matches it should display the same output. but i am not getting the result alone. can any one tell where i have missed some statement it would be of great help indeed.

code is
package com.example.android.pattern;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class PatternActivity extends Activity {
TextView tt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
{
String raw_string = ("hi how are you");
Pattern p = Pattern.compile("hi how are you");
Matcher m = p.matcher(raw_string);
if (m.find()){
String res = m.toMatchResult().group(0);
tt = setText(res.toString());
}

}
private TextView setText(String res) {
// TODO Auto-generated method stub
return null;
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button android:layout_width="wrap_content"
android:id="@+id/calculate"
android:layout_x="41dip"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_y="232dip"></Button>

<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="172dip"
android:id="@+id/tt"
android:layout_height="wrap_content"
android:layout_y="232dip"></EditText>

</LinearLayout>

can any one tell what is the error. i have attached the emulator link here
output-for-pattern-matching-javaranch.png
[Thumbnail for output-for-pattern-matching-javaranch.png]
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which statement, exactly, do you think should cause something to be displayed?
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the statement " String res = m.toMatchResult().group(0);"

and tt is for viewing the text that is matched so i have given an statement like this
tt = setText(res.toString());

is my declaration wrong for displaying the output ?
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me put it another way: there is nothing in your code that would cause anything to be displayed. The setText method should change the text that is displayed by the TextView; check the javadocs of that class for how the method is called. Currently that method does nothing. And you should definitely not reassign "tt" to null (or anything else after it's been initialized - which, actually, it isn't anywhere. You should do that in the onCreate method.)
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am unable to find out what your telling in the code and where to put in in the code. can you please help me to get the output? is the html tag is correct?
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not HTML, it's XML. I suggest you work through an introductory tutorial on how to build GUIs on Android; they cover stuff like this. As long as the code doesn't obtain a proper reference to "tt", and then calls an appropriate method on it to make it display whatever text you want it to display, nothing will be displayed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic