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 2 of 2

Thread: Basic Java question

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic Java question

    I'm just starting to learn some Java from a .NET, C#, and C++ background. Anyways the code presented to me is below, with a few basic questions following:

    package com.SCM.FRS;
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Writer;
    import java.util.ArrayDeque;
    import java.util.Collection;
    import java.util.Deque;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Vector;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
     
    public class FileComparison {
     
    	/**
    	 * 
    	 * @param f file to read in
    	 * @return rows in the file
    	 * @throws IOException
    	 */
    	private static Deque<String[]> getCollumns(String f) throws IOException{
    		FileInputStream fstream = new FileInputStream(f);
    		DataInputStream input = new DataInputStream(fstream);
    		BufferedReader in = new BufferedReader(new InputStreamReader(input));
    		//Header
    		String line = in.readLine();
    		//Header 2 then collumns 
    		Deque<String[]> values = new ArrayDeque<String[]>();
     
    		while (in.ready()){
    			line = in.readLine();
    			String[] types = line.split("\\|");
    			values.add(types);
    		}
    		return values;
    	}
     
    	/**
    	 * 
    	 * @param args [0] is the new file [1] is original
    	 * @throws IOException
    	 */
    	public static void main (String args[]) throws IOException{
    		if (args.length != 2){
    			System.out.println(args.length);
    			System.out.println("This program must have two inputs");
    			System.out.println("First is the new file, second the original");
    			System.exit(0);
    		}
    		Deque<String[]> newFile = getCollumns(args[0]);
    		Deque<String[]> oldFile = getCollumns(args[1]);
    		//I am assuming header is the same for both of them
    		String[] headers = newFile.pop();
    		oldFile.pop();
    		HashMap <String, Integer> hash = new HashMap <String, Integer>();
    		HashMap <String, String> examples = new HashMap <String, String>();
    		//HashMap <String, File> files = new HashMap <String, File>();
    		String fileBase = args[0].substring(0,args[0].lastIndexOf("."));
    		File report = new File(fileBase + "_report_summary.txt");
    		report.createNewFile();
     
    		HashMap <String, Vector<String>> buffs = 
    			new HashMap <String, Vector<String>>();
     
    		for (int i = 0; !newFile.isEmpty(); i++){
    			String[] newInput = newFile.pop();
    			String[] oldInput = oldFile.pop();
    			for (int j = 0; j < newInput.length && j < oldInput.length; j++){
    				if (!newInput[j].matches(oldInput[j])){
    					String message = "On row " + i + " was: " + oldInput[j] 
    					                          + " is now " + newInput[j] + "\r\n";
    					if (hash.containsKey(headers[j])){
    						hash.put(headers[j], hash.remove(headers[j]) + 1);
    						/*File f = files.get(headers[j]);
    						Writer output = new BufferedWriter(new FileWriter(f, true));
    						output.append(message);
    						output.close();*/
    						Vector <String> vec = buffs.get(headers[j]);
    						vec.add(new String(message));
    					}else{
    						Vector <String> vec = new Vector<String>();
    						vec.add(new String(message));
    						buffs.put(headers[j], vec);
    						hash.put(headers[j], 1);
    						examples.put(headers[j], message);
    					/*	File f = new File (fileBase + "_" + headers[j] + ".txt");
    						f.createNewFile();
    						Writer output = new BufferedWriter(new FileWriter(f));
    						files.put(headers[j], f);
    						output.write(message);
    						output.close();*/
    					}
    				}
    			}
    		}
    		Collection<String> examplesDump = examples.values();
    		Collection<String> examplesKeys = examples.keySet();
    		Iterator<String> itter = examplesDump.iterator();
    		Iterator<String> itterKeys = examplesKeys.iterator();
    		while(itter.hasNext()){
    			Writer output = new BufferedWriter(new FileWriter(report, true));
    			String key = itterKeys.next();
    			output.write(key + "has been changed " + hash.get(key) + "times\r\n");
    			output.write(itter.next() + "\r\n");
    			output.close();
    		}
     
    		Collection<Vector<String>> buf = buffs.values();
    		Collection<String> bufKeys = buffs.keySet();
    		Iterator <Vector<String>> itt = buf.iterator();
    		itterKeys = bufKeys.iterator();
    		ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(fileBase + "_report.zip"));
     
    		while(itt.hasNext()){
    			/*File f = new File (fileBase + "_" + itterKeys.next() + ".txt");
    			f.createNewFile();
    			Writer output = new BufferedWriter(new FileWriter(f));*/
    			String name = new File(args[0]).getName();
    			name = name.substring(0,name.lastIndexOf("."));
    			zip.putNextEntry(new ZipEntry(name + "_" + itterKeys.next() + ".txt"));
     
    			Vector <String> messages = itt.next();
    			for (int i = 0; i < messages.size();i++){
    				zip.write(messages.elementAt(i).getBytes());
    			}
    			zip.closeEntry();
    		}
    		zip.close();
    	}
    }
    1. In the line that says "private static Deque<String[]> getCollumns(String f) throws IOException{" what is the getCollumns(string f) ... part mean? So far I've only understood that you have private static and then function name
    2. The comment says f is the file to read in, however where do you set this variable to point to the file you want the program to read?


  2. #2
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Basic Java question

    Hello fred2028,
    getColumns would be the name of the method and (String f) shows that the method takes 1 parameter of type string. The Deque<String[]> part is the method's return type. As for setting f, you can set it by using a String literal when you call the method e.g. getColumns("C:\some_text_file"), or you could create a String variable in the main method like String file = "C:\some_text_file" and then pass that in the method: getColumns(file).

Similar Threads

  1. Basic java question
    By erosgol in forum Java Theory & Questions
    Replies: 5
    Last Post: September 2nd, 2011, 06:24 PM
  2. Very new to Java basic question
    By loofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2011, 07:21 PM
  3. please answer some basic question
    By togaurav in forum Java Theory & Questions
    Replies: 5
    Last Post: April 16th, 2011, 08:58 AM
  4. [SOLVED] Asking what I suspect to be a very basic question
    By Noobert in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 24th, 2010, 08:42 AM
  5. How to link two classes in java to use it method
    By Sterzerkmode in forum Object Oriented Programming
    Replies: 3
    Last Post: May 13th, 2009, 07:52 AM