Word Scramble Not Scrambling Words - java

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

Related

Problems with iterated nextLine function

I am trying to use user inputted N lines of N characters to do some operations with. But first I need to know N and another int being inputted. When I define N and the other integer K and then write 5 lines (in this case) of 5 characters each the program runs well. But when I use the represented String a (which I then would split into 2 ints, N and K, not shown here to not complicate things), an error occurs. Even if I now input 6 lines, being the 5 last of 5 characters each, the program gives an error of no line found for the multi function. I don't understand what's the problem, and if I remove the string a and just define N and K the program runs well. What's more surprising, the program runs if I use an interactive console instead of text input and write the terms one by one.
static String [][] vetor (int N) {
Scanner scan = new Scanner(System.in);
String[][] multi = new String [N][N];
for (int i = 0 ; i<N ; i++){
String forest = scan.nextLine();
String[] chars = forest.split("");
for (int k=0; k<N; k++){
multi[i][k]= chars [k];
}
}
return multi;
}
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String a = scan.nextLine();
int N = 5;
int K = 5;
String [][] multi = vetor(N);
I've tried many things, but I can't make sense of this. I didn't find any similar questions, but feel free to redirect me to an explanation.
Edit: This is a similar program one can run (with a possible input down (K<= N)) :
import java.util.Scanner;
import java.util.Arrays;
public class Main {
static int[] numerificar() {
Scanner myObj = new Scanner(System.in);
String Input = myObj.nextLine();
String[] Inputs = Input.split(" ", 0);
int size = Inputs.length;
int [] a = new int [size];
for(int i=0; i<size; i++) {
a[i] = Integer.parseInt(Inputs[i]);}
return a;
}
static String [][] vetor (int N) {
Scanner scan = new Scanner(System.in);
String[][] multi = new String [N][N];
for (int i = 0 ; i<N ; i++){
String forest = scan.nextLine();
String[] chars = forest.split("");
for (int k=0; k<N; k++){
multi[i][k]= chars [k];
}
}
return multi;
}
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int[] a = numerificar();
int N = a[0];
int K = a[1];
int cadeira = 0;
String [][] multi = vetor(N);
for (int i = 0 ; i<N ; i++){
if (cadeira == 1) {
break;
}
for (int k=0; k<N-K+1; k++){
if (cadeira == 1) {
break;
}else if( multi[i][k].equals(".")){
for (int j=0; j<K; j++){
if(multi[i][k+j].equals( "#")){
k+=j;
break;
} else if (j == K-1) {
cadeira = 1;
}
}
}
}
}
System.out.println(cadeira);
}
}
5 3
.#.##
#####
##...
###..
#####
The output should be 1 in this case.
The problem is you are creating more than one Scanner that reads from System.in. When data is readily available, a Scanner object can read more data than you ask from it. The first Scanner, in the numerificar() method, reads more than the first line, and those lines are not available to the second Scanner, in the vetor() method.
Solution: use just one Scanner object in the whole program.
public class Main {
static Scanner globalScanner = new Scanner(System.in);
static int[] numerificar() {
String Input = globalScanner.nextLine();
String[] Inputs = Input.split(" ", 0);

How can I make a java program that takes an input of words, finds certain words, replaces them and then prints out everything again?

I am not sure what to do, I have found all this online and I am trying to change it to do what I explained above but I am stuck. Basically what I want to do is copy and paste an essay into the code somewhere, the have it look through the essay for any words i tell it to look for, and if it finds them then to replace it with the word or words I want it to.
/**
*
import java.util.Arrays;
public class Main {
public static String[] wordList(String line){
return line.split(" ");
}
public static void main(String args[]) {
String words = "test words tesing";
String[] arr = wordList(words);
for(words i=0; i<words.length; i++)
for (String s: arr)
System.out.println(s);
}
}
*/
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main ( String args[] ){
String[] sEnterWord = getSortedWordArr();
showWordlist(sEnterWord);
String sWordToChange = getInputFromKeyboard("Which word would you like to change? ");
System.out.println("You have chosen to change the word : " + sWordToChange);
changeWordInArray(sWordToChange, sEnterWord);
Arrays.sort(sEnterWord);
showWordlist(sEnterWord);
}
private static String[] getSortedWordArr(){
String line = getInputFromKeyboard("How many words are you going to enter? ");
int length = Integer.valueOf(line);
String[] sEnterWord = new String[length];
for(int nCtr = 0; nCtr < length; nCtr++){
sEnterWord[nCtr] = getInputFromKeyboard("Enter word " + (nCtr+1) + ":");
}
Arrays.sort(sEnterWord);
return sEnterWord;
}
private static String getInputFromKeyboard(String prompt){
System.out.print(prompt);
Scanner s = new Scanner(System.in);
String input = s.nextLine();
return input;
}
private static void showWordlist(String[] words){
System.out.println("Your words are: ");
for (String w : words){
System.out.println(w);
}
}
private static void changeWordInArray(String word, String[] array){
String newWord = getInputFromKeyboard("Enter the new word: ");
for (int i = 0; i < array.length; i++){
if (array[i].equals(word)){
array[i] = newWord;
break;
}
}
Arrays.sort(array);
}
}
To read from the keyboard use
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);
String cadena = br.readLine();
And all the words you introduced form the keyboard will be in "cadena".
To separte all the words you introduced you could use the method split from String.class.
String[] words = cadena.split(" ");
To find a specific word you could use a method and the code would be in your method would be:
String yourWord = "";
for(int i = 0; i < words.length; i++)
{
if(words[i].equals("your word"))
{
yourWord = words[i];
break;
}
}
To replece a word use the method replce(theWord, "the replacement")
You can prints all the words using a loop for with System.out.println(yourWord);

when I input 4 abcd bcda cdab dabc,I want the result 1,but it turn out to be 4,I don't know why?

when I input
4
abcd
bcda
cdab
dabc
I want the result 1, but it turn out to be 4, I don't know why?
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
String[] strArr = new String[N];
for (int i = 0; i < N; i++) {
strArr[i] = sc.nextLine();
}
System.out.println(fun(strArr));
}
public static int fun(String[] arr) {
TreeSet<String> treeSet = new TreeSet<>();
for (int i = 0; i < arr.length; i++) {
char[] chars = arr[i].toCharArray();
Arrays.sort(chars);
treeSet.add(chars.toString());
}
return treeSet.size();
}
}
Here is your working code:
/**
* #param args
*/
public static void main(final String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
String[] strArr = new String[N];
// StringBuffer sb = new StringBuffer();
for (int i = 0; i < N; i++) {
strArr[i] = sc.nextLine();
}
System.out.println(fun(strArr));
}
public static int fun(final String[] arr) {
TreeSet<String> set = new TreeSet<>();
for (int i = 0; i < arr.length; i++) {
char[] chars = arr[i].toCharArray();
Arrays.sort(chars);
set.add(new String(chars));
}
return set.size();
}
The problem is as you can see the toString of chars delivers you the memory address representation. therefore you get 4 items as it is always a new instance of the same string.
You are using treeSet.add(chars.toString()); which actually refers to Object.toString() which formats the array as something like [C#39ed3c8d.
If you use:
treeSet.add(Arrays.toString(chars));
the strings will look like [a, b, c, d] which should then do what you want.

Making a word Scrambler with Math.random

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

Changing input so in can take any length of string, problems with output

Currently I have a method that asks user for an input string but only outputs the first 16 characters! The method is supposed to take in any length of string then output the characters in 4x4 blocks after it does the following: first row remains the same. Shift the second row one position to the left, then shifts the third row two positions to the left. Finally, shift the fourth row three positions to the left. As of now it will only output the first 4x4 block
Also I am not sure how I can change the method so it doesnt ask for user input
I would like it to use a given string like:
String text = shiftRows("WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO");
"WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO" is the given encrypted string I would like to use. but without asking for user input..I keep getting errors and incorrect outputs..please show how I might fix this
code I am using:
public class shiftRows {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] input= new String[4];
String[] output= new String[4];
System.out.println("Enter a String");
String inputStr = sc.next();
for (int i = 0, n = 0; i < 4; i++, n+=4) {
input[i] = inputStr.substring(0+n, 4+n);
}
// -
output[0] = input[0];
for(int i=1; i<4; i++)
{
output[i] = Shift(input[i],i);
}
for(int i=0; i<4; i++)
{
System.out.println(output[i]);
}
}
public static String Shift(String str, int shiftNum)
{
char[] out = new char[4];
if(shiftNum==1)
{
out[0]=str.charAt(1);
out[1]=str.charAt(2);
out[2]=str.charAt(3);
out[3]=str.charAt(0);
}
if(shiftNum==2)
{
out[0]=str.charAt(2);
out[1]=str.charAt(3);
out[2]=str.charAt(0);
out[3]=str.charAt(1);
}
if(shiftNum==3)
{
out[0]=str.charAt(3);
out[1]=str.charAt(0);
out[2]=str.charAt(1);
out[3]=str.charAt(2);
}
return new String(out);
}
}
Here's a good way to do it :
import java.util.Scanner;
public class shiftRows {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String inputStr = "WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO";
for (int i = 0 ; i < inputStr.length() ; i++){
System.out.print(inputStr.charAt(i));
if ((i + 1)%4 == 0) System.out.println();
}
}
}
If you want to stock it into a String, just concatenate at each loop and add a "\n" each time the if test is valid.

Categories

Resources