Making a word Scrambler with Math.random - java

import java.util.Scanner;
public class WordScrambler {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int chosenWord;
String[] words = {"hogwash","Rudolph","yule-log","Eggnog","CandyCane","Christmas","Fruitcake","gingerbread","Krampus","nutcracker"};
System.out.println("Pick a number between 1-10");
chosenWord=in.nextInt();
String a=words[chosenWord].substring(0,words[chosenWord].length()/2);
String b=words[chosenWord].substring(words[chosenWord].length()/2);
String c=b+a;
int x=(int)(Math.random()*(words[chosenWord].length()-1))+1;
String d=c.substring(0, words[chosenWord].length()-x);
String e=c.substring(words[chosenWord].length()-x);
String f=e+d;
System.out.println(f);
}
}
This is what i have so far.
I cant find a way to Scramble it anymore.
Right now the output is for example: Hogwash :shhogwa.
Thats the only word that Scrambles.

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int chosenIndex;
String[] words = { "hogwash", "Rudolph", "yule-log", "Eggnog", "CandyCane", "Christmas", "Fruitcake",
"gingerbread", "Krampus", "nutcracker" };
System.out.println("Pick a number between 1-10");
chosenIndex = in.nextInt();
String chosenWord = words[chosenIndex - 1];
System.out.println(chosenWord);
StringBuilder scrambled = new StringBuilder();
List<Character> letters = new ArrayList<Character>();
for (char x : chosenWord.toCharArray()) {
letters.add(x);
}
while (letters.size() != 0) {
int random = (int) (Math.random() * letters.size());
scrambled.append(letters.remove(random));
}
System.out.println(scrambled.toString());
}
use meaningful variable names so its clear what its referring to

Related

Print First N characters in reverse

import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int N = sc.nextInt();
StringBuilder sb = new StringBuilder(str);
System.out.print(sb.reverse().toString());
}
}
the output should be:-
apple
3
ppa
but I'm getting full string in reverse
elppa
like this
You aren't splitting the string at all in your code. What you need is a .substring(). Since you can't reverse a word after performing the .substring() on a StringBuilder, we'll just create our own reverse method. Try this:
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
String str = sc.nextLine();
int n = Integer.parseInt(sc.nextLine());
System.out.print(reverse(str.substring(0, n)));
sc.close();
}
public static String reverse(String text) {
return new StringBuilder(text).reverse().toString();
}
}
Using strictly StringBuilder methods (as you requested), you can do:
import java.util.Scanner;
public class SplitReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int n = Integer.parseInt(sc.nextLine());
StringBuilder sb = new StringBuilder(str);
sb=new StringBuilder(sb.substring(0,n));
System.out.println(sb.reverse());
sc.close();
}
}
However, I would recommend this one:
import java.util.Scanner;
public class SplitReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int n = Integer.parseInt(sc.nextLine());
StringBuilder sb = new StringBuilder(str.substring(0,n));
System.out.println(sb.reverse());
sc.close();
}
}
Try this code. I solved the same problem, I got it right.
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int N = sc.nextInt();
StringBuilder sb = new StringBuilder(str.substring(0,N));
System.out.print(sb.reverse().toString());
}
}

Word Scramble Not Scrambling Words

This program is supposed to shuffle the letters in the strings input, how ever it is just repeating the input. I am not sure what I am doing wrong, the issue seems to be in the scrambler function. Thank you
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random r = new Random();
while (in.hasNext()) {
String str = in.next();
System.out.println(str);
}
}
//shuffle letters in word besides first and last
public static String scrambler(String str, Random r) {
//basic char array for input strings
char[] a = str.toCharArray();
//scramble letters
for (int i = 0; i < a.length; i++) {
//shuffle letters in word besides first and last
int j = r.nextInt(a.length);
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return new String(a);
}
You're not calling Scrambler method, you should write
public static void main( String[] args){
Scanner in = new Scanner(System.in);
Random r = new Random();
while(in.hasNext()){
String str = in.next();
System.out.println(scrambler(str,r));
}

Why is my program not running? Please check

The following program prompts the user to enter a word; reverse the word and print it. I believe my program contains no error but it does not seem to run in eclipse. Please somebody check my code. Thanks!
import java.util.Scanner;
public class reverseWord
{
public static String reverse(String m)
{
String reverse = "";
int Length = m.length();
for(int i = Length-1; i>=0; i=i-1)
{
reverse = reverse + m.charAt(i);
}
return reverse;
}
public static void main(String args[])
{
Scanner keyIn = new Scanner(System.in);
String store = keyIn.nextLine();
reverseWord rw = new reverseWord();
rw.reverse(store);
}
}
Your problem is that you are only returning the reversed String but no printing it out to the console. You should also changed the method to be a non-static method or accessing it using the class name instead of an instance. And class names in java always start with a uppercase letter :) Try something like this:
import java.util.Scanner;
public class ReverseWord {
public String reverse(String m) {
String reverse = "";
int Length = m.length();
for (int i = Length - 1; i >= 0; i = i - 1) {
reverse = reverse + m.charAt(i);
}
return reverse;
}
public static void main(String args[]) {
Scanner keyIn = new Scanner(System.in);
String store = keyIn.nextLine();
ReverseWord rw = new ReverseWord();
String reversedWord = rw.reverse(store);
System.out.println(reversedWord);
}
}

Java - string split error

I'm trying to split a string and return each sub-string to an array in Java (easier in c#) but the compiler is not having it. I keep getting an index out of bounds error when I try to call the value of any string in the array indexed higher than 0. Here's the code I'm using:
public class hello {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int setter = 3000;
String num = in.next();
String[] numbers = num.split(" ");
int j = numbers.length;
for (int i =0; i < numbers.length ; i++) {
System.out.println(numbers[i]);
}
System.out.println(j);
Even the length of the array being returned is 1.
As David Wallace said in comments: you should use nextLine from Scanner...
But... why read line to split into int?
public static void main(final String[] args) {
final Scanner in = new Scanner(System.in);
String line = null;
List<List<Integer>> all = new ArrayList<>();
while ((line = in.nextLine()) != null) {
final String[] tokens = line.split(" ");
List<Integer> forOneLine = new ArrayList<>();
for (final String token : tokens) {
try {
final Integer value = Integer.valueOf(token);
forOneLine.add(value);
} catch (final NumberFormatException e) {
// Not an Integer
}
}
all.add(forOneLine);
}
Is it ok now?
Ended up parsing to an integer
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String n = in.nextLine();
int ne = Integer.parseInt(n);
String m = in.nextLine();
String[] numbers = m.split(" ");
System.out.println(n);
System.out.println(m);
for (String string : numbers) {
System.out.println(string);
}
}

java beginner programming, putting strings in char array to filter input

in this code I've been trying to filter the characters in the array with a for-loop to a second array, but am unable to. Could anyone tell me what is exactly wrong with my code?
public class Deel1 {
public static void main(String[] args) {
String zinInvoer = getInput();
String zinUitvoer = filterZin(zinInvoer);
}
static String getInput() {
Scanner scan = new Scanner(System.in);
String zinInvoer = "";
System.out.println("Voer een zin in: ");
if (scan.hasNextLine()) {
zinInvoer = scan.nextLine().trim();
}
if (zinInvoer.equals("")) {
System.out.println("Geen invoer!");
System.exit(0);
}
return zinInvoer;
}
static String filterZin(String zinInvoer) {
String zinUitvoer = "";
char ongefilterd[] = zinInvoer.toCharArray();
String nogFilteren = new String(ongefilterd);
char a = nogFilteren.charAt(97);
for (a = 97; a <= 122; a++) {
a = a += 32;
char gefilterd[] = //second array to be printed
}
System.out.println("Gefilterd: " + zinUitvoer);
return zinInvoer;
}
}
Sorry if it annoys you but I had to translate your variables into english in order to figure out what their purposes were.
First of all, it will always throw an exception when the string is less than 98 letters long because it looks for the 97th letter.
Second, the for loop in "filterZin" will only filter letter # 98, which I am guessing was not your intention.
Also, geFilterd should probably be created outside of the for loop, and in the for loop you (i guess) would want to do
geFilterd[a]=a+32;
a+=32;
Because I couldn't figure out what your overall goal was for this program, I made a version of it that does what I think you were trying to do, but again, I do not know.
import java.util.Scanner;
public class Deel1 {
public static void main(String[] args) {
String phraseInput = getInput();
filterPhrase(phraseInput);
}
static String getInput() {
Scanner scan = new Scanner(System.in);
String phraseInput = "";
System.out.println("Voer een zin in: ");
if (scan.hasNextLine()) {
phraseInput = scan.nextLine().trim();
}
if (phraseInput.equals("")) {
System.out.println("Geen invoer!");
System.exit(0);
}
return phraseInput;
}
static String filterPhrase(String phraseInput) {
String phraseOutput = "";
char onFiltered[] = phraseInput.toCharArray();
String currentFilter = new String(onFiltered);
// for (a = 97; a <= 122; a++) {
// a = a += 32;
// //char filtered[] = //second array to be printed
// }
char[] filtered = new char[26];
for(int i=97;i<=122;i++){
char a = currentFilter.charAt(i);
filtered[i-97]= (char) (a+32);
}
System.out.println("filtered: " + filtered.toString());
return phraseInput;
}
}

Categories

Resources