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

Dani AI

Generated

The reason the output was on two lines is that the split results were printed inside a loop with println, so each part became its own line. Also splitting on a plain comma left a leading space on the second element (so you saw " Adams"). A robust solution is to build one output string (first, comma, last) and print it once, and to trim whitespace or use a split that ignores surrounding spaces.

Example approach using the first-comma position (safer when names might contain extra commas later):

int commaPos = input.indexOf(',');
if (commaPos >= 0) {
    String lastName = input.substring(0, commaPos).trim();
    String firstName = input.substring(commaPos + 1).trim();
    String result = firstName + ", " + lastName;
    System.out.println(result);
} else {
    System.out.println("Input not in 'last, first' format");
}

Alternative: use a regex split that swallows surrounding whitespace, then validate the array length before reordering:

  • String[] parts = input.split("\\s*,\\s*");
  • check parts.length >= 2
  • print parts[1] + ", " + parts[0]

Notes and gotchas:

  • Trim each part to remove leftover spaces. Calling trim() prevents leading spaces like " Adams".
  • Validate input to avoid ArrayIndexOutOfBoundsException when there is no comma.
  • If a name can legally include commas (rare), prefer the indexOf/substring approach that only splits at the first comma.
  • Follow Java naming conventions: use lowerCamelCase for variables (avoid Name).

was right that printing without an extra newline can help, but assembling the whole string first is cleaner. pointed at indexing — watch the order — and corrected the index; just add trimming and a comma to match the required output.

Recommended Answers

All 5 Replies

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.