0

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 by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by salamjamal10
0

Or try this,

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

** and variable names should start with a simple letter

Edited by localp: n/a

0

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

0

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 by jazz_vill: Typo

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

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.