string - Scanner Skipping Numbers Java -


i writing code search through document , find numbers , put them array. here piece of document:

 username sam movies id 1 citizen kane id 2  vertigo id 3  rules of game... 

when run program nextword skips on number after id , uses next word. need read number can put number in array.

please help. thank you!

package arrayinversions; import java.io.*; public class main{ public static void main (string[]args)    throws filenotfoundexception{     textreader read = new textreader("/users/name/desktop/movie-data.json");      int[] arraysam = new int[1000];     int[] arrayterry = new int [1000];     int[] arraydana = new int [1000];     int temp;     int i=0;      string nextword;     string name=null;     string id="id";      nextword=read.getword();     while (nextword!=null){         if (nextword.compareto("username")==0){             nextword=read.getword();             name=nextword;             system.out.println("name: "+name);             nextword=read.getword();             i=0;         }         system.out.println("* "+nextword+"="+id);          if(nextword.compareto(id)==0){             nextword=read.getword();             system.out.println(nextword);             temp=integer.valueof(nextword);              if (name.compareto("sam")==0){                 system.out.println("sam");                 arraysam[i]=temp;                 i++;             }             else if (name.compareto("terry")==0){                 system.out.println("terry");                 arrayterry[i]=temp;                 i++;             }             else{                 system.out.println("dana");                 arraydana[i]=temp;                 i++;             }         }         nextword=read.getword();             }         }         } 

package arrayinversions; import java.util.*; import java.io.*;  public class textreader {  private scanner read; private string currline;  public textreader(string filename){ try{     currline = "";     read = new scanner(new file(filename)); } catch (exception ex){                    system.out.println("file not exist error: "+ex.tostring()); } } private static boolean isletter(char ch) {     return ((ch >= 'a')&&(ch <= 'z') ||             (ch >= 'a')&&(ch <= 'z') ||                 // (ch <= '1')&&(ch <= '9') ||  //allows numbers?             (ch == '-') || (ch == '\'')             );  } private string removenextword(string s) {     //returns string first 'word' removed     //first,  pull non-letters off front     while ((s.length()>0) && (isletter(s.charat(0))== false))       s = s.substring(1);     //now, pull letters off front     while ((s.length()>0) && (isletter(s.charat(0))== true))       s = s.substring(1);     //finally remove non-letters off front     while ((s.length()>0) && (isletter(s.charat(0))== false))       s = s.substring(1);      return s;                             //return resulting string } private string getnextword(string s) {     //returns first 'word' of string     //first,  pull non-letters off front     string word="";     while ((s.length()>0) && (isletter(s.charat(0))== false))       s = s.substring(1);     //now, keep letters pull them off front     while ((s.length()>0) && (isletter(s.charat(0))== true))     {   word = word + s.charat(0);  //build word         s = s.substring(1);         //remove letters string input     }     return word;                    //return resulting word string }  public string getword(){  // throws filenotfoundexception  //required throw line {   string nextword;      while ((currline != null)&&(currline.length()== 0)){         if (read.hasnext())           currline = read.nextline().trim();         else           currline = null;     }     if (currline != null)     {    nextword = getnextword(currline);   //get word front of line          currline = removenextword(currline).trim();  //update line w/o word     }     else     {          nextword = null;     }    return nextword; } } } 

your removenextword() method greedy i.e. it's removing numbers well.

in implementation, first remove that's not letter before word, word , characters following word not letters removes id numbers well.

perhaps, should uncomment line , rename isletter() istoken().

 (ch >= '0') && (ch <= '9') ||  // allow numbers 

edit: please fix conditional check numbers. ch <= '1' incorrect.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -