Created
June 6, 2018 08:26
-
-
Save Venipa/521886b9229ed1ba7ba04460929a3e0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stringUebung; | |
import java.util.Random; | |
import java.util.Scanner; | |
public class Sbuilder { | |
public static void main(String[] args) { | |
new Sbuilder().start(); | |
} | |
public void start() { | |
Scanner sin = new Scanner(System.in); | |
int input = 0; String fuellzeichen = null; | |
boolean loop = true; | |
while(loop) { | |
System.out.print("Geben Sie die Anzahl Zeichen des neuen Strings ein: "); | |
int p = 0; | |
try { | |
p = sin.nextInt(); | |
} finally { | |
input = p; | |
sin.nextLine(); | |
} | |
System.out.print("Füllzeichen (erster Character wird benutzt): "); | |
if((fuellzeichen = sin.nextLine()).isEmpty()) { | |
System.out.println(this.getRandString(p)); | |
} else { | |
System.out.println(this.getIteratedString(p, fuellzeichen)); | |
} | |
} | |
} | |
private String getRandString(int length) { | |
String _availchars = "abcdefghijklmnopqrstuvwxyz"; | |
char[] _chars = (_availchars.toUpperCase() + _availchars.toLowerCase()).toCharArray(); | |
StringBuilder sb = new StringBuilder(); | |
for(int i : new int[length]) { | |
sb.append(Character.toString(_chars[new Random().nextInt(_chars.length - 1)])); | |
} | |
return sb.toString(); | |
} | |
private String getIteratedString(int p, String f) { | |
StringBuilder sb = new StringBuilder(); | |
for(int i : new int[p]) { | |
sb.append(f.toCharArray()[0]); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment