Character array convertion

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 20
Reputation: Teethous is an unknown quantity at this point 
Solved Threads: 0
Teethous Teethous is offline Offline
Newbie Poster

Character array convertion

 
0
  #1
Oct 5th, 2009
[TEX]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.

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. import javax.swing.JOptionPane;
  4.  
  5.  
  6. public class Check {
  7.  
  8. /**
  9. * @param args
  10. */
  11. public static void main(String[] args)
  12. {
  13. Scanner input = new Scanner ( System.in); // creates an object of scanner
  14. Random random = new Random (); // creates an object of random
  15.  
  16.  
  17.  
  18. char secondLength[]= new char [5]; // array for five characters
  19. int number = 0; // variable for number generated
  20. String s1 = new String( secondLength );
  21.  
  22. //random number generator ranging from 10-99
  23. number = 10 + random.nextInt ( 90 );
  24.  
  25. // gather data from user
  26. String firstName = JOptionPane.showInputDialog( "Enter first name" );
  27. String lastName = JOptionPane.showInputDialog( "Enter last name" );
  28.  
  29. lastName.getChars ( 0, 5, secondLength, 0 );
  30.  
  31. System.out.print( number );
  32. JOptionPane.showMessageDialog
  33. ( null, "Your new string is: " + firstName.charAt(0) + s1 + number );
  34.  
  35.  
  36. }
  37.  
  38. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,711
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso
 
1
  #2
Oct 6th, 2009
I believe that your problem is this:

  1. String s1 = new String( secondLength );
  2. ....
  3. 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 :
  1. String finalResult = firstName.charAt(0) + s1 + number;
  2. System.out.println("Final Result :"+finalResult);
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 20
Reputation: Teethous is an unknown quantity at this point 
Solved Threads: 0
Teethous Teethous is offline Offline
Newbie Poster
 
0
  #3
Oct 6th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC