Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By diamonddragon

Thread: Reading from txt file to arraylist

  1. #1
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Reading from txt file to arraylist

    Hi, i'm working on my hw and I have a problem. I know it's a lot of reading but i would be great full if someone can answer my questions.
    My code doesn't compile.
    My question is, how should I set up my while loop so it reads data from txt file, creates new Packet (with id, s and w as parameters) and then adds the packet object to arraylist shipment ?
    Also how should i set up the constructor in packages.

    Thanks.




    The assignment is :
    Packet objects have unique ID number, state abreviation for their destination, and weight in pounds with 2 decimals.
    Class Packet describs packets and has variables idNumber, state, and weight of type int, String and double, respectively. In addition, it has the following methods
    isHeavy that returns true when packet is above 10 pounds, and false otherwise.
    isLight that returns true when packet is below 5 pounds, and false otherwise.
    toString returns String which is one line representation of Packet objects.


    Create an input file called packetData.txt with the following 7 lines:
    1001 7.37 CA
    1002 5.17 CT
    1003 11.35 NY
    1004 3.17 MA
    1005 9.99 FL
    1006 14.91 VT
    1007 4.97 TX


    Each line in the packetData.txt file has information about one packet object.

    Class Packages has shipment and totalWeight variables. Variable called shipment contains the collection of all packets. Variable shipment is of ArrayList type. All objects in the ArrayList are of of Packet type. Variable totalWeight is initialized to 0.0. Totalweight specifies the total weight of all packets. The constructor assigns variables shipment and totalWeight (by reading the data from the input file specified above. Each line in the file has data about one packet object and data from it should be assigned to a Packet object first and then added to the collection. Use input file called packetData.txt to read data from the file. Class Packages has methods

    toString which returns String representation about entire collection of packets with one packet object specified per line.
    displayLightPackages which displays all packets that are light.
    displayHeavyPackages which displays all packets that are heavy .
    displayOtherPackages which displays all packets that are neither light nor heavy.
    displayTotalWeight displays total weight of all packets with full sentence. (Use simplest code)
    displayAverageWeight displays average weight (with two decimals) of all packets. Use full sentence. (Use simplest code)


    Your application should also have class TestPackages with only main method in it, in addition to classes Packet and Packages.

    This is my code.


    Java Code:
    public class Packet
    {
        private int idNumber;
        private String state;
        private double weight;
        
        public Packet(int id, String s, double w)
        {
         idNumber = id;
         state = s;
         weight = w;
        }
    
       
        public boolean isHeavy(double y)
        {
          return ( y > 10 );  
        }
    
        public boolean isLight(double y)
        { 
          return (y < 5);
        }
        
        public String toString()
        {
         String result = "Packet id" + idNumber;
         result += state + "\t" + weight;
         
         return result;
        }
    }

    Java Code:
    public class Packages
    {
       
       
       
    
      public Packages()
        {
          public ArrayList<Packet> shipment;
          totalWeight = this.displayTotalWeight();
        }
    
        
        public void displayLightPackages()
        {
            System.out.println("The light packages are : ");
            for(packet mypacket : shipment )
              if (mypacket.isLight)
                 System.out.println(mypacket);
        }
        
        public void displayHeavyPackages()
        {
           System.out.println("The heavy packages are : ");
           for(packet mypacket : shipment)
             if (mypacket.isHeavy)
               System.out.println(mypacket);
         }
        
         public void displayOtherPackages()
         {
          for(packet mypacket : shipment)
            if ( mypacket.isHeavy || mypacket.isLight)
               System.out.println("No other packages"):
               else 
                 System.out.println(mypacket);
          }  
         
          public void displayTotalWeight
          { 
           
           for(packet mypacket : shipment) 
              total += weight;
           System.out.println("Total weight is: " + totalWeight);
          }
      
          public void displayAverageWeight
          {
           double hold:
           int count;
           for(packet mypacket : shipment)
              {
               hold += weight;
               count++;
               }
            System.out.println("The average weight it " + hold / count);  
              
          }  
          
     }
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class TestPackages
    {
       
        public static void main (String[] args) 
        {
          Packages shipment = new ArrayList<String>();
          String currentPacket;
          Scaner fileScan, packetScan;
          
          fileScan = new Scanner (new file("packetData.txt"));
        
          while (fileScan.hasNext()){
           currentPacket = fileScan.nextLine();
           
           packetScan = new Scanner (currentPacket);
              
               while (packetScan.hasNext()){
                  id = packetScan.nextInt();
                  s = packetScan.next();
                  w = packetScan.nextDouble();
              
                }
              pocket one = new packet (id, s, w);
              shipment.add(one);
        
        
        }
    
        
    }
    
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Reading from txt file to arraylist

    My code doesn't compile.
    What is the compiler message? And what line(s) of your code does it refer to?

  3. #3
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading from txt file to arraylist

    The compiler error refers to constructor in Packages class(public ArrayList<Packet> shipment;) , but i was hoping someone can give me some tips on how to set up the loop in TestPackages because the one i have isn't correct.

  4. #4
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading from txt file to arraylist

    ok i have been working on my code, it compiles now, but it still doesn't work as i wanted it to do.
    My question is, how should i set up my arraylist "thisone" in main method, so it can use the methods from packages class??
    please answer.

    here is my code

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
    
    
    public class TestPackages extends Packages
    {
       
        public static void main (String[] args) throws IOException 
        {
          
          String currentPacket;
          Scanner fileScan, packetScan;
          
          ArrayList<Packet> thisone = new ArrayList<Packet>();
          
          
          fileScan = new Scanner(new File("packetData.txt"));
          while (fileScan.hasNext()){
            currentPacket = fileScan.nextLine();
            packetScan = new Scanner (currentPacket);
            
               int id = packetScan.nextInt();
               double w = packetScan.nextDouble();
               String s = packetScan.next();
                
              Packet temp = new Packet (id, w, s);
              thisone.add(temp);
            }
         
             System.out.println(thisone);  
             
            
      }
    }
    Java Code:
    import java.util.ArrayList;
    
    public class Packages
    {
       public ArrayList<Packet> shipment;
       double totalWeight ;
       
       
       
        public Packages()
        {
          shipment =  new ArrayList<Packet>();
          totalWeight = 0.0; 
        }
    
        
        public void displayLightPackages()
        {
            System.out.println("The light packages are : ");
            for(Packet mypacket : shipment )
              if (mypacket.isLight())
                 System.out.println(mypacket);
        }
        
        public void displayHeavyPackages()
        {
           System.out.println("The heavy packages are : ");
           for(Packet mypacket : shipment)
             if (mypacket.isHeavy())
               System.out.println(mypacket);
         }
        
         public void displayOtherPackages()
         {
          for(Packet mypacket : shipment)
            if ( mypacket.isHeavy() || mypacket.isLight())
               System.out.println("No other packages");
               else 
                 System.out.println(mypacket);
          }  
         
          public void displayTotalWeight(){ 
           for(Packet mypacket : shipment) 
              totalWeight += mypacket.weight;
           System.out.println("Total weight is: " + totalWeight);
          }
      
          public void displayAverageWeight()
          {
           double hold;
           int count;
           
           for(Packet mypacket : shipment)
              {
               totalWeight = shipment.size();
               }
            System.out.println("The average weight is " + totalWeight);  
              
          }  
       
          public String toString()
          {
            String result = "The shipment \n";
              
            for(Packet mypacket : shipment)
               result += mypacket;
            
            return result;
            
            }
          
          
    }

    Java Code:
    public class Packet
    {
        private int idNumber;
        private String state;
        public double weight;
        
        public Packet(int id, double w, String s)
        {
         idNumber = id;
         state = s;
         weight = w;
        }
    
       
        public boolean isHeavy()
        {
          return ( weight > 10 );  
        }
    
        public boolean isLight()
        { 
          return (weight < 5);
        }
        
        public String toString()
        {
         String result = "\nPacket id: " + idNumber + " State: ";
         result +=  state + " Weight: " + weight;
         
         return result;
        }
    }

  5. #5
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading from txt file to arraylist

    I think You should implement reading from file in packages class constructor, not in testing program.

    When You create an instance of class Packages, You will have packet objects(instances of packet class) inside packages object.
    Packet objects inside packages object is represented by packages object data field(instance field) of ArrayList type, (that holds references to packet objects).
    Last edited by diamonddragon; 02-10-2012 at 05:57 AM.
    artur likes this.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. reading a file and writing to a file....help!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 07:53 PM
  3. Replies: 9
    Last Post: 10-20-2009, 11:52 AM
  4. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  5. Reading API for ArrayList
    By kian_hong2000 in forum New To Java
    Replies: 10
    Last Post: 08-22-2008, 07:09 PM

Posting Permissions

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