Results 1 to 3 of 3
- 04-16-2012, 12:22 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Reading input from a .txt file and then putting it into a array
Hi I am trying to read in a text file that is
What i have so far isadd job Internet with length 6 and priority 4
add job Launchcode with length 3 and priority 3
add job Facebook with length 2 and priority 5
add job explorer with length 1 and priority 2
add job shutdown with length 5 and priority 1
Java Code:String[] myarray; String fileName; FileInputStream in = new FileInputStream("input.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; myarray = new String[10]; for (int j = 0; j < myarray.length; j++) { myarray[j] = br.readLine(); } in.close(); System.out.print(myarray);
From there i need to extract the name of the job the length and then the priority and put it all into a priority queue. I am tackling this one step at a time and one problem at a time. This is a homework assignment I just need some help, I am not asking for answers. I just don't know how to pull information from a string? Like my plan is to do a for loop to go through each value in my array then another value to pull out the 4th 7th and 9 word from this string and set them name to a string variable and the numbers to integer variables. From there I plan on implementing a Priority queue ADT.
- 04-16-2012, 12:48 AM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Re: Reading input from a .txt file and then putting it into a array
I'm not sure if this is the simplest way, but you could use the String Classes indexOf(String str) member function to find the index of the starting of each item your interested in, i.e.
indexOf("with length") and indexOf("and priority").
If the priority is always a single digit, you won't have to use indexOf("and priority"). You can just compute that using indexOf("with length"). In other words, the length and priority can be found by and offset of indexOf("with length"). and of course the job is just a fixed length.
Once you have the index positions you can use substring(int beginIndex, int endIndex) to get the string value. You might have to convert the priority and length to integers using Integer.parseInt( String string ).
- 04-16-2012, 12:56 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Reading input from a .txt file and then putting it into a array
What I am doing is I have a for loop to access a line of my .txt file then I split that string , and then grab the values i need. But thank you for the datatype help I can see me having that question later. I Think I am good for the rest of this until i get to implementing the priority queue. But those problems are probably better suited for a different thread?
Similar Threads
-
Reading input and storing into array
By joeyalf in forum New To JavaReplies: 1Last Post: 12-04-2011, 08:08 PM -
Help with reading input file
By mgeno216 in forum New To JavaReplies: 17Last Post: 02-11-2011, 10:04 PM -
Need help wit reading txt file into input.
By ShinTec in forum New To JavaReplies: 7Last Post: 02-02-2010, 04:40 PM -
reading input from array and saving output to database md5 algorithm
By avinash4m in forum Advanced JavaReplies: 4Last Post: 03-12-2009, 11:25 AM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks