java - Multitask homework for begginers, looking for advice, -
java - Multitask homework for begginers, looking for advice, -
i have problem task.
my job
grabstring
file (in source file numbers in 1 line, dividied spaces) split string spaces then parse each string
int
and utilize bubblesort on them. i'm stucked on parsing, don't thought how it.
code atm looks sth this:
public class main { public static void main(string[] args) throws ioexception { string numbers = new string(files.readallbytes(paths.get("c:\\readme.txt"))); string s[] = numbers.split(" "); (string element : s) { system.out.println(element); } } }
i tried utilize scanner read string numbers, loop parseint, doesnt work me. appreciate help.
the method looking integer#parseint()
when using java 8 create utilize of stream api follows:
final list<integer> intlist = new linkedlist<>(); seek { files.lines(paths.get("path\\to\\yourfile.txt")) .map(line -> line.split(" ")) .flatmap(stream::of) .map(integer::parseint) .foreach(intlist::add); } grab (ioexception ex) { ex.printstacktrace(); }
without streams:
final list<integer> intlist = new linkedlist<>(); seek { (string line : files.readalllines(paths.get("path\\to\\yourfile.txt"))) { (string numberliteral : line.split(" ")) { intlist.add(integer.parseint(numberliteral)); } } } grab (ioexception ex) { ex.printstacktrace(); }
java string parsing split int
Comments
Post a Comment