Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

baileyr12

Simple Programming Question

5 posts in this topic

So I just took a test in my CS1 class, and I had a complete brain fart and cannot figure out how to do this:

 

I am supposed to write a program that will take 2 words from the command line when the program is run and only print the word(s) that start with "a,b,c,d, or e". So this is what I've got:

I know I need to set 

 

String a = args[0];

String b = args[1];

 

Then I did

 

String a1 = "" + a.charAt(0);

String b1 = "" + b.charAt(0);

 

then I didn't know how to compare b1 & a1 to the first 5 letters of the alphabet unless I typed them all in one by one....

 

Any help would be great. Thanks!

 

 

 

EDIT: Ok, so I did finally get it to work, but it's pretty rough, there has to be an easier way..

public class Tester{	public static void main(String[] args)	{		String a = args[0];		String b = args[1];		String c = "" + a.charAt(0);		String d = "" + b.charAt(0);		String e = "a";		String f = "b";		String g = "c";		String h = "d";		if (c.equals(e) || c.equals(f) || c.equals(g) || c.equals(h))		{			System.out.println(a);		}				if (d.equals(e) || d.equals(f) || d.equals(g) || d.equals(h))		{			System.out.println(;		}	}} 
Edited by baileyr12

Share this post


Link to post
Share on other sites

You can convert to ASCI values and loop through them.

Edited by Liam.
1 person likes this

Share this post


Link to post
Share on other sites

Char's actually just ints on the ASCII table, so you can compare them with ints.

Table for reference: http://www.asciitable.com

 

public static Char RANGE_START = 'a'; // apublic static Char RANGE_END = 'e'; // epublic boolean charTest( Char char ) {     return ( char >= RANGE_START && char <= RANGE_END );}public static void main( String[] args ) {    for( String arg : args ) {        if( charTest( arg.charAt( 0 ) ) ) {             System.out.println( arg );        }    }}
DISCLAIMER: untested, might have off by one errors Edited by frenchie4111
2 people like this

Share this post


Link to post
Share on other sites
   //The primitive type 'char' is a unicode character. Since the characters are sequentially, you can simply do a range check.   public static void main(String[] args) {        if (args.length != 2) {            throw new IllegalArgumentException("There must be 2 arguments");        }                for (int i = 0; i < 2; i++) {            char first = args[i].charAt(0);            if (first >= 'a' && first <= 'd') {                System.out.println(args[i]);            }        }    }    //Another approach would be to iterate through a char array    public static void main(String[] args) {        if (args.length != 2) {            throw new IllegalArgumentException("There must be 2 arguments");        }        char[] chars = {'a', 'b', 'c', 'd' };        for (int i = 0; i < 2; i++) {            for (char c : chars) {                char first = args[i].charAt(0);                if (first == c) {                    System.out.println(args[i]);                }            }        }    }    //more simplistic approach    public static void main(String[] args) {        char[] chars = {'a', 'b', 'c', 'd' };        String arg1 = args[0];        String arg2 = args[1];        if (startsWith(arg1, chars)) {            System.out.println(arg1);        }        if (startsWith(arg2, chars)) {            System.out.println(arg2);        }    }    private static boolean startsWith(String s, char[] chars) {        for (char c : chars) {            if (s.charAt(0) == c) {                return true;            }        }        return false;    }

Edit: just saw @Liam. already suggested the 1st solution

edit2: and that @Red_Spark suggested the other, I should read the replies next time haha

Edited by AlphaDog

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.