Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Beginner Java Help

Options
  • 04-12-2012 3:00pm
    #1
    Registered Users Posts: 2


    Hey guys, I was wondering if someone could point me in the right direction for some of my coursework.
    I have a .txt file with a list of names, subjects, number etc on each line like so:
    10007814	Cardiff University 	(F) Languages 	First degree 	Q06 	2%	10%
    

    And I need to extract information from the .txt file, so far I've done this:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.*;
    
    
    public class SS{
    	public SS(String line) {
    		// TODO Auto-generated constructor stub
    	}
    
    	public static void main(String[] args)  throws Exception{
    		ArrayList<SS> all = new ArrayList<SS>();
    		BufferedReader fh =
    			new BufferedReader(new FileReader("info.txt"));
    		String line;
    		while ((line=fh.readLine())!=null)
    		{
    			all.add(new NSS(line));
    		}
    		fh.close();
    		System.out.println("Print results");
    	}
    }
    

    I understand how to narrow down the results, but I don't understand how I'm suppose to get java to know that the first column in the .txt file is their number and say second column is the university, any help?


Comments

  • Registered Users Posts: 42 discodowney


    Java doesnt need to know. As long as you program it to know yourself. If it wont change you can just hard code it to accept the first piece to be a number and the next to be a uni name. There are functions to read in a string and ensure it is a number. So use that to ensure you are getting a number when you expect one and a string when you expect a string


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    It would help to understand what your assignment is?

    But I guess you should create an object class first for each line, the object will contain the variables and getters/setters for each element on each line EG: Number, University...

    Then you need to parse each line for each piece of information and then add these to each newly created object.

    Then you add this object to the ArrayList rather than the String containing the whole line from the text file.

    Then you can get the information from each object based on their position in the ArrayList.

    Does that sound correct or am I over complicating what your assignment actually is?


  • Registered Users Posts: 2 instantmash


    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.*;
    
    
    public class SS{
    	public SS(String line) {
    		// TODO Auto-generated constructor stub
    	}
    
    	public static void main(String[] args)  throws Exception{
    		ArrayList<SS> all = new ArrayList<SS>();
    		BufferedReader fh =
    			new BufferedReader(new FileReader("info.txt"));
    		String line;
    		while ((line=fh.readLine())!=null)
    		{
    			all.add(new SS(line));
    		}
    		fh.close();
    		System.out.println("Question 1:"); q00(all);
    	}
    
    
    	
    	public static void q00(ArrayList<SS> all)
    	{
    
    		for (SS ss : all)
    			if (ss.institution.equals("Oxford University") &&
    					ss.question==2)
    				System.out.println(ss.subject+" "+ss.response);
    	}
    }
    

    So, what if I go about it this way?


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    Take a look at StringTokenizer http://www.mkyong.com/java/java-stringtokenizer-example/

    You have roughly the right idea :)


  • Closed Accounts Posts: 799 ✭✭✭Logical_Bear


    just use a regex if you know what format the data in the file is going to be consistant.

    iirc i think stringtokeniser has been deprecated in the java api


  • Advertisement
  • Registered Users Posts: 437 ✭✭t1mm


    I understand that each line contains an entry, with values on each line seperated by a "tab". My approach would be:

    Create an "Entry" class with variables for each value type ("University","Course","CourseCode") etc. Let the constructor accept a string (a line from your file). In the constructor, use Java's String.split() function to assign values to the individual variables.

    In your main function, simply create an ArrayList of "Entry" objects, creating one for each line you read from the text file.

    :)


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    just use a regex

    Now the OP has two problems ;)


Advertisement