| | |
Character array convertion
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 20
Reputation:
Solved Threads: 0
[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)
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 ); JOptionPane.showMessageDialog ( null, "Your new string is: " + firstName.charAt(0) + s1 + number ); } }
1
#2 Oct 6th, 2009
I believe that your problem is this:
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)
String s1 = new String( secondLength ); .... lastName.getChars ( 0, 5, secondLength, 0 );
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)
String finalResult = firstName.charAt(0) + s1 + number; System.out.println("Final Result :"+finalResult);
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- cin of multi-dimension character array (C++)
- Character array (C++)
- Converting from a string to a character array. (C++)
- How do you define character array constant (C++)
- Sorting a string or character array (C++)
- Character Array to character pointer. (C)
- Help with folding a character array please (C)
Other Threads in the Java Forum
- Previous Thread: How to extract RPM file using Java program
- Next Thread: PrintWriter
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






