Hey everyone, I am having a problem with converting my character array. The program runs, but in the display box I get little squares instead of a string. The program should display the first char of the fist name, the first five of the second name and a random number between 10-99. Thanks much for your help.

import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Check {

	 * @param args
	public static void main(String[] args) 
		Scanner input = new Scanner ( System.in); // creates an object of scanner
		Random random = new Random (); // creates an object of random

		char secondLength[]= new char [5]; // array for five characters
		int number = 0; // variable for number generated
		String s1 = new String( secondLength );
		//random number generator ranging from 10-99
		number = 10 + random.nextInt ( 90 );
		// gather data from user
		String firstName = JOptionPane.showInputDialog( "Enter first name" );
		String lastName = JOptionPane.showInputDialog( "Enter last name" );
		lastName.getChars ( 0, 5, secondLength, 0 );
		System.out.print( number );
		         ( null, "Your new string is: " + firstName.charAt(0) + s1 + number );	

7 Years
Discussion Span
Last Post by Teethous

I believe that your problem is this:

String s1 = new String( secondLength );
lastName.getChars ( 0, 5, secondLength, 0 );

When you create the "s1" the "secondLength" array is empty. Then you call the method that gives to the array values, but the s1 was already created.
Try first to enter values to the array and then create the String "s1".

Also try to print the result at a System.out.println :

String finalResult = firstName.charAt(0) + s1 + number;
System.out.println("Final Result :"+finalResult);
Votes + Comments
Very concise approach

Thank you very much javaAddict for you input. It has helped me a whole bundle. I do hope to get on top of this programming thing soon.

This question has already been answered. 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.