How to take integer and remove other data types from the file java? -


i not know how take integer , ignore strings file using scanner. have far. need know how read file token token. yes, homework problem. thank much.

import java.io.file; import java.io.filenotfoundexception; import java.util.scanner;  public class clientmergeandsort{ public static void main(string[] args){   int length = 13;  try{   scanner input = new scanner(system.in);         system.out.print("enter file name extention : ");         file file = new file(input.nextline());          input = new scanner(file);           while (!input.hasnextint()) {            input.next();          }          int[] arraylist = new int[length];          for(int =0; < length; i++){         length++;         arraylist[i] = input.nextint();         system.out.print(arraylist[i] + " ");             }       } catch (exception ex) {         ex.printstacktrace();     }    } 

}

take @ api you're doing.

http://docs.oracle.com/javase/7/docs/api/java/util/scanner.html#hasnextint()

specifically, scanner.hasnextint().

"returns true if next token in scanner's input can interpreted int value in default radix using nextint() method. scanner not advance past input."

so, code:

while (!input.hasnextint()) {     input.next(); } 

that's going , see if input hasnextint().

so if next token - 1 character - int, it's false, , skips loop. if next token isn't int, goes loop... , iterates next character.

that's going either: - find first number in input, , stop. - go end of input, not find numbers, , hits illegalstateexception when try keep going.

  • write down in words want here.
  • use api docs figure out how hell tell computer that. :) 1 bit @ time right; has several different parts, , first 1 doesn't work yet.
  • example: read file, , display each line first. lets debugging; lets build 1 thing @ time, , once know thing works, build 1 more part on it.
  • read file first. display read it, know works.
  • then worry if has numbers or not.

Popular posts from this blog