Okay this is my assignment question:
Write a program that accept the format last name, first name and print out first name, last name

Thats what I did:

import java.util.Scanner;

public class Name
{

	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		
		System.out.println("Enter your name");
		String name = input.nextLine();
		
		System.out.println( "\nReversed name:" );
	    String[] Name = name.split( "," );
	    
	    for ( String n: Name)
		{
			System.out.println(n);
		}



	}

}

*****************************************************

Output

Enter your name
Sam, Adams

Reversed name:
Sam
Adams

******************************************************

My question is How can I make my last name goes beside my first name not below it and also I want to keep the comma in the reversed name .

I would appreciate your help and sorry if this is a simple question

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Or try this,

System.out.println(name[0] + " " + name[1]);

** and variable names should start with a simple letter

Edited 6 Years Ago by localp: n/a

None of them worked. It keeps giving me error. Please if somebody know the answer, please help me !

The split method splits the string to depending on the regex specified then converts the string into an Array of subtrings.
Localps solution is almost correct except that the index where wrong it should be,

System.out.println( "\nReversed name:" );
String[] Name = name.split( "," );

System.out.println(Name[1] +" "+ Name[0]);

Edited 6 Years Ago by jazz_vill: Typo

thank you very much my friend. It worked with me. Thanks again :)

The split method splits the string to depending on the regex specified then converts the string into an Array of subtrings.
Localps solution is almost correct except that the index where wrong it should be,

System.out.println( "\nReversed name:" );
String[] Name = name.split( "," );

System.out.println(Name[1] +" "+ Name[0]);

This article has been dead for over six months. Start a new discussion instead.