944,094 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 832
  • Java RSS
Oct 5th, 2009
0

Character array convertion

Expand Post »
[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.

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Teethous is offline Offline
32 posts
since Jan 2009
Oct 6th, 2009
1
Re: Character array convertion
I believe that your problem is this:

Java Syntax (Toggle Plain Text)
  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 :
Java Syntax (Toggle Plain Text)
  1. String finalResult = firstName.charAt(0) + s1 + number;
  2. System.out.println("Final Result :"+finalResult);
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 6th, 2009
0
Re: Character array convertion
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Teethous is offline Offline
32 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to extract RPM file using Java program
Next Thread in Java Forum Timeline: PrintWriter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC