Results 1 to 6 of 6
  1. #1
    JavaStudent64 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default My Java HW Help Thread

    In order to not overwhelm the message boards, I hope this is OK. I have created one thread where I can post questions I have about my Java HW.

    So first of all I am new here! Thank you for having a forum like this. Very helpful. A little about me: Undergrad student, interested in pursuing Computer Science, and currently taking a Java class with sometimes very difficult homework. I am not simply looking for answers here, but help UNDERSTANDING what I am doing.

    So here is a question on my recent HW:

    Write your program in the file PigLatin.java.
    "Pig Latin" is a fake language used as a children's game. A word in English is "translated" into Pig Latin using the following rules:

    If the English word begins with a consonant, move the consonant to the end of the word and add "ay". The letter Y should be considered a consonant.
    If the English word begins with a vowel (A, E, I, O, or U), simply add "way" to the end of the word.
    (This is a simplified dialect of Pig Latin, of course.)

    Ask the user for a word (one string) and output its Pig Latin translation (one string). Do not make any assumptions about the case (uppercase/lowercase) of the input. However, it does not matter whether your output is in uppercase or lowercase.

    Construct a set of test cases for your program, and enter these in assign4-testcases.txt.

    Example:
    java PigLatin
    clam
    RESULT: "lamcay"

    This is the code I have written so far:
    Java Code:
    public class PigLatin
    {
        public static void main(String[] args)
        {
        	    
        	    System.out.println("Please enter a phrase that you want me to translate into Pig Latin: ");
        	    String prepig = IO.readString();
        	    String prepig1 = prepig.substring(0,1);
        	    System.out.println(prepig1);
        	    boolean tester = (prepig1 == "a" || prepig1 == "e" || prepig1 == "i" || prepig1 == "o" || prepig1 == "u" || prepig1 == "A" || prepig1 == "E" || prepig1 == "I" || prepig1 == "O" || prepig1 == "U");
        	    System.out.println(tester);
        	    if (tester)
        	    {
        	    	   String finalstr = prepig + "way";
        	    	   System.out.println(finalstr);
        	    }
        	    else
        	    {
        	    	    String first = prepig.substring(0,1);
        	    	    String slice = prepig.substring(1,prepig.length());
        	    	    System.out.println(slice + first + "ay");
        	    }
    
        	    
        }
    }
    My problem is that for test cases where I am using a word that begins with a vowel, I am not getting the correct output. For example:
    If I input the string "Octagon" my output is "ctagonoay"

    What am I doing wrong?
    Thanks for any help!

    -JS64

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Hmm, first of all, this line:
    Java Code:
    String prepig1 = prepig.substring(0,1);
    Wouldn't it be easier to do:
    Java Code:
    char prepig1 = Character.toLowerCase(prepig.charAt(0));
    Then you do the testing like this:
    Java Code:
    if(prepig1 == 'a' || prepig == 'e' ... //for all consonants)
    The problem you had, was the you weren't checking for uppercase consonants. The way I've written it, you take the first character from the prepig string, and convert it to lowercase, so you don't need to worry about cases not matching.

  3. #3
    JavaStudent64 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the feedback. I think I was checking for all the vowels both lower and uppercase, if you scroll right there are the uppercase vowels listed. I think for some reason my "if" is not being run...

    I have some time to work on it again now, so I am trying to figure out what I am doing wrong. More feedback would always be great.
    Thanks,
    -JS64

  4. #4
    JavaStudent64 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    I figured it out, thanks for the help. I took your advice and whatever I did seemed to work.

    Now I'm having another problem:

    This next question asks:
    Write your program in the file Compress.java.
    Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences.

    Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of:

    the number of characters in the sequence
    the repeating character
    If a character does not repeat, it should be left alone.
    For example, consider the following string:

    qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
    After applying the RLE algorithm, this string is converted into:

    q9w5e2rt5y4qw2Er3T
    In the compressed string, "9w" represents a sequence of 9 consecutive lowercase "w" characters. "5e" represents 5 consecutive lowercase "e" characters, etc.

    Write a program that takes a string as input, compresses it using RLE, and outputs the compressed string. You may assume that there are no digit characters in the input string. Case matters - uppercase and lowercase characters should be considered distinct.

    Construct a set of test cases for your program, and enter these in assign4-testcases.txt.

    Example:
    java Compress
    aaaaaaabbbabbbbbccccc
    RESULT: "7a3ba5b5c"

    And I have written the following code:

    XML Code:
    public class Compress
    {
    	public static void main(String[] args)
    	{
    		System.out.println("Please enter a string: ");
    		String users = IO.readString();
    		int count = 0;
    		for (int i=0 ; i <= users.length() ; i++)
    		{
    			while (users.charAt(i) == users.charAt(i + 1))
    			{
    				count = count + 1;
    			}
    			System.out.println(count + users.charAt(i));	
    		}			
    	}
    }
    Basically I want to nest loops in such a fashion so that the commputer takes the input, compares each character to the next, and when it notices a change, ads the previous characters number to the list of things to be printed in a new string, and then prints the letter of that sequence...

    But when I ran this code with the following input:
    Hellllllooooooo
    I got back:
    72
    101

    ???
    Thanks for any help,
    -JS64

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    count + users.charAt(i), this line produces an int, since a char is nothing more than an int, that denotes the characters position in the ASCII table (could be wrong about the significance). Try System.out.print(count+""+users.charAt(i));. Note the print, not println, as you want all the output in one line.

  6. #6
    JavaStudent64 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    OK so no for this code:

    Java Code:
    public class Compress
    {
    	public static void main(String[] args)
    	{
    		System.out.println("Please enter a string: ");
    		String users = IO.readString();
    		String userssp = (users + " ");
    		int count = 0;
    		for (count = 0 ; count < userssp.length() ; count++)
    		{
    			while (userssp.charAt(count) != userssp.charAt(count + 1))
    			{
    				System.out.println(count+1);
    				String newstr = userssp.substring(0,count +1);
    				//System.out.println(newstr);
    				int digits = newstr.length();
    				System.out.println(digits);
    				newstr.substring(count, (count + 1));
    				System.out.println(newstr.charAt(0));
    				userssp = userssp.substring(count + 1, userssp.length());
    				//System.out.println(userssp);
    				
    			}
    			
    		}			
    	}
    }
    Results for the following test cases:
    INPUT: aaaabbbbcccc
    OUPUTS:
    4
    4
    a
    4
    4
    b
    4
    4
    c
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(Unknown Source)
    at Compress.main(Compress.java:11)

    INPUT: aaaabbbccd
    OUTPUTS:
    4
    4
    a
    5
    5
    b
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    at java.lang.String.charAt(Unknown Source)
    at Compress.main(Compress.java:11)

    What am I doing wrong?

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 02:13 AM
  3. Replies: 3
    Last Post: 11-06-2008, 04:24 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •