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 5 of 5

Thread: Java reverse order using pointers

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

    Default Java reverse order using pointers

    This is what I have to do:
    Write a program that takes a string of someone's name and prints it out last name first. Your program must use pointers, not array subscripts. You may use the available string manipulation functions if you find an opportunity.
    Example:
    "George Washington"
    "Washington, George"

    I am not sure how to reverse the name, I have been looking in my textbook and online and cannot figure it out. This is what I have put together so far, it does not run. At the end it says unnecessary return value. I have a feeling if my code did run it would be completely wrong. I really appreciate any help given.

    import java.util.*;
    import java.util.Scanner;
    public class Test {
        public static void main ( String [] args )
    {
        Scanner sc = new Scanner(System.in);
     
        System.out.print("Enter name: ");
        String name = sc.nextLine();
     
        String lastname = "";
    	    String firstname = "";
    	    for(int i = 0; i < name.length(); i++)
    	    {
    	        if(name.charAt(i) == ' ')
    Last edited by javanewbi; October 9th, 2014 at 04:22 PM. Reason: Edited


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Java reverse order using pointers

    You're in the main method, which has a void return type. Just replace the return by a System.out.println(....).

  3. The Following User Says Thank You to PhHein For This Useful Post:

    javanewbi (October 9th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Java reverse order using pointers

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

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

    javanewbi (October 9th, 2014)

  6. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java reverse order using pointers

    Thank you, so anytime I have a void return type I can convert it to this? What is I wanted to change it from void? Lastly, I am trying to find out more about pointers. In this the pointers is the last name first name?

  7. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java reverse order using pointers

    There are no pointers in java.
    What you have in java are variables.

    In your case there are 4 variables in your program:
    The variable "sc" of type Scanner, the variable "name" of type String and the variables "firstname" and "lastname" of type String.

    You are supposed to extract 2 substrings from the variable "name" and store these in the variables "firstname" and "lastname". To get these substrings I would recommend reading the String API (google for "java doc String") and choose the methods you want to use.


    If you need any further information about the basics of java perhaps you should read a few online tutorials or books.

  8. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    GregBrannon (October 9th, 2014), javanewbi (October 9th, 2014)

Similar Threads

  1. fibonacci series in reverse order
    By r2dak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 17th, 2013, 10:06 AM
  2. How do I process an Arrays.toString in reverse order????
    By jessieaugust in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 5th, 2013, 03:21 AM
  3. Java pointers
    By bassie in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 29th, 2012, 12:41 PM
  4. Reverse the order of Strings in an ArrayList?
    By jean28 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 8th, 2012, 11:25 PM
  5. Replies: 1
    Last Post: August 5th, 2012, 09:28 PM

Tags for this Thread