java - How would I add a return statement for this, and how do I call the method? -
public class inputfiledata { /** * @param inputfile file giving data electronic * equipment supplier’s product range * @return array of product details * @throws ioexception */ public static product [] readproductdatafile(file inputfile) throws ioexception { // code goes here (input data text file , sort arraylists) }
readproductdatafile
used read text file, , store in array of type product[]
. code provided cannot changed, need way works code. i've managed make file reading , sorting array lists work in different class, running in way giving me couple of problems:
1) can't call readproductdatafile
method main
class, if can't find method (it's in correct package).
2) can't figure out how format return statement, i've tried lots of different things can't see how store array type product[]
.
i haven't provided lot of specific code far because don't want answer handed me on platter (this part of assignment don't want other people straight me), able point me in right direction solve this?
to give idea of how i'm doing @ moment, following test code worked me:
electronicsequipmentdemo
class:
public class electronicsequipmentdemo { /** * @param args command line arguments */ public static void main(string[] args) throws ioexception { name inputfile = new name(); inputfile.privatename(); } }
name
class:
public class name { public string privatename() { try { filereader fr = new filereader("myoutput.txt"); bufferedreader br = new bufferedreader(fr); string str; while ((str = br.readline()) != null) { char firstletter = str.charat(0); if (firstletter == 'p') { string[] list = str.split("/"); arrays.tostring(list); string fullname = list[1] + " " + list[2] + " " + list[3] + "\n"; system.out.println(fullname); } } br.close(); } catch (ioexception e) { system.out.println("file not found"); } return null; } }
which reads text file and, if line begins p, splits arrays , prints out specified values (although attempt add return statement made return first line, still struggling there).
this should it:
public class name { public string[] privatename(){ string[] list; try { filereader fr = new filereader("myoutput.txt"); bufferedreader br = new bufferedreader(fr); string str; while ((str = br.readline()) != null) { char firstletter = str.charat(0); if (firstletter == 'p'){ list = str.split("/"); //arrays.tostring(list); string fullname = list[1] + " " + list[2] + " "+ list[3] + "\n"; system.out.println(fullname); } } br.close(); } catch (ioexception e) { out.println("file not found"); } return list; } }
edit: fixed scoping issue.
Comments
Post a Comment