简体   繁体   中英

Making a word scrambler in java

import java.util.Scanner;

public class WordScrambler 
{
    public String prefix, inner, postfix, newword;


    public static void main(String[] args) 
    {
        Scanner sc = new Scanner (System.in);
        String words = sc.nextLine();
        System.out.println(words);
    }


public void Scrambler()

}

String [] words = words.split(" ");

}

}

I'm not really understanding how to get my variable "words" into my scramble method so i can split each word that i put into seperate strings, am i just declaring a new array words when i do this? How do i grab that variable from above. Also, my system.out.println is just checking to see if my scanner worked. Or am i doing it right and is it actually splitting the words?

It seems that you simply want to pass a variable from one method to another.

sidenote: in Java, method names begin with a lowercase letter and are usually verbs: scramble not Scrambler

In:

public static void main(String[] args) 
{
    Scanner sc = new Scanner (System.in);
    String words = sc.nextLine();
    System.out.println(words);
}

You've gotten the input, but still don't have anywhere to send it because you haven't created an instance of WordScrambler yet.

So, let's make one of those:

WordScrambler wordScrambler = new WordScrambler();

Okay, now you have access to the scramble method, so

wordScrambler.scramble( words )

Will get things moving

Now, you'll need to do something with that String in scramble (after you fix the method declaration to accept a String parameter), like split it, jumble the words and output the result, for instance.

import java.util.Scanner;

public class WordScrambler {

    public String prefix, inner, postfix, newword;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String words = sc.nextLine();
        WordScrambler wordScrambler = new WordScrambler();
        wordScrambler.scrambler(words);

    }

    public void scrambler(String words) {

        String[] word = words.trim().split(" ");

        for (int i = 0; i < word.length; i++) {

            System.out.println(word[i]);
        }
    }
}

You need to pass the variable to the method as a parameter. If you make the method static , you can call it simply from your main() .

Also, name your input as (obviously) input , because that's what it is.

Here's how I would do it:

public static void main(String[] args) {
    Scanner sc = new Scanner (System.in);
    String input = sc.nextLine();
    System.out.println(scramble(input));
}

public static String scramble(String input) {
    List<String> words = new ArrayList<String>(Arrays.asList(input.trim().split("\\s+")));
    Collections.shuffle(words);
    return words.toString().replaceAll("[^\\w ]", "");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM