Results 1 to 3 of 3
  1. #1
    BetaDave1877 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default 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

    add 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
    What i have so far is
    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.

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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 ).

  3. #3
    BetaDave1877 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default 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

  1. Reading input and storing into array
    By joeyalf in forum New To Java
    Replies: 1
    Last Post: 12-04-2011, 09:08 PM
  2. Help with reading input file
    By mgeno216 in forum New To Java
    Replies: 17
    Last Post: 02-11-2011, 11:04 PM
  3. Need help wit reading txt file into input.
    By ShinTec in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:40 PM
  4. Replies: 4
    Last Post: 03-12-2009, 12:25 PM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •