java - Multitask homework for begginers, looking for advice, -



java - Multitask homework for begginers, looking for advice, -

i have problem task.

my job

grab string 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

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -