Results 1 to 2 of 2

Thread: Scanner help

  1. #1
    onions is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Scanner help

    Hi, I’m trying to find capitalized names in a sentence. For example: “His name is Bugs Bunny and he loves carrots. “ The code below will get Bugs Bunny but then it skips “and” because of the scan.next(); Is there any way to NOT skip the “and”? I want the scanner to continue scanning the same sentence in case of another name and I don’t want to miss any of the tokens.

    *EDIT* I really apologize, I didn't realize i didn't post enough of the code to show the problem. The scanner goes through the sentence with a while loop, however when it finds a keyword (after the keyword might be a name) from the database in the sentence. It will check if the following word is capitalized and scans until it doesnt find a capitalized word anymore.

    Java Code:
    ArrayList<String> Persons=new ArrayList<String>();  
    int i;  
    Scanner scan=new Scanner("His name is Bugs Bunny and he loves carrots.");  
    String word,nextWord,name=" ";  
    
     while (scan.hasNext()) {
                                word=scan.next();
                                db.pst = (com.mysql.jdbc.PreparedStatement) db.con.prepareStatement("SELECT * FROM articletable");
                                db.rs = db.pst.executeQuery();
                                while (db.rs.next()) {
                                    kword=db.rs.getString(2);
                                    if(word.equalsIgnoreCase(kword))
                                    {
                                        isKword=true;
                                    }
                                    
                                }
                                if(isKword){  //if a keyword was found, check if the following words are capitalized and add to arrayList
                                    if(scan.hasNext())
                                    {
                                        nextWord=scan.next();
       
                                        if(Character.isUpperCase(nextWord.charAt(0)))
                                        {
                                            name+=" "+nextWord;
                                            if(scan.hasNext())
                                            {   
                                                nextWord=scan.next();
                                    
                                                while(Character.isUpperCase(nextWord.charAt(0)) && scan.hasNext())
                                                {  
                                                    name+=" " +nextWord;
                                                    nextWord=scan.next();
         
                                                }
                                            }
                                            if((name.trim().length() > 0)){
                                               Persons.add(name);
                                               name=" "; 
                                            }
                                        }
    
                                    }
                                    isKword=false;
                                }
    
                            }
    Last edited by onions; 04-15-2012 at 04:02 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Scanner help

    Use a while loop to go through the String checking while (scan.hasNext()), and inside of this loop, call scan.next() only once. In other words each call to scan.hasNextXXX() should be matched by a single call to scan.nextXXX().

Similar Threads

  1. How to use Scanner ?
    By sandeep43 in forum New To Java
    Replies: 7
    Last Post: 08-10-2011, 02:29 PM
  2. Using scanner for CSV
    By getName() in forum Advanced Java
    Replies: 7
    Last Post: 06-20-2010, 05:33 PM
  3. Help With Scanner
    By jtmoney0511 in forum New To Java
    Replies: 10
    Last Post: 10-13-2009, 12:24 AM
  4. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 04:37 PM
  5. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 02:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •