gpt4 book ai didi

java - 如何在 Java 中的字符串数组中的单词/数字之间添加一行?

转载 作者:行者123 更新时间:2023-11-30 01:57:49 25 4
gpt4 key购买 nike

令人困惑的标题,但我想知道的是如何分解一长行单词,或者在本例中是数字。

我正在记下用户输入的城镇/城市的邮政编码。有些城镇/城市有很多邮政编码,并且输出很长。

我想每 100 个字符就将这些行拆分一次,以便更容易阅读。

I would like output to look like this: 

You asked me to search for place: Chicago, Il


Chicago, Il has zip codes: [60601, 60602, 60603, 60604, 60605, 60606,
60607, 60608, 60609, 60610, 60611, 60612, 60613, 60614, 60615, 60616,
60617, 60618, 60619, 60620, 60621, 60622, 60623, 60624, 60625, 60626,
60628, 60629, 60630, 60631, 60632, 60633, 60634, 60636, 60637, 60638,
60639, 60640, 60641, 60643, 60644, 60645, 60646, 60647, 60649, 60651,
60652, 60653, 60654, 60655, 60656, 60657, 60659, 60660, 60661, 60663,
60664, 60665, 60667, 60668, 60669, 60670, 60671, 60672, 60673, 60674,
60675, 60677, 60678, 60679, 60680, 60681, 60683, 60684, 60685, 60687,
60690, 60691, 60693, 60694, 60697, 60699, 60701]

^^^ My current output is that, but all in one line. ^^^

我想知道如何修复它,使其看起来像这样。这是我目前的代码。

import java.util.ArrayList;
import java.io.*;
import java.util.*;

/**
* In this class, I will obtain two private variables that are a type
String and ArrayList.
* The String will be the location that the user types in and the zip
code will be the zip codes
* that exist within that town/city.
*/
public class Place
{
private ArrayList<String> zipCodes;
private String location;

/**
* In this method, we find the file in which we will retrieve this
data. We put it in a Scanner and
* put each line in an ArrayList that is ZipCodes.
* @throws FileNotFoundException if the file doesn't exist.
*/
public Place() throws FileNotFoundException
{

File zipsFile = new File("/Users/adanvivero/IdeaProjects/assignment
6/zips.txt");
zipCodes = new ArrayList<String>();

Scanner coordinates = new Scanner(zipsFile);

while(coordinates.hasNextLine())
{
String lineScan = coordinates.nextLine();
zipCodes.add(lineScan);
}
}

/**
* In this method, we print out the zip code of the town/city in which
the user typed in.
* @throws FileNotFoundException We use this since we used a file in
the String method, setZipCode
* down below.
*/
public void addPlace() throws FileNotFoundException
{
String theSummary = location + " has zip codes: " + setZipCodes();
if(theSummary.length() > 100)
{
System.out.println();
System.out.print(theSummary);
}

}

/**
* In this method, we retrieve the zip code of the city/town. I have
two ArrayList since I don't need
* to get the coordinates of the file, but rather only the name of the
town and the zip code of it. Therefore,
* I add the zip codes into one of the ArrayList, which in this
instance happen to be our private one, and
* the name of the location in another Arraylist.
* @return We return a String that will be the zipCodes which is in an
Array, but converted as a String.
* @throws FileNotFoundException We retrieve the file in which these
will come out in. If the file doesn't exist,
* we throw a FileNotFoundException.
*/
public String setZipCodes() throws FileNotFoundException
{
File zipsFile = new File("/Users/adanvivero/IdeaProjects/assignment
6/zips.txt");
//ArrayList<String> zips = new ArrayList<String>();
zipCodes = new ArrayList<String>();
ArrayList<String> codes = new ArrayList<String>();

Scanner console = new Scanner(zipsFile);

while(console.hasNextLine())
{
String z = console.nextLine();
String [] zip = z.split("\t");
String [] code = z.split("\t");
if(location.equalsIgnoreCase(code[3]))
{
zipCodes.add(zip[0]);
codes.add(code[3]);
}
}
String theSummary = zipCodes.toString();
for(int i = 0; i <= theSummary.length()-1; i++)
{
while(theSummary.charAt(i) == 100)
{
System.out.println();
}
}
return zipCodes.toString();
}

/**
* In this String method, we as the user to type in a place and we
return the place the user typed in so other
* methods can use it.
* @return it returns the String of the location the user types in.
*/
public String getLocation()
{
Scanner wuddup = new Scanner(System.in);
System.out.print("You asked me to search for place: ");
location = wuddup.nextLine();
return location;
}
}

所以我相信解决我的问题是在 setZipCode() 方法下或在我的 addPlace() 方法上。

最佳答案

在 addPlace() 中执行以下操作:

 String theSummary = location + " has zip codes: " + setZipCodes();
String theNewSummary = "";
for(int i = 0; i < theSummary.length()-1; i = i+100){
if(i+99 < theSummary.length()-1)
theNewSummary += theSummary.substring(i, i+99)+"\r\n";
else
theNewSummary += theSummary.substring(i, theSummary.length()-1);

}
System.out.print(theNewSummary );

为了每行显示 10 个邮政编码:

  String theSummary = location + " has zip codes: \r\n";
String[] zipCodes = setZipCodes().split(" ");
for(int i = 0; i < zipCodes.length; i++){
theSummary += " "+zipCodes[i];
if(i%10 == 0 && i !=0)
theSummary +="\r\n"
}
System.out.print(theSummary );

关于java - 如何在 Java 中的字符串数组中的单词/数字之间添加一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53784960/

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