Results 1 to 12 of 12
  1. #1
    rawan is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default Small Java program help

    HI,
    im new in java world
    and i shoud make this programe
    i tried to make it but i couldn't

    so,
    i hope u to help me

    this is the program



    4. write a program that reads a string composed of 4 alphabetic characters; the program forms another string comosed of alphabetic characters following those in the first string. Print the resulting string.

    Ex: input: dlpw output: emqx



    pleaaaaaaaaaase help me

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Post your code. What doesn't work? Where are you stuck?

    We certainly won't do your homework for you.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    rawan is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    ok
    i don't ask u to do it

    just explain it for me

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    loop over the String, get every single char, add 1 to each char, put chars into new String, done. Think about what should happen if the input String contains a 'z'.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    rawan is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    can i do it in another way
    not loop

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

    Default

    Quote Originally Posted by rawan View Post
    can i do it in another way
    not loop
    You could do it using recursion, but I recommend using the loop as was recommended above.

    Why can't you use a loop? Are there other conditions or requirements for this problem that we don't know as yet?

  7. #7
    rawan is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    no but i didn't study it yet
    the loop


    this is my code but it doesn't work

    Java Code:
    import java.util.*;
    
    public class Q4
    {
    
    static Scanner console = new Scanner (System.in);
    
    public static void main(String [] args)
    {
    
    String word;
    char s1,s2,s3,s4;
    
    System.out.println(" Entr 4 alphabetic: ");
    word = console.next();
    
    s1 = word.charAt(1);
    s2 = word.charAt(2);
    s3 = word.charAt(3);
    s4 = word.charAt(4);
    
    System.out.print((char)(s1+1)+(char)(s2+1)+(char)(s3+1)+(char)(s4+1));
    
    }
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-27-2010 at 07:01 PM. Reason: Moderator Edit: Code tags added

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Character positions in Java Strings (and element positions in Java arrays and other ordered collections) begin at position 0, not position 1.

    Hope that helps.

    -Gary-

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

    Default

    1) When your code doesn't work, please tell us why as it will make it 1000 times easier to help you.
    2) Please use code tags when posting code here. My signature below can help show you how.
    3) Your attempt is workable, but please note that the character position in Strings start at 0 not 1.
    4) If you want to concatenate characters and show them in a print or println statement, you may want to first have an empty pair of quotes present:

    Java Code:
    System.out.println("" + s1 + s2 + s3 + s4);
    Much luck!

  10. #10
    rawan is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaanks

    its work now

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

    Default

    Wonderful! :)

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Since you have it working now, I will show you how to do it in a loop (and maybe you will be a little bit ahead of your class :cool: ).

    Java Code:
    public class Q4a
    {
        static Scanner console = new Scanner (System.in);
    
        public static void main(String[] args) {
            String word;
            char c;
    
            System.out.print(" Enter 4 alphabetic: ");
            word = console.next();
            for (int i = 0; i < 4; i++) {
                c = word.charAt(i);
                c += 1;
                System.out.print(c);
            }
        }
    }
    Good luck!

    -Gary-

Similar Threads

  1. Proofreading this small Java program
    By almina in forum New To Java
    Replies: 5
    Last Post: 10-23-2009, 08:42 AM
  2. Small yahtzee program
    By kimmelim in forum New To Java
    Replies: 20
    Last Post: 03-12-2009, 01:11 PM
  3. Small Dice Program
    By kimmelim in forum New To Java
    Replies: 13
    Last Post: 02-15-2009, 02:01 AM
  4. Please HELP Java small program
    By afrttoh in forum New To Java
    Replies: 14
    Last Post: 11-08-2008, 03:29 AM
  5. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 09:28 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
  •