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

Thread: Extremely basic -- help with multiple arguments

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Extremely basic -- help with multiple arguments

    Hello all.

    So my first assignment in my computer science class is to write a program that counts the number of vowels. Basically, running
     java main.Assignment1 "This is one argument" "A second one"
    should return
    Argument 1: 1 a, 2 e, 2 i, 1 o, 1 u
    Argument 2: 1 a, 2 e, 0 i, 2 o, 0 u
    All: 2 a, 4 e, 2 i, 3 o, 1 u
    .

    I'm very new Java. I have a working code, but it would only work for one argument:

    package main;
    public class Assignment1 {
     
    	public static void main(String args[]) {
    		String str = args[0];
     
    		int numA = 0;
    		int numE = 0;
    		int numI = 0;
    		int numO = 0;
    		int numU = 0;
     
    		for (int i = 0; i < str.length(); i++) {
    			char c = str.charAt(i);
    			if (c == 'A' || c == 'a') {
    				numA++;
    			}
    			if (c == 'E' || c == 'e') {
    				numE++;
    			}
    			if (c == 'I' || c == 'i') {
    				numI++;
    			}
    			if (c == 'O' || c == 'o') {
    				numO++;
    			}
    			if (c == 'U' || c == 'u') {
    				numU++;
    			}
    		}
    		System.out.print("Argument 1: ");
    		System.out.print(numA + " a, ");
    		System.out.print(numE + " e, ");
    		System.out.print(numI + " i, ");
    		System.out.print(numO + " o, ");
    		System.out.print(numU + " u");
    	}
    }

    My question is how can I make this work for any number of input arguments?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Extremely basic -- help with multiple arguments

    Could follow the approach you've already been following, but enclose all of it in a for loop. You could then do String str = args[indexOfEnclosingLoop];.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Extremely basic -- help with multiple arguments

    Do what you are currently doing for each element of the args[] array.

    Stop and think first. Describe what you will do in real life language before you attempt it in code. Your description will probably begin "For each string in the args[] array...".

    Then attempt what you have come up with in code. It depends on what steps you intend taking, but my guess is that your description of the process will involve a second for loop and some more variables.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extremely basic -- help with multiple arguments

    Quote Originally Posted by newbie View Post
    Could follow the approach you've already been following, but enclose all of it in a for loop. You could then do String str = args[indexOfEnclosingLoop];.
    I was thinking something like this, but how can the program know to use all of the arguments (be it 2 or 5000)?

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Extremely basic -- help with multiple arguments

    Quote Originally Posted by jhnhskll View Post
    I was thinking something like this, but how can the program know to use all of the arguments (be it 2 or 5000)?
    Arrays have a length property, discussed in the Arrays page of Oracle's Tutorial.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extremely basic -- help with multiple arguments

    Thanks for the help guys! Here's what I'm working with now:

    package main;
     
    public class Assignment1 {
     
    	public static void main(String args[]) {
    		int numA = 0;
    		int numE = 0;
    		int numI = 0;
    		int numO = 0;
    		int numU = 0;
     
    		for (int idx = 0; idx <= (args.length - 1); idx++) {
    			String str = args[idx];
     
    			for (int i = 0; i < str.length(); i++) {
    				char c = str.charAt(i);
    				if (c == 'A' || c == 'a') {
    					numA++;
    				}
    				if (c == 'E' || c == 'e') {
    					numE++;
    				}
    				if (c == 'I' || c == 'i') {
    					numI++;
    				}
    				if (c == 'O' || c == 'o') {
    					numO++;
    				}
    				if (c == 'U' || c == 'u') {
    					numU++;
    				}
    			}
     
    			System.out.print("Argument " + (idx + 1) + ": ");
    			System.out.print(numA + " a, ");
    			System.out.print(numE + " e, ");
    			System.out.print(numI + " i, ");
    			System.out.print(numO + " o, ");
    			System.out.println(numU + " u");
    		}
    		System.out.print("All: ");
    		System.out.print(numA + " a, ");
    		System.out.print(numE + " e, ");
    		System.out.print(numI + " i, ");
    		System.out.print(numO + " o, ");
    		System.out.println(numU + " u");
    	}
    	}
    }

    This returns
    Argument 1: 1 a, 2 e, 2 i, 1 o, 1 u
    Argument 2: 2 a, 4 e, 2 i, 3 o, 1 u
    All: 2 a, 4 e, 2 i, 3 o, 1 u
    when run with the arguments "This is a string" and "Another one"

    Now the only issue I'm having is figuring out how to count each separate argument instead of just increasing the count for all of them. I have no idea how to approach this =/

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Extremely basic -- help with multiple arguments

    Glad you solved it, and thanks for sharing your solution jhnhskll.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. #8
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extremely basic -- help with multiple arguments

    Quote Originally Posted by newbie View Post
    Glad you solved it, and thanks for sharing your solution jhnhskll.
    Well, I haven't quite gotten it yet. I need some way to count the vowels in each argument separately.

  9. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Extremely basic -- help with multiple arguments

    Apologies yes, I was away with the fairies when I read your post.
    A basic way to accomplish what you need is to use more variables, which you also print, then add to final tally, before resetting the new variables back to 0 at the end of each iteration.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #10
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extremely basic -- help with multiple arguments

    Quote Originally Posted by newbie View Post
    Apologies yes, I was away with the fairies when I read your post.
    A basic way to accomplish what you need is to use more variables, which you also print, then add to final tally, before resetting the new variables back to 0 at the end of each iteration.
    Ohhh I totally get it now. Code working as intended! Thanks for the help!

Similar Threads

  1. Replies: 6
    Last Post: December 9th, 2011, 05:53 PM
  2. [SOLVED] manipulating collections as/from parameter/arguments
    By chronoz13 in forum Collections and Generics
    Replies: 12
    Last Post: October 1st, 2011, 09:05 PM
  3. execute bat file with arguments
    By kafka82 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 6th, 2011, 01:20 PM
  4. Tiny problem with arguments.
    By Melawe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 15th, 2011, 02:26 PM
  5. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM