Results 1 to 4 of 4
  1. #1
    Vivid is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default How to get pieces of text from a text file

    I have a project where i need to get the name a user put in and find it in a data file and total up the revenue for that and i need to use if statements and while loops. I'm currently stuck on how to total the revenue because there is no order in the data file i have to find the name and add up the revenue every time they're mentioned in the data file. I have no idea how to tell the program to just add up the revenue for each instance of the person. I'm currently using it with the scanner class i have most of the other programming done i'm just stuck at this point.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How to get pieces of text from a text file

    It is hard to provide advice when your problem is vague and you provide no code. However what you can do is read the entire file and store the data in Objects and those Objects in a List. Then whenever you need something simply search the List for the appropriate object and then call the appropriate method to get the info you need.

  3. #3
    Vivid is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: How to get pieces of text from a text file

    Sorry about not adding in the code this is what i have so far which is virtually nothing for the project i'm working on and to be more specific i need to have the user enter in a band name and go to a data file that has concert revenues for years 2005-2012 and add up all the different revenues for the different years for the band or artist that the person entered in. I've used the filereader to get info before but not to specifically pick certain things out of it, i've only went in order in what was in the textfile not to add specific things up in the file. I hope i was a little more specific for you and sorry if the code doesn't really help you at all I'm having a heard time learning programming.



    import java.util.*;
    import java.io.*;

    public class Project1
    {
    public static void main (String []args)throws FileNotFoundException

    {
    Scanner console = new Scanner(System.in);

    //variables
    String artistName;
    String artist;
    int year;
    int concertRevenue;
    int count = 0;
    int number;
    int Sum = 0;

    System.out.print("Please Enter in Artist or Bands Name: ");

    artistName = console.nextLine();

    Scanner inFile = new Scanner(new FileReader ("Project1ArtistList.txt"));

  4. #4
    wsaryada is online now Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: How to get pieces of text from a text file

    You have created a Scanner object to read the file. The next step is to read lines from the file. You can use the nextLine() method of the Scanner object. To read the entire file you'll need to do it in a loop, for example a while loop. To check whether the file has more lines to read you can use the hasNextLine() method of the Scanner object.

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  3. Replies: 1
    Last Post: 02-28-2011, 05:30 PM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  5. Replies: 8
    Last Post: 11-06-2009, 06:52 AM

Posting Permissions

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