gpt4 book ai didi

Java ISBN 格式

转载 作者:行者123 更新时间:2023-12-01 10:04:17 25 4
gpt4 key购买 nike

我正在尝试编写一个程序来读取书籍的库存文件并生成写入屏幕的库存报告。我在尝试格式化 ISBN 编号时遇到问题。输入文件中的 ISBN 以 10 位数字序列给出(例如:0321479270)。对于报告,我需要使用 1-3-5-1 数字的模式(例如 0-321-479270)重新格式化它。

这是我到目前为止所拥有的......

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class Inventory
{
public static void main(String[] args)throws IOException
{

//Vaiable declartions
int edition, quanity;
double pricePerBook;
String isbn, author, title, publisher;

//Open the file and set delimiters
File file = new File("inventory.txt");
Scanner inputFile = new Scanner(file);
inputFile.useDelimiter("_|/|\\r?\\n");

//Read from the file
while (inputFile.hasNext())
{
isbn = inputFile.next();
author = inputFile.next();
title = inputFile.next();
edition = inputFile.nextInt();
publisher = inputFile.next();
quanity = inputFile.nextInt();
pricePerBook = inputFile.nextDouble();
System.out.printf("%s" /* %s %s %d %s %d %f "*/, formatISBN(isbn)); //, author, title, edition, publisher, quanity, pricePerBook);
}

//Close the flie
inputFile.close();
}

//ISBN Method
public static String formatISBN(String isbn)
{
if (isbn.length() == 1)
{
isbn += -;
return isbn;
}
}
}

所以我在 ISBN 方法中遇到了麻烦,我似乎不知道如何用“-”打印 ISBN。

最佳答案

最简单的方法是提取子字符串:

return String.format("%s-%s-%s-%s",
isbn.substring(0, 0 + 1),
isbn.substring(1, 1 + 3),
isbn.substring(4, 4 + 5),
isbn.substring(9, 9 + 1));

关于Java ISBN 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36560379/

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