将字符串中的单词拆分为每个字母

发布于 2025-01-12 15:29:38 字数 777 浏览 0 评论 0原文

所以我基本上是在尝试用java制作wordle(如果你不知道它是什么,请查找它是一个有趣的文字游戏),并且我有一个包含一堆单词的文本文件。我使用缓冲阅读器随机选择其中一个单词,这将是您必须猜测的单词,我不知道您在猜测单词后如何检查每个字母。我认为我需要将字符串拆分为单独的字母,因此我尝试将 line.toCharArray() 行作为字符串名称,但这不起作用。这是我读到该行的代码

int min = 1;
    int max = 101;
    Random r = new Random();
    int value = r.nextInt(max-min) + min;
    
    
    int lines = 0;
    File text = new File("text.txt");
     try (BufferedReader br = new BufferedReader(new FileReader(text)))
    {
        String line = null;
        while ((line = br.readLine()) != null) {
            lines++;
                if(lines == value)
                    line.toCharArray()
                    System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

So i am basically trying to make wordle in java(if you don't know what it is look it up its a fun word game), and I have a text file of a bunch of words. I randomly pick 1 of the words using a bufferd reader which would be the word you have to guess, I don't know how you would be able to check each letter after you guess a word. I think I need to split the string up into individual letters so I tried doing this line.toCharArray() line being the string name, but that didn't work. here is the code from where i read the line

int min = 1;
    int max = 101;
    Random r = new Random();
    int value = r.nextInt(max-min) + min;
    
    
    int lines = 0;
    File text = new File("text.txt");
     try (BufferedReader br = new BufferedReader(new FileReader(text)))
    {
        String line = null;
        while ((line = br.readLine()) != null) {
            lines++;
                if(lines == value)
                    line.toCharArray()
                    System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2025-01-19 15:29:38

String.toCharArray()方法返回char[]并且不会修改原始字符串

char[] letters = line.toCharArray();
System.out.println(Arrays.toString(letters));

参考:Java 文档

String.toCharArray() method returns char[] and won't modify the original string

char[] letters = line.toCharArray();
System.out.println(Arrays.toString(letters));

Reference: Java doc

誰ツ都不明白 2025-01-19 15:29:38

tl;dr

for( int codePoint : line.codePoints().toArray() ) { … }

避免 char

作为 16 位值,char 类型在物理上无法表示大多数字符。自 Java 2 以来,char 类型已基本上被破坏,并且自 Java 5 以来已被遗留。

代码点

而是使用 代码点整数。代码点是 Unicode 联盟永久分配给每个字符的数字。

下面的代码用于获取某些文本的代码点作为数组。此代码生成一个 IntStream 来访问每个字符的代码点。然后我们将该流的元素收集到一个 int 原始数字数组中。

int[] codePoints = line.codePoints().toArray() ;

现在您可以循环这些代码点编号。

for( int codePoint : codePoints ) 
{
    …
}

您可以通过多种方式从代码点数字生成文本。

这是一个使用 Character.toString 的方法。

String s = Character.toString( codePoint ) ;

另一个是调用StringBuilder#appendCodePoint


顺便说一句,ThreadLocalRandom 类比 Random 类具有更方便的方法。

这:

int min = 1;
int max = 101;
Random r = new Random();
int value = r.nextInt(max-min) + min;

……变成这样,不需要任何数学。

int origin = 1, bound = 101;
int value = ThreadLocalRandom.current().nextInt( origin, bound );

提示:努力为变量命名,而不是“值”。


tl;dr

for( int codePoint : line.codePoints().toArray() ) { … }

Avoid char

As a 16-bit value, char type is physically incapable of representing most characters. The char type has been essentially broken since Java 2, and legacy since Java 5.

Code points

Instead use code point integer numbers. A code point is the number permanently assigned to each character by the Unicode Consortium.

Here is code to get the code points of some text as an array. This code generates an IntStream to access each character’s code point. Then we collect the elements of that stream into an array of int primitive numbers.

int[] codePoints = line.codePoints().toArray() ;

Now you can loop those code point numbers.

for( int codePoint : codePoints ) 
{
    …
}

You can generate text from the code point numbers in various ways.

Here’s one, using Character.toString.

String s = Character.toString( codePoint ) ;

And another is calling StringBuilder#appendCodePoint.


By the way, the ThreadLocalRandom class has more convenient methods than Random.

This:

int min = 1;
int max = 101;
Random r = new Random();
int value = r.nextInt(max-min) + min;

… becomes this, without any math needed.

int origin = 1, bound = 101;
int value = ThreadLocalRandom.current().nextInt( origin, bound );

Tip: Work on better naming of your variables than “value”.


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文