Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

    I'm supposed to write a program in Java . I'm given a text file containing

    I am Hakan. My email address is hakan@cs.uh.edu, and what is your name? Hi my name is Tarikul and my favorite email address is tarikul2000@cs.uh.edu

    and im suppossed to take tarikul2000@uh.edu and hakan@cs.uh.edu and organize them acording to whether or not they have a subdomain(the "cs" part there other possibilities) or not and store it into class arrays of type Email and UniversityEmail. I'm to then take a user iput of 0-7 and depending on the input print out a different set of info I've created the classes and but i dont know how i can take the info then sort it. The possible subdomains are 1.art 2. chee 3. chem 4. coe 5. cs 6. egr 7. polsci

    this is what i have so far if anyone can help me move foward I appreciate it
    import java.io.* ;
    import java.util.HashSet;
    import java.util.Scanner;
    import java.io.PrintWriter;
    import java.io.FileOutputStream;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
     
     
    public class Try {
    	public static void main(String[] args) {		 
    		Email [] storage;// email is a class that was made to store the data
    		storage = new Email [99];
    		UniversityEmail[] save;
    		save= new UniversityEmail[99];
     
    		HashSet<String> hs = new HashSet<>();
    		Scanner input= null;
    		PrintWriter output= null;
    		try {
    			input= new Scanner("inputemails.txt");
    			output= new PrintWriter("outputemails.txt");
     
    			}
     
    		 catch (FileNotFoundException e) {
    			System.out.print("File not found");
    			System.exit(0);}
    		String line = null;
     
    		while(input.hasNextLine())
    		{
    		fillEmailsHashSet(line, hs);
    		}
     
     
     
     
    		input.close();
    		output.close();
    	}
     
     
    public static void fillEmailsHashSet(String line,HashSet<String> container){
        Pattern p = Pattern.compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})");
        Matcher m = p.matcher(line);
     
        while(m.find()) {
            container.add(m.group(1));
        }
     
    }}

    The Email and UniversityEmail are in seperate code chunks i can post if it helps


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

    I'm going to venture a guess that both Email and UniversityEmail have a String variable containing the email. If so then it is a matter of iterating through each element in the UniversityEmail array and checking the aforementioned String variable for a subdomain, based on user input, and then output whatever matches. Another guess here is that if 0-7 is pressed you will output all emails that correspond to the subdomain, or lack there of, associated with the number. Seeing the other classes could help a bit and also I don't see any code for taking user input (is that an issue in itself). Hope some of that helps.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

    taking user input isnt a problem i jus have gotten around to it yet, i was more concerned with sorting info

    here is the Email class
    public class Email {
     
    	public String username;
    	public String email1;
    	public String extension;
    	public Email()
    	{
    		//default construntor
     
    	}
     
    	public Email(String U, String Em , String Ex)
    	{
    		username =U;
    		email1= Em;
    		extension=Ex;
     
    	}
    	public void print()
    	{
     
    	}
    }
    and here is the UniversityEmail
    public class UniversityEmail extends Email {
     
    	public String department;
    	public int code;
    	public UniversityEmail()
    	{//default constructor 
    	}
    	public UniversityEmail(String U, String Em , String Ex
    			, String Dep,int C)
    	{super( U, Em , Ex);
    	department= Dep;
    	code=C;
     
    	}
    	public void print()
    	{
     
    	}
     
    }

  4. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

    This may seem like a dumb question but I want to be certain. By "sort" are you referring to adding the email to either the Email/UniveristyEmail array or actually sorting those arrays, perhaps by subdomain/alphabetically?
    At any rate for the first possibility, take this sudo code:
    while(check input text for next line)
        {
            if next line of input contains art, chee, chem...etc then
                extract required info
                add new UniversityEmail to UniversityEmail array
            else if it only has something like the @ symbol then
                extract required info
                add new Email to Email array
        }
    As for the second possibility, where you sort by subdomain/alphabetically you would still do the above then manipulate the arrays as needed.

    One thing you haven't mentioned is whether or not your code successfully parses the email addresses from the input.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

    if anyone can help me move foward I appreciate it . . . taking user input isnt a problem i jus have gotten around to it yet
    Volunteers invest their time freely (as in free beer and freedom) to do their best to help you based on what you tell us and in response to the specific questions you ask. Please make the best use of the time invested to help you by asking specific questions, and please don't turn away or trivialize the help you are offered.

    --- Update ---

    if anyone can help me move foward I appreciate it . . . taking user input isnt a problem i jus have gotten around to it yet
    Volunteers invest their time freely (as in free beer and freedom) to do their best to help you based on what you tell us and in response to the specific questions you ask. Please make the best use of the time invested to help you by asking specific questions, and please don't turn away or trivialize the help you are offered.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

    By sort i do mean jus puting the info in the appropriate arrays and yes the code for it to print out is

    for (String string : hs) {
    System.out.println(string);

    i had taken it out cause it wasnt part of what i need to turn in

  7. #7
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

    Ok so like the sudo code suggest loop through the input, text file in this case, then parse out the email, check if it contains a subdomain (or not) and place it in the respective array. Once you have given that a shot post the code that you have tried and any problems with it or let us know if all is well.

  8. #8
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Taking info from a text file, sorting it, save into class arrays and reprinting into new txt file

    so ive looked around the web for a way to check the string line and i got
    	while(input.hasNextLine())
    		{
    		fillEmailsHashSet(line, hs);
    		if(input.contains("art"))
    			{emails[i]=hs.toArray(new String[hs.size()]);
    			}
    		if(input.contains("chee"))
    		if(input.contains("coe"))
    		if(input.contains("cs"))
    		if(input.contains("egr"))
    		if(input.contains("polsci"))
    		i++;
     
    		}
    but i get two errors first that in the input.contains and input.matches
    "The method contains(String) is undefined for the type Scanner"
    and the second is
    Type mismatch: cannot convert from String[] to String
    at
    {emails[i]=hs.toArray(new String[hs.size()]);}
    any idea how to fix this?

    also i discovered i dont need to put the emails into arrays of class types but store the class objects in an array

Similar Threads

  1. Button to save data in text fields to a File
    By puuts in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 24th, 2013, 10:07 AM
  2. Save array to text file
    By JGW in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2013, 10:45 AM
  3. Reading from a txt file into 3 arraylists and save it as double
    By depi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2011, 02:25 PM
  4. [SOLVED] Storing info into a text file & adding to it
    By BlackFlame in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 09:06 PM
  5. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM