5

I'm new to java. I have started about 3 days ago. I want to make a line of random characters and to put them into a string. Thank you.

    import java.util.Random;
      public class test{
      public static void main (String[]args){

final String alphabet = "abcdefghigklmnopqrstuvwxyz";
final int N = alphabet.length();
Random r = new Random();

for (int i = 0; i < 50; i++) {
    String s = alphabet.charAt(r.nextInt(N));
    // System.out.println(alphabet.charAt(r.nextInt(N)));
}}}
Aras
  • 89
  • 1
  • 1
  • 8
  • Compilation completed. The following files were not compiled: 1 error found: File: C:\Users\Arnas\Desktop\Java\hack.java [line: 10] Error: incompatible types required: java.lang.String found: char – Aras Aug 22 '13 at 11:51
  • 3
    First rule of programming: read the error message. Second rule of [Java] programming: read the [Java]doc. What does the documentation say about String.charAt()? What does it return? – JB Nizet Aug 22 '13 at 11:51
  • 1
    A question that was asked multiple times in stackoverflow http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string – AurA Aug 22 '13 at 11:51
  • `charAt` method returns a `char` not `String` – ogzd Aug 22 '13 at 11:52
  • I guess you want to use something like a StringBuilder which you append in your for loop. After the loop you can call toString on that builder and you have your String built. What you are doing is to create a new String with exactly one character in every run through your loop. – Matthias Aug 22 '13 at 11:52
  • Another similar question http://stackoverflow.com/questions/2863852/how-to-generate-a-random-string-in-java – AurA Aug 22 '13 at 11:52

6 Answers6

7

Easiest way would be to use a StringBuilder or StringBuffer (syntax is the same).

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50; i++) {
    sb.append(alphabet.charAt(r.nextInt(N)));
}
String s = sb.toString();
hermansen
  • 158
  • 3
1
String s = "";
for (int i = 0; i < 50; i++) {
    s += alphabet.charAt(r.nextInt(N));
}
drewdles
  • 244
  • 1
  • 10
1

Append your random characters to StringBuilder that acts as a text buffer.

StringBuilder sb = new StringBuilder();

for (int i = 0; i < 50; i++) {
    sb.append(alphabet.charAt(r.nextInt(N)));
}

System.out.println(sb);
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
1

Use StringBuilder

StringBuilder sb = new StringBuilder()
for (int i = 0; i < 50; i++) {
    sb.append(alphabet.charAt(r.nextInt(N)))
}
String s = sb.toString()
Mikkel Løkke
  • 3,710
  • 23
  • 37
1

The simplest way would be to just add the char with the addition-operator ("+") to the string. Its function is overloaded, so that should work.

As you get more comfortable with Java, you should start using StringBuilder or StringBuffer.

1

Use a StringBuilder for adding characters to build a string -

import java.util.Random;

public class test{

    public static void main (String[]args){
        final String alphabet = "abcdefghigklmnopqrstuvwxyz";
        final int N = alphabet.length();
        Random r = new Random();
        StringBuilder builder = new StringBuilder();

        // make strings with 50 characters
        for (int i = 0; i < 50; i++) {
            builder.append(alphabet.charAt(r.nextInt(N)));
        }

        System.out.println(builder.toString();
    }
}
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178