Results 1 to 2 of 2
  1. #1
    HiddenSquid is offline Member
    Join Date
    Apr 2016
    Posts
    1
    Rep Power
    0

    Default couple of questions on data structures and sorts.

    Hello, everyone I am new to the java world. I have a quick couple of questions that I need help with.

    I am currently working on a misson count program that reads from a file and then I am suppose to take that information from scanner and then store them into a data structure, so my question is what is the best data structure for storing strings in java? I also can't sort the data manually or use the collections.sort();. I'm to use to C#/c++ so I don't know where to start.

    I'm just struggling trying to figure out java and make it work any help would be appreciated.
    Java Code:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.regex.Pattern;
    import java.util.Arrays;
    import java.util.*;
    public class MissionCount
    {
        // returns an InputStream that gets data from the named file
        private static InputStream getFileInputStream(String fileName) throws Exception
        {
        InputStream inputStream;
    
        try {
            inputStream = new FileInputStream(new File(fileName));
        }
        catch (FileNotFoundException e) {       // no file with this name exists
            inputStream = null;
            throw new Exception("unable to open the file -- " + e.getMessage());
        }
        return inputStream;
        }
        
    
        public static void main(String[] args) {
        
        if (args.length != 1) {
            System.out.println("USage: MissionCount <datafile>");
            System.exit(1);
        }
        
        try {
    
    
            InputStream log = getFileInputStream(args[0]);
            Scanner sc = new Scanner(log);
            sc.useDelimiter(Pattern.compile(",|\n"));
            
            while (sc.hasNext())
            {
    
                String crewMember = sc.next();
                String mission = sc.next();
                  // Add code to read in the data and store it in a data structure here
            }
                
       
            
            sc.close();
    
            // Add code to print the report here
                
    
        }catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: couple of questions on data structures and sorts.

    It all depends on what you want to do with the Strings. A List implementation is fine for holding strings. So is a Set or a Map. A TreeSet automatically sorts the strings as they are added to the set.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Couple of questions about a program
    By BobbyH in forum AWT / Swing
    Replies: 1
    Last Post: 10-10-2013, 09:46 AM
  2. A couple GUI questions.
    By SpicyHero in forum New To Java
    Replies: 7
    Last Post: 09-22-2013, 04:30 AM
  3. A couple of questions
    By perfidia in forum Java Applets
    Replies: 0
    Last Post: 06-14-2013, 11:05 AM
  4. Couple questions
    By L2Code in forum New To Java
    Replies: 7
    Last Post: 03-22-2013, 09:54 PM
  5. Couple of questions regarding threading
    By exernet in forum New To Java
    Replies: 1
    Last Post: 12-15-2009, 12:23 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
  •