IProblems with Scan, Strings and TextArea

I'm trying to build in a search button. Upon clicking the search button, it will read in the text from JComponent outTextArea.

It will read it each word, and compare each read-in word to the word I'm searching for. My problem is, it works, sorta. It only reads the last word from the outTextArea.

This is the code snippet

if(e.getActionCommand().equals("Search")) { String strSearchString = searchTextField.getText(); System.out.println(strSearchString); String text = outTextArea.getText(); System.out.println(text); Scanner sc = new Scanner(text); while(sc.hasNext() == true) { String s = sc.next(); if (s.equals(strSearchString) == true) { searchOutLabel.setText("Yes"); } else { searchOutLabel.setText("Non!"); } } sc.close(); }

If I add break; after else, it'll search the first word. So it tells me my logic must be flawed somewhat and it can't be done this way.

-------------Problems Reply------------

Your problem is that it will set the text for the label for all words, but will do so so quickly that you won't have time to read it. If you want to do it slowly you'll need to use something to slow down the loop such as a Swing Timer. Also no need for

if (suchAndSuch == true)

Cleaner to simply do

if (suchAndSuch)

For example:

if (e.getActionCommand().equals("Search")) {
final String strSearchString = searchTextField.getText();
System.out.println(strSearchString);
String text = outTextArea.getText();
System.out.println(text);
final Scanner sc = new Scanner(text);
int timerDelay = 2 * 1000;

new Timer(timerDelay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (sc.hasNext()) {
String s = sc.next();
if (s.equals(strSearchString)) {
searchOutLabel.setText("Yes");
} else {
searchOutLabel.setText("Non!");
}
} else {
((Timer)e.getSource()).stop();
sc.close();
}
}
}).start();
}

Edit 1

If you want to print yes if any match has been found, then you'll need to alter your logic to set the textfield if any match is found then exit the method. If no match is found (you've reached the end of the while loop), then set the label there:

while (sc.hasNext()) {
String s = sc.next();
if (s.equals(strSearchString)) {
searchOutLabel.setText("Yes");
sc.close();
return;
}
}
searchOutLabel.setText("Non!");
sc.close();

String s = sc.next(); //Change to
String s = sc.nextLine();

Similarly change

sc.hasNext(); to
sc.hasNextLine();

Also add a break statement in your if true statement. Like this

if (s.equals(strSearchString) == true)
{
searchOutLabel.setText("Yes");
break;
}

else
{

Then I must comment on your formating preferences. Make me happy and write the above like this

if (s.equals(strSearchString)) {
searchOutLabel.setText("Yes");
break;
} else {

Category:java Views:1 Time:2011-09-07

Related post

  • flex bison C lex yacc scanning string buffers 2011-02-06

    Theres a bit of C code something like: int doCommand(char* command) { // +2 on strlen is for the two extra '' characters // needed by flex when scanning strings. YY_BUFFER_STATE yybs = yy_scan_buffer(command, strlen(command)+2); yy_switch_to_buffer(y

  • Problem of alignment of string in textarea in Flex 3.0 2010-10-07

    I need to display a long string in a TextArea in the form of two columns. Say 20 characters of the string in left side and then some space and then 20 character on right side of TextArea. At next line again I am doing same thing till my string is com

  • scan string using nsscanner class 2010-10-27

    I want to scan this string "hello I am emp 1313 object of string class 123" so in this I want to know if their are any integer value present and if present I want to display them for this I am using the NSScanner class and heres a view of my code NSS

  • Scan string for special characters and alphabets 2011-02-13

    I have a textbox which accepts time (max of 5 chars) in the format hh:mm or hhmm. Pls tell me a way I can just scan the string entered in the textbox, for special characters and alphabets? [If the string entered has these chars or alphabets, then an

  • Removing special strings from textarea/content area using jquery or javascript 2011-04-26

    I'm doing a website in php. I just got a requirement to remove some special strings like phone number, email address, facebook address etc from a textarea which users enter. I want to identify such things from the content and show an alert message to

  • Counting occurrences of a string in textarea using jQuery/Javascript not updating on new button-press 2011-10-16

    I have a button that puts the string: <dt></dt><dd></dd> on to the end of the text in a textarea with the id #upcoming_text. I'm using JavaScript to count the number of occurrences of </dd> each time that the button is p

  • Flex: find next instance of string in TextArea 2011-12-04

    I have a TextArea, a TextInput and a button. When text is entered into the TextInput the TextArea is searched for the matching string, however it only highlights the first found instance of the string. How can I do a 'find next' type of operation. pr

  • html strings in textarea 2011-12-18

    I feed a string to my textarea. The string contains HTML strings, such as &copy;, &eacute;, etc. I need these strings to be displayed as such (&copy;, &eacute;) and not be turned into © and é respectively. How would I do it? ---------

  • scanning string in c 2011-12-26

    int main( ) { char str[200]; int n,tc; scanf("%d",&tc); while(tc--) { scanf("%d",&n); gets(str); puts(str); } return 34; } Why this code is not scanning no. of strings(str) equals to tc? Platform UBUNTU 10.04 --------------Solutions----------

  • How to get the deleted character and update my string inside textarea? 2013-06-24

    I'm trying the get the string when the user starts typing with '@' and ends with '@'... So i went on with jquery .keydown() event. For example, if the i type '@[email protected]', i should retrieve the string 'testing' but the problem occurs if i delete any c

  • Scanning string into structure 2014-11-02

    Inside the text file, the first number is number of albums, second is number of tracks associated with a single album, and the number infront of each track title is the character length of the title. Right now I am having trouble scanning in the name

  • Java: scanning string for a pattern 2010-03-05

    This is probably a quicky. Why does this code not return anything? import java.util.Scanner; public class MainClass { public static void main(String[] args) { try { Scanner sc = new Scanner("asda ASA adad"); String pattern = "[A-Z]+"; while ((sc.hasN

  • How can I scan strings with multiple words multiple times in C [not C++]? 2011-06-22

    I have googled many times but I cannot find a concrete answer to my question/problem. I know fgets() allows it, as well as gets(). But if i do it multiple times, there's always an error. Multiple times mean, e.g. I want to ask for his first name, the

  • Replace/Add string in textarea in js 2011-08-03

    I have a textarea and some buttons. Onclick of each button I have to do following: Check if textarea contains some text XXX. If contains then remove it. If not then add it. How can I do this in javascript? I have tried following but it does not work:

  • PHP function to Javascript: scan string and add words in array 2011-12-06

    Im trying to convert this php function to javascript: function sanitize_words($string,$limit=false) { preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]{1,}/u",$string,$matches,PREG_PATTERN_ORDER); return $matches[0]; } Basically, it takes this strin

  • ValueError: invalid literal for int() with base 10 and occasion EOL while scanning string literal 2011-12-01

    I am a python newbie and I'm trying to create and define a set of functions that compute the median mode and mean of a set of numbers in a list. I'm also trying to include a main function that tests all 3 functions with a given list. I've been gettin

  • how to hyphenate string / text in a textarea using jquery 2011-05-11

    i am not able to hyphenate string using jquery. it seems there is no library available . i tried following link but failed . please help if you know how to hyphenate a string in textarea according to textarea width using jquery plugin (if found) http

  • Scanning a String with a comma 2011-06-05

    I have created a Scanner with system.in. How do I allow an input to be able to have commas in it without going to the next input? For example: System.out.print("Enter City, State."); String location = scan.nextLine(); I cannot enter city,state becaus

  • Progress string parsing in C 2008-10-11

    I have the following character string: "..1....10..20....30...40....50...80..." and I need to extract all numbers from it into array. What is the best way to do it in C? --------------Solutions------------- Perhaps the easiest way is to use the strto

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.136 (s). 11 q(s)