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

Thread: Need help with code

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

    Default Need help with code

    I need help with my code. Nothing is being printed out. I know the lists work because blocked out the ifs and tried both one at a time. What im trying to do is get the if statment to work if i type in Group1 to run the first list and group2 to run the second if. Or instead of the ifs, can someone tell me how i run an argument to run the first list and a second argument to run the second list? Im still kind of confused about what an argument is. One of my friends said its like in the cmd when you type ipconfig /renew and "/renew" is argument. Im using eclipse to make my program.
    Thanks

    import java.util.ArrayList;
    import java.io.*;
    public class Sorting {
     
    	public static void main(String[] args) throws IOException{
    		// TODO Auto-generated method stub
    		String userInput;
    		String command1= "Group1";
    		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    			userInput = dataIn.readLine();
    			String menu = userInput;
    		//List 1
    			if ( menu == command1) {
    				List input1 = new List("groupA");
    				List input2 = new List("groupB");
     
    				ArrayList<String> myList = input1.getList();
    				ArrayList<String> myList2 = input2.getList();
     
     
    				myList.removeAll(myList2);
     
    				System.out.println("myList =" + myList);
    				System.out.println(myList.size());
    				}
    		//List 2
    			if ( menu == "Group2") {
    				List input1 = new List("groupC.txt");
    				List input2 = new List("groupD.txt");
     
    				ArrayList<String> myList = input1.getList();
    				ArrayList<String> myList2 = input2.getList();
     
     
    				myList.removeAll(myList2);
     
    				System.out.println("myList =" + myList);
    				System.out.println(myList.size());
    				}
    	}
    }


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

    Default Re: Need help with code

    Hey redzero, i've been read your code and when you compare two string, you can not compare as objects. I mean, the object String is compared with the equals function. In your code you're compare memory directions. So, my hint is that you replace "==" with equals() method and try again.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need help with code

    Another recommendation: if the comparisons in the if statements are mutually exclusive ie only one of them can be true, then use if/else if with a trailing else clause that prints out a message saying that none of the above if statements were true and show the value of the data. This can be useful when debugging.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with code

    Thanks AndyNewman yours did the trick. and I'll keep Norms advice in mind if i run into any problems. I was told to have arguments in this program so i can run actions through a command line to choose which list i want to print out or is the ifs the arguments? Any advice.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need help with code

    arguments in this program
    Is that from the commandline? Then the arguments will be in the args array.

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with code

    args array? I've been trying to figure out what that is. would you mind showing me an example or directing me to a correct tutorial, I've been trying to read articles on google, but don't quite understand whats happening and how to implement them in my code.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need help with code

    If your command line is:
    java YourProgram the arguments are
    then the args array passed to the main() method like this:
    String[] args = {"the", "arguments", "are"};

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with code

    Okay, I kinda understand what your saying. So how would I make create an argument to only output the list1/list2 of in my code? I kinda understand how to type it in a command line but not into a program. Would this be done by slitting it up by classes, then the command line runs the class?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need help with code

    So how would I make create an argument
    The java command creates the args array. You ask it to put values into the array by using a commandline like this:
    java YOURCLASSNAME the args go here
    This would put 4 Strings into the args array that is passed to the main() method.

    For testing write a very small class with only a main() method that prints out the args it receives.
    Then execute it with different command lines and see what you get and what prints out.

  10. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with code

    hmm okay I think I completely understand what an argument is now. So is an argument pretty much the input of what the user types in or a string array correct? Is it possible to put it in my if statement and see if its equal a string i want?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need help with code

    is an argument pretty much the input of what the user types in
    Yes it is what the use puts on the command line
    Is it possible to put it in my if statement and see if its equal a string i want?
    Yes the String[] args variable passed to the main() method can be used anywhere in the main() method just like any argument. It can also be passed to other methods or a constructor.

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Need help with code

    People use argument and parameter interchangably.

    System.out.println("hello");

    The argument to the println method is the String "hello". What Norm was explaining is that when you launch a java application you can pass in values via the args array. They would be the arguments to the main method.

    What you have to get clear in your head: is your assignment asking for values to be passed in via the args array or some other method. If you do use the args array then it would be a simple matter of using those values in your if statements. You can access them just like you would access values in any array, by using the correct array notation.
    Improving the world one idiot at a time!

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with code

    Cool thanks everyone for your help. I was able to finish my program, time to start my next one!!! I ran into another problem in my new program, but i wanna see if i can fix it myself. Thanks everyone.