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)));
}}}
6
  • 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 Commented Aug 22, 2013 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? Commented Aug 22, 2013 at 11:51
  • 1
    A question that was asked multiple times in stackoverflow stackoverflow.com/questions/41107/… Commented Aug 22, 2013 at 11:51
  • charAt method returns a char not String Commented Aug 22, 2013 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. Commented Aug 22, 2013 at 11:52

6 Answers 6

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();
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for using StringBuilder
upvote for StringBuilder
Thank you. It worked but I had to add if statement that if sb length is over 30 sb would be reseted and then I used Mikkel Løkke method.
I'm not familliar with StringBuilder yet. Thank you anyway.
1
String s = "";
for (int i = 0; i < 50; i++) {
    s += alphabet.charAt(r.nextInt(N));
}

Comments

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);

Comments

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()

Comments

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.

Comments

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();
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.