Results 1 to 18 of 18
  1. #1
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default i need help with my excercise about strings please :(

    Hellow there i need help from anyone who have time with this excercise
    *am using jcreator pro

    demanded points
    i should write a program to enter a String of single or multiple words in length. (Their entry may contain spaces, digits, and/or punctuation characters.)
    The program will then work out the modified version of the user's String as follows.
    Starting with an empty output String the input String is examined one character at a time, keeping track of the previous characters value:
    If the character is a number or punctuation character then
    it is copied to the output string
    Otherwise
    If the character matches the previous character then it is dropped from the output string
    Otherwise
    If the character is a vowel (a, e, i, o, u) and not a single character word then
    The letter is dropped from the output string
    Otherwise the letter is copied to the output string
    The output String is then displayed on the screen
    Statistics about the String are then displayed. These are the number of characters that were dropped from the String, and the average number of characters dropped per word within the String.
    The User is then asked if they would like to shorten another String accepting Y or y (for yes) continue, and quitting on all other input.
    The User will then have displayed for them a count of how many Strings they abbreviated.

    Sample run of the program (user input is bold)
    Please enter a Sentence
    The Quick Brown Fox
    Abbreviated String: Th Qck Brwn Fx
    Sentence Stats
    Number of Words: 4
    Average number of letters dropped per word: 1.25
    Do you want to try another? (y/n)
    Y
    Please enter a Sentence
    I like bananas
    Abbreviated String: I lk bns
    Sentence Stats
    Number of Words: 3
    Average number of letters dropped per word: 3.66
    Do you want to try another? (y/n)
    y
    Please enter a Sentence
    Laughing should never be banned
    Abbreviated String: Lghng shld nvr b bnd
    Sentence Stats
    Number of Words: 5
    Average number of letters dropped per word: 2.2
    Do you want to try another? (y/n)
    n


    and i have to use this methods: substring, string, replace, concat
    loops: for, while, if, else


    actually i've started and reached untill here and i got stocked :(


    Java Code:
    import java.util.Scanner;
    
    public class assignment1
    {
    	public static void main (String[] args) 
    	{
    		Scanner sc;
    		sc=new Scanner(System.in);
    		String ab, cd, ef;
    		String output;
    		int length;
    		char temp_char;
    		char temp_char2;
    		
    		
    		System.out.println("enter the sentence you want to remove double letters from");
    		
    		ab=sc.nextLine();
    		length=ab.length();
    		
    		
    		System.out.println("length is "+length);
    		
    		
    		
    		for (int i=0; i<length; i++)
    		{
    			temp_char=ab.charAt(i);
    			temp_char2=ab.charAt(i+1);
    			
    			if
    			(temp_char==temp_char2)
    			{

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Comments on code so far:
    Use meaningful variable names, not ab, cd etc

    Add some comments describing what each section of code is supposed to do.
    For example what is purpose of this code:
    Java Code:
    if
    			(temp_char==temp_char2)
    			{

  3. #3
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default "

    thank you for your reply

    ok lets take it step by step .. first i want to enter the input " any sentence " and i want the output without double letters

    example

    input: am running in the street
    output: am runing in the stret

    Java Code:
    import java.util.Scanner;
    
    public class assignment1
    {
    	public static void main (String[] args) 
    	{
    		Scanner sc;
    		sc=new Scanner(System.in);
    		String sent1, sent2, sent3;
    		String output;
    		int length;
    		char temp_char;
    		char temp_char2;
    		
    		
    		System.out.println("enter the sentence you want to remove double letters from");
    		
    		sent1=sc.nextLine();
    		length=sent1.length();
    		
    		
    		System.out.println("length is "+length);
    		
    		
    		
    		for (int i=0; i<length-1; i++)
    		{
    			temp_char=ab.charAt(i);
    			temp_char2=ab.charAt(i+1);
    			
    			if
    			(temp_char==temp_char2) // here i want to remove the double letters in each word fot my output sentence (sent1)
    			{
    Last edited by zohdi; 05-18-2010 at 01:12 AM.

  4. #4
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    read the first post again please and it may help you

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    1) The for loop should not go to i < length but i < length - 1; Why? Because if you go to < length, you'll try to compare the last letter with a non-existant letter beyond the length of a String.

    2) Consider creating an empty result String before your for loop (or better a StringBuilder object) and then inside of the for loop copy a letter over from the original String to the new result String if there is no duplicate.

    3) Then when done, show the result String.

    buena suerte!

  6. #6
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    1) you're right .. thats my fualt i corrected it
    2) i dont know how to do it :( thats why i created this thread
    3) -

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by zohdi View Post
    2) i dont know how to do it :( thats why i created this thread
    Give it a try, for in trying is how you learn.

  8. #8
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    thanks for your interest . i'll try

  9. #9
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    help :( please

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Where are you now? I don't see any new code.

    Take the "rules" for editing the input and creating the output and work on ONE at a time.

  11. #11
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default "

    am still here sir

    Java Code:
     
    Code: 
    import java.util.Scanner;
    
    public class assignment1
    {
    	public static void main (String[] args) 
    	{
    		Scanner sc;
    		sc=new Scanner(System.in);
    		String sent1, sent2, sent3;
    		String output;
    		int length;
    		char temp_char;
    		char temp_char2;
    		
    		
    		System.out.println("enter the sentence you want to remove double letters from");
    		
    		sent1=sc.nextLine();
    		length=sent1.length();
    		
    		
    		System.out.println("length is "+length);
    		
    		
    		
    		for (int i=0; i<length-1; i++)
    		{
    			temp_char=ab.charAt(i);
    			temp_char2=ab.charAt(i+1);
    			
    			if
    			(temp_char==temp_char2) // here i want to remove the double letters in each word fot my output sentence (sent1)
    			{
    for now . i just want to remove double letters and vowels from output
    and i'll figure the rest

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What you just posted for code looks the same as what you posted before???

  13. #13
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    i know.. thats why i said am still here..
    i tried using cocat and substring .. but i dont know how to implement it inside loops because am a beginner
    i just want any one to help me untill i have the output with removed double letters and vowels

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Do you have an algorithm to do that? IE what are the steps you must take to remove double letters? You'll need an output String to copy the single letters to.

  15. #15
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I outlined a brief algorithm above. Can you expand on it?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Here's one to copy only one of line of repeated letters to output:
    Copy the first letter to the output
    look at the next letter
    If not same as previous letter, copy it to output. (If same skip it)
    Repeat at step 2 above.

  17. #17
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    hi i have the exact same exercise to do Zohdi!!! and am getting nowhere. please could you post you code for me so i can get some ideas. i would really appreciate it.
    this is what i have so far, spent hours and hours on it and dont know what to do.
    please anybody help. i would be very greatful. thanks

    /*
    *AssigOne210
    */


    import java.util.Scanner;


    public class AssigOne210
    {
    public static void main (String[] args)
    {

    Scanner scan = new Scanner (System.in);

    String input;
    String output;
    char current;


    System.out.println ("Enter a sentence:");
    input = scan.nextLine();
    current = input.charAt(0);
    if (current == 'a')
    output = output + current;
    // line above is creating error if (output is a string rather than a char)
    System.out.println ("+output");






    }
    }

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM
  2. Strings as array help please..
    By crazygurl in forum New To Java
    Replies: 1
    Last Post: 12-01-2009, 08:23 PM
  3. need in help in comparing Strings
    By jaq in forum New To Java
    Replies: 1
    Last Post: 11-25-2009, 01:06 PM
  4. Subtracting Strings
    By lazygun247 in forum New To Java
    Replies: 6
    Last Post: 03-29-2009, 10:05 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
  •