gpt4 book ai didi

java解析数组输入控件

转载 作者:行者123 更新时间:2023-12-02 11:20:20 25 4
gpt4 key购买 nike

感谢您查看我的问题。

首先,该计划有以下目标;用户输入货币格式为“xD xC xP xH”;程序检查输入是否正确,然后打印回“长”版本:“x Dollars, x Cents, x Penny's, x half penny's”

这里我有一些代码,它将用户的输入作为字符串currencyIn,将字符串拆分为数组标记,然后用美元等替换D并打印输出。

public class parseArray
{

public parseArray()
{
System.out.print('\u000c');
String CurrencyFormat = "xD xS xP xH";
System.out.println("Please enter currency in the following format: \""+CurrencyFormat+"\" where x is any integer");
System.out.println("\nPlease take care to use the correct spacing enter the exact integer plus type of coin\n\n");

Scanner input = new Scanner(System.in);
String currencyIn = input.nextLine();
currencyIn.toUpperCase();
System.out.println("This is the currency you entered: "+currencyIn);

String[] tokens = currencyIn.split(" ");

for (String t : tokens)
{
System.out.println(t);
}
String dollars = tokens[0].replaceAll("D", " Dollars ");
String cents = tokens[1].replaceAll("C", " cents");
String penny = tokens[2].replaceAll("P", " Penny's");
String hPenny = tokens[3].replaceAll("H", " Half penny's");

System.out.println(" "+dollars+ " " +cents+ " " +penny+ " " +hPenny);
input.close();
}
}

问题 1: 目前,程序会打印出您输入的几乎所有内容。我如何建立一些输入控件?我在教科书上看到过使用 switch 语句和一系列 if 语句来完成此操作,但对我来说太复杂了。它会使用 charAt() 解析数组中每个元素的字符吗?

问题 2:是否有“更好”的方式来打印输出?我的 friend 说将我的 4 个字符串(dollars、cents、penny's、hpenny's)转换为新数组(称为 newArray)的元素 0、1、2、3 并按如下方式打印:

System.out.println(Arrays.toString(newArray));

提前非常感谢。

最佳答案

有一个巧妙的解决方案,涉及 Regular Expressions , Streams和一些lambdas 。核心概念是我们通过正则表达式定义输入格式。我们需要一些数字序列,后跟 'D''d' ,后跟 " " ,后跟一系列数字,再后跟 Cc ,...我将跳过此模式的推导,它在我上面链接的正则表达式教程中进行了解释。我们会发现

final String regex = "([0-9]+)[D|d]\\ ([0-9]+)[C|c]\\ ([0-9]+)[P|p]\\ ([0-9]+)[H|h]";

满足我们的需求。通过这个正则表达式,我们现在可以确定我们的 input 是否字符串具有正确的格式( input.matches(regex) ),并提取我们真正感兴趣的信息位( input.replaceAll(regex, "$1 $2 $3 $4") 。遗憾的是, replaceAll 产生另一个 String ,但它将包含我们想要的四位数字序列感兴趣,除以 " " 。我们将使用一些流魔法将 String 转换为 long[] (其中第一个单元格保存 D -值,第二个单元格保存 C -值,...).最终的程序如下所示:

import java.util.Arrays;

public class Test {
public static void main(String... args) {
final String input = args[0];
final String regex =
"([0-9]+)[D|d]\\ ([0-9]+)[C|c]\\ ([0-9]+)[P|p]\\ ([0-9]+)[H|h]";

if (input.matches(regex) == false) {
throw new IllegalArgumentException("Input is malformed.");
}

long[] values = Arrays.stream(input.replaceAll(regex, "$1 $2 $3 $4").split(" "))
.mapToLong(Long::parseLong)
.toArray();

System.out.println(Arrays.toString(values));
}
}

如果你想拥有List<Long>而是 long[] (或 List<Integer> 而不是 int[] ),您可以使用

        List<Long> values = Arrays.stream(input.replaceAll(regex, "$1 $2 $3 $4").split(" "))
.map(Long::parseLong)
.collect(Collectors.toList());

需要改mapToLongmap收到 Stream<Long>而不是 LongStream 。我确信有人可以以某种方式编写一个自定义 Collector 对于 LongStream将其转换为List<Long> ,但我发现这个解决方案更具可读性和可靠性(毕竟,使用的 Collector 来自 Oracle,我相信他们广泛测试了他们的代码)。

这是一些示例调用:

$> java Test "10D 9c 8p 7H"
[10, 9, 8, 7]

$> java Test "10E 9C 8P 7H"
Exception in thread "main" java.lang.IllegalArgumentException: Input is malformed.
at Test.main(Test.java:10)

$> java Test "10D 9C 8P 7H 10D 9C 8P 7H"
Exception in thread "main" java.lang.IllegalArgumentException: Input is malformed.
at Test.main(Test.java:10)

关于java解析数组输入控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49966655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com