Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By kjkrum
  • 1 Post By zFollette

Thread: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

  1. #1
    Somik Datta is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Exclamation Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Following is the code for my assignment where a user inputs his full name in a string from keyboard. It should be done using BufferedReader. The program will give his initials as output.

    ############################
    For example: User inputs-> Ram Kumar Das
    Program Returns-> R.K.D.

    I am written the following piece of code. It does not return the correct output. If "Ram Kumar Das" is given as input, it gives R.K.K. as output. Please help me with this. Arrays and objects are strictly not allowed.

    Java Code:
    import java.io.*;
    public class Assignment
    {
        static int i=0;
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        public static void main (String args[]) throws IOException
        {
            System.out.print ("Please write your full name:   ");  //asks user for input
            String fullname=br.readLine();  //takes input from the keyboard
            char initial=fullname.charAt(i);
            System.out.print (initial+".");
            int last=fullname.lastIndexOf(' ');
            while (i<=fullname.length())  // loop for R.K.D.
            {
                char pos;
                while (i<=fullname.length())
                {
                    pos=fullname.charAt(i);
                    if (pos==' ')
                    {
                        int index=fullname.indexOf(pos);
                        System.out.print (fullname.charAt(index+1)+".");
                        
                    }
                    i++;
                }
            }
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Why not keep the capital characters in the String only?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Somik Datta is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Quote Originally Posted by JosAH View Post
    Why not keep the capital characters in the String only?

    kind regards,

    Jos
    Please explain more @Jos

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    What needs further explaining of what he said? Assuming that the first letter of the names is capitalized, keep the capital letters of the input string and you have your initials.

  5. #5
    Somik Datta is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    oh! But anyway I made it.

    /**
    * @author (Somik Datta)
    * @version (4.1.14)
    * Returns the abbreviated from of a name
    */
    import java.io.*;
    public class RamKumarDas
    {
    static int i=0;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static void main (String args[]) throws IOException
    {
    System.out.print ("Please write your full name: "); //asks user for input
    String fullname=br.readLine(); //takes input from the keyboard
    char initial=fullname.charAt(i);
    System.out.print (initial+".");
    int last=fullname.lastIndexOf(' ');
    int p=0,z=0;
    for (p=0; p<fullname.length(); p++) //loop for R.K. Das
    {
    int last_space=fullname.lastIndexOf(' ');
    if (p==last_space)
    {
    System.out.print (fullname.substring(last_space));
    }
    else
    {
    if(fullname.charAt(p)==' ')
    System.out.print(fullname.charAt(p+1)+".");
    }
    }
    System.out.println ();
    System.out.print (initial+".");
    for (z=0; z<fullname.length(); z++) //loop for R.K.D.
    {
    if(fullname.charAt(z)==' ')
    System.out.print(fullname.charAt(z+1)+".");
    }
    }
    }

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Quote Originally Posted by Somik Datta View Post
    Arrays and objects are strictly not allowed.
    Wut.
    zFollette likes this.
    Get in the habit of using standard Java naming conventions!

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Its not as complicated as you made it. With loops and what not.

    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] split = br.readLine().split(" ");
        char first = split[0].charAt(0);
        char last = split[1].charAt(0);

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    I've seen far too many classes that were supposed to be "introduction to programming using Java" taught as "introduction to Java programming"... or worse, "stupid Java tricks". And it's not just India; this is how it was at Georgia Tech around 2000.
    Get in the habit of using standard Java naming conventions!

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Quote Originally Posted by kjkrum View Post
    I've seen far too many classes that were supposed to be "introduction to programming using Java" taught as "introduction to Java programming"... or worse, "stupid Java tricks". And it's not just India; this is how it was at Georgia Tech around 2000.
    Are you ranting about his or my code?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Quote Originally Posted by kjkrum View Post
    Wut.
    He wrote that objects nor arrays are allowed; strictly. jeeez, can't you read?

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Quote Originally Posted by zFollette View Post
    Are you ranting about his or my code?
    I'm ranting about the requirement that the assignment be done without using arrays (which is a silly hoop to jump through) or objects (which is impossible).
    Get in the habit of using standard Java naming conventions!

  12. #12
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Please Help ASAP. Tomorrow is the submission of my assignment. String problems

    Quote Originally Posted by kjkrum View Post
    I'm ranting about the requirement that the assignment be done without using arrays (which is a silly hoop to jump through) or objects (which is impossible).
    Yeah, whoever came up with those guide lines doesn't know Java at all. You clearly need at least one Object
    Kagiso likes this.

Similar Threads

  1. Need help ASAP! Problem due tomorrow!
    By ProtoMan in forum New To Java
    Replies: 19
    Last Post: 02-22-2011, 05:00 AM
  2. Replies: 3
    Last Post: 09-22-2009, 05:16 AM
  3. problems with java code! (very new - need help asap!)
    By sumkindafreek in forum New To Java
    Replies: 1
    Last Post: 01-07-2009, 05:00 AM
  4. array problems need your help ASAP!
    By notherand in forum New To Java
    Replies: 1
    Last Post: 06-29-2008, 08:59 PM

Tags for this Thread

Posting Permissions

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