java - How to create an arraylist from a file where the input for the program is IntegerType? -
forgive me if may seem elementary/answered not understanding how apply program. i'm trying create program accepts array of either integers or strings text file sorted other classes isbetterthan()
method in sort classes.
how can accept input text file line line create array of integertype or stringtype? (integer/string , file user input.)
as sits, i'm getting error either "no suitable method" array class or "incompatible types". thanks.
import java.util.arraylist; import java.util.scanner; import java.io.*; public class insert { public static void main(string[] args) throws ioexception { //variables string filename; integertype line; arraylist<integertype> alist = new arraylist<integertype>(); //ask file scanner scan = new scanner (system.in); system.out.print ("enter pathname of input file: "); filename = scan.nextline(); scanner s = new scanner(new file(filename)); //--------------------my problem while (s.hasnext()){ alist.add(s.next()); } s.close(); } } class integertype implements anytype { private int number; public integertype() { number = 0; } public integertype(int i) { number = i; } public boolean isbetterthan(anytype datum) { return (this.number > ((integertype)datum).number); } public int tointeger() { return number; } } class stringtype implements anytype { private string word; public stringtype(){ word = ""; } public stringtype(string s){ word = s; } public boolean isbetterthan(anytype datum) { return (this.word.compareto(((stringtype)datum).word) > 0); } public string tostring() { return word; } } interface anytype { public boolean isbetterthan(anytype datum); }
based on code, file contains integers , try read each token 1 one , add arraylist carry type integer.
lets take @ part try read file 1 token @ time
while (s.hasnext()){ alist.add(s.next()); }
let's break down
what s.hasnext()
means?
public boolean hasnext()
returns true if scanner has token in input. method may block while waiting input scan. scanner not advance past input. specified by: hasnext in interface iterator type string
what s.next()
means?
public string next()
<--- as see return type string
finds , returns next complete token scanner. complete token preceded , followed input matches delimiter pattern. method may block while waiting input scan, if previous invocation of hasnext() returned true.
i believe issue arias when try add string arraylist in type integer
listint.add(in.next());
good news can parse in.next()
in string
type type integer
using
integer.parseint(whatever want conver type integer);
your while loop changed
while (in.hasnext()) { string token = in.next(); listint.add(integer.parseint(token)); }
for validation goal see if input string or integer
try { integer.parseint(text); return true; } catch (numberformatexception e) { return false; }
try sample distinguishing between string , integer type
public static void main(string[] args) { string s = "1 b 2"; string[] sp = s.split(" "); (int = 0; < sp.length; i++) { if(stringorinteger(sp[i])) system.out.println(sp[i] + " type integer"); else system.out.println(sp[i] + " type string"); } } private static boolean stringorinteger(string s) { try { integer.parseint(s); return true; } catch (numberformatexception e) { return false; } }
output:
1 type integer type string b type string 2 type integer
i did not see have wrapper class name integertype:
you can use hasnextint read each token file since in type int
public boolean 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.
and since in wrapper class have constructor
integertype = new integertype(variable type int goes here);
how read variable type int?
use nextint()
means scans next token of input int.
so while loop become :
list<integertype> listint = new arraylist<>(); while (in.hasnextint()) { integertype = new integertype(in.nextint()); listint.add(it); }
and printing variable have use tointeger() wrapper class
(int = 0; < listint.size(); i++) { system.out.print(" " + listint.get(i).tointeger()); }
output:
70 35 81 8 96 64 46 57 99 19
file content:
70 35 81 8 96 64 46 57 99 19
Comments
Post a Comment