Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Hello

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Hello

    Hello,

    Im trying to find the position of all the spaces from within a value I stored in a string with a Scanner. Ive been at it two days and I dont seem to be getting anywhere with various indexOf 's Ive tried. I was thinking if i cant store a space then maybe I would split the sting at the space and search the first char after the split.

    This is what I'm at so far.

    import java.util.Scanner;
    public class Initials
    {
    public static void main(String[] args)
    {
    Scanner myKeyboard = new Scanner(System.in); // Initializing new Scanner
    System.out.println("Please enter full name: ");
    String fullName = myKeyboard.next(); // Statement to store value inputed from keyboard

    System.out.println("" + fullName);
    int lastnamestart = firstName.indexOf(' ');


    String title = "hello my name is Warren"

    If someone could point me in the right direction I can figure out the finer details

    Cheers

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,441
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Hello

    Instead of describing the step (finding the indexes of the spaces), can you describe your final goal? It seems that your classmates must have been on here a little while ago, because this problem looks pretty similar. Perhaps searching the forum would help you find their posts?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello

    I very much doubt it was any of my class mates as none of them are this far into the task sheet. What we were tasked to do was to take a full name like Alan Kane Sablo and convert it to a acronym. After many hours going through the API and "java in a nutshell" I think I have cracked it. Its only displaying the first initial but another hour or so and a cup of tea or two and I will have it up and running. Thank you for taking the time to reply though especially as the first post I up was very here and there.This was the approach I have taken anyway.

    impot.java.util.Scanner;

    public class Initials
    {
    public static void main(String[] args)
    {
    Scanner myKeyboard = new Scanner(System.in);
    System.out.println("Please enter full name(First Second Surname): ");
    String fullName = myKeyboard.next();


    String[] newWord = fullName.split(" ");

    String acronym =
    Character.toString(newWord[0].charAt(0)).toUpperCase() +
    Character.toString(newWord[1].charAt(0)).toUpperCase() +
    Character.toString(newWord[2].charAt(0)).toUpperCase();

    System.out.println("Original words were: " +
    fullName + "Three letter acronym is " + acronym);
    }
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,441
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Hello

    I have definitely seen this exact question in the past couple weeks. So even if your classmates aren't the ones asking, another teacher must be using the same problem.

    Anyway, here's a hint: What does the Scanner.next() method return? You could try printing it out, or consulting the API.

    Scanner (Java Platform SE 6)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    AlanSablo (October 7th, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello

    Alan 1 - 0

    I did it ,cheers for your help matey. As im sure you already knew i was trying to fix the wrong piece of code changed .next() to nextLine() and hey presto all three parts of the acronym had appeared.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,441
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Hello

    Quote Originally Posted by AlanSablo View Post
    Alan 1 - 0

    I did it ,cheers for your help matey. As im sure you already knew i was trying to fix the wrong piece of code changed .next() to nextLine() and hey presto all three parts of the acronym had appeared.
    No problem, glad you got it sorted.

    Another way to do it would be to continue using the next() method, which would give you one name at a time, so there'd be no need to split.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!