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

use

System.out.print("to print");

instead of

System.out.println("to print");

Or try this,

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

** and variable names should start with a simple letter

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]);

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]);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.