Results 1 to 14 of 14
  1. #1
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    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:

    Java Code:
    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
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Basic Java question

    getCollumns(String f)
    That is the name of the method and its argument is in the ()s
    f is the file to read in, however where do you set this variable to point to the file
    f is a String that is the name of the file.
    The caller of the getColumns() method would pass it as the argument:
    .... = getColumns("THE FILE NAME HERE");

  3. #3
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Basic Java question

    Quote Originally Posted by Norm View Post
    That is the name of the method and its argument is in the ()s

    f is a String that is the name of the file.
    The caller of the getColumns() method would pass it as the argument:
    .... = getColumns("THE FILE NAME HERE");
    Thanks, I see that getCollumns is called in the main method. However, it's passing args[0] and args[1], which seems to be the parameters of the main method. How do I set the parameters (I thought the main method is where it starts, so no method would be able call it and pass stuff)?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Basic Java question

    How do I set the parameters
    I assume you mean the String array being passed to the main method. They come from the commandline:
    java YourProgram arg0 arg1 arg2

    no method would be able call it and pass stuff)
    What is the "it" you refer to?
    You can call the main method just like any other static method:
    FileComparison.main(new String[] {"arg0, arg1"});
    Last edited by Norm; 09-13-2011 at 11:24 PM.

  5. #5
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Basic Java question

    Quote Originally Posted by Norm View Post
    I assume you mean the String array being passed to the main method. They come from the commandline:
    java YourProgram arg0 arg1 arg2


    What is the "it" you refer to?
    You can call the main method just like any other static method:
    FileComparison.main(new String[] {"arg0, arg1"});
    yes, my question was about the command line. Thanks.

    I tried running it and I get the attached error, any idea how to troubleshoot that?Basic Java question-1.jpg

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Basic Java question

    Please copy and paste your error message here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  7. #7
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Basic Java question

    C:\views\fliu_view8\frsapp\Comparison\fileComparis on\src\com\SCM\FRS>javac FileC
    omparison.java

    C:\views\fliu_view8\frsapp\Comparison\fileComparis on\src\com\SCM\FRS>java FileCo
    mparison 1.csv 2.csv
    Exception in thread "main" java.lang.NoClassDefFoundError: FileComparison (wrong
    name: com/SCM/FRS/FileComparison)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)

    C:\views\fliu_view8\frsapp\Comparison\fileComparis on\src\com\SCM\FRS>

    The FileComparison.java and FileComparison.class files exist
    Last edited by fred2028; 09-13-2011 at 11:36 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Basic Java question

    Either take the package statement out of the source or change the folder where you issue the java command and change the name of the class to be the full package path and class name.
    I can't copy and paste from an image so I'll wait until you post the console contents then I can show the command line etc.

  9. #9
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Basic Java question

    Quote Originally Posted by Norm View Post
    Either take the package statement out of the source or change the folder where you issue the java command and change the name of the class to be the full package path and class name.
    I can't copy and paste from an image so I'll wait until you post the console contents then I can show the command line etc.
    OK, I've decided to run it from the src folder. The output, after referring to the 2 files, is

    Java Code:
    C:\views\fliu_view8\frsapp\Comparison\fileComparison\src>java com/SCM/FRS/FileCo
    mparison com/SCM/FRS/1.csv com/SCM/FRS/2.csv
    Exception in thread "main" java.util.NoSuchElementException
            at java.util.ArrayDeque.removeFirst(Unknown Source)
            at java.util.ArrayDeque.pop(Unknown Source)
            at com.SCM.FRS.FileComparison.main(FileComparison.java:76)
    
    C:\views\fliu_view8\frsapp\Comparison\fileComparison\src>
    Is this saying the array it's referring to does not exist?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Basic Java question

    at java.util.ArrayDeque.pop(Unknown Source)
    at com.SCM.FRS.FileComparison.main(FileComparison.jav a:76)
    At line 76 in the main method you call the pop method which eventually throws the NoSuchElementException.

    Look at line 76 and see what is wrong.
    Have you read the API doc for the pop method to see when it will throw that exception?

  11. #11
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Basic Java question

    Quote Originally Posted by Norm View Post
    At line 76 in the main method you call the pop method which eventually throws the NoSuchElementException.

    Look at line 76 and see what is wrong.
    Have you read the API doc for the pop method to see when it will throw that exception?
    Thanks, I'll read the doc and get back to you. As mentioned before, this code is not written by me and I am starting to learn Java from it.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Basic Java question

    Not a good choice then.
    You should start learning Java from the simplest bit of code and build there.
    Don't try this by leaping into something fairly complex, which relies on some API knowledge on top of any basic syntax knowledge.

    Start here, probably Trails Covering the Basics section.

    This may sound like you have to go right back to basics, but if you don't know what main(String[] args) is then I think you ought to. Considering that has been the standard entry point for C-based languages since the start.

  13. #13
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Basic Java question

    Quote Originally Posted by Tolls View Post
    Not a good choice then.
    You should start learning Java from the simplest bit of code and build there.
    Don't try this by leaping into something fairly complex, which relies on some API knowledge on top of any basic syntax knowledge.

    Start here, probably Trails Covering the Basics section.

    This may sound like you have to go right back to basics, but if you don't know what main(String[] args) is then I think you ought to. Considering that has been the standard entry point for C-based languages since the start.
    Thanks for the reply. The main thing I was confused about was how to pass in the args[] parameter since in C/C++ it's just int main() with no parameters. I didn't know you could pass in parameters from the command line like java FileComparison 1 2

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Basic Java question

    C has one of two, the second uses parameters (an argument count and an array of arguments), which is called in essentially the same way as in Java:
    myexe p1 p2 p3

Similar Threads

  1. Basic JSP question..
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-28-2009, 07:51 PM
  2. Java basic question
    By JavaJunkie in forum New To Java
    Replies: 3
    Last Post: 04-17-2009, 09:09 PM
  3. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:09 PM
  4. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:44 PM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 07:30 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
  •