在Java中,如何在字符串数组中的单词/数字之间添加一行?
让人困惑的标题,但我想知道的是如何将一长串单词或数字分开 我正在草草记下用户键入的城镇/城市的邮政编码。一些城镇有很多邮政编码,输出相当长 我想每100个字符把这些行分开,这样更容易阅读在Java中,如何在字符串数组中的单词/数字之间添加一行?,java,Java,让人困惑的标题,但我想知道的是如何将一长串单词或数字分开 我正在草草记下用户键入的城镇/城市的邮政编码。一些城镇有很多邮政编码,输出相当长 我想每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, 6060
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;
}
}
import java.util.ArrayList;
导入java.io.*;
导入java.util.*;
/**
*在这个类中,我将获得两个类型为
字符串和数组列表。
*字符串将是用户键入的位置和zip
代码将是邮政编码
*存在于该城镇/城市内的。
*/
公共课场所
{
私有数组列表zipCodes;
私有字符串位置;
/**
*在这个方法中,我们找到了我们将在其中检索的文件
数据。我们把它放在扫描仪里
*将每一行放在一个数组列表中,该列表是ZipCodes。
*@如果文件不存在,则抛出FileNotFoundException。
*/
public Place()引发FileNotFoundException
{
File zipsFile=新文件(“/Users/adanvivero/IdeaProjects/assignment
6/zips.txt”);
zipCodes=newarraylist();
扫描仪坐标=新扫描仪(zipsFile);
while(coordinates.hasNextLine())
{
字符串lineScan=coordinates.nextLine();
zipCodes.add(行扫描);
}
}
/**
*在这种方法中,我们打印出所在城镇的邮政编码
用户输入了。
*@throws-FileNotFoundException我们使用这个,因为我们在中使用了一个文件
字符串方法setZipCode
*下面。
*/
public void addPlace()引发FileNotFoundException
{
字符串summary=location+”的邮政编码为:“+setZipCodes();
如果(summary.length()>100)
{
System.out.println();
系统输出打印(摘要);
}
}
/**
*在这个方法中,我们检索城市/城镇的邮政编码
两个ArrayList因为我不需要
*获取文件的坐标,而仅获取文件的名称
城镇和它的邮政编码。因此,
*我将邮政编码添加到其中一个ArrayList中,如下所示
实例正好是我们的私人实例
*另一个Arraylist中位置的名称。
*@return我们返回一个字符串,该字符串将是位于
数组,但已转换为字符串。
*@throws FileNotFoundException我们检索这些
将在中显示。如果文件不存在,
*我们抛出一个FileNotFoundException。
*/
公共字符串setZipCodes()引发FileNotFoundException
{
File zipsFile=新文件(“/Users/adanvivero/IdeaProjects/assignment
6/zips.txt”);
//ArrayList zips=新的ArrayList();
zipCodes=newarraylist();
ArrayList代码=新的ArrayList();
扫描仪控制台=新扫描仪(zipsFile);
while(console.hasNextLine())
{
字符串z=console.nextLine();
字符串[]zip=z.split(“\t”);
字符串[]代码=z.split(“\t”);
if(location.equalsIgnoreCase(代码[3]))
{
zipCodes.add(zip[0]);
代码。添加(代码[3]);
}
}
String theSummary=zipCodes.toString();
对于(int i=0;iIn addPlace(),请执行以下操作:
String theSummary=location+”有邮政编码:“+setZipCodes();
字符串theNewSummary=“”;
for(int i=0;i
为了每行显示10个Zipcode:
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 );
String theSummary=location+“有邮政编码:\r\n”;
字符串[]zipCodes=setZipCodes().split(“”);
对于(int i=0;i
从addPlace()可以调用此方法格式化输出
这种方法的优点是它可以:-
将字符串拆分为任意长度
在邮政编码中间没有中断。
工作代码:-
/**
* Format a String to specific line length.
* @param summary : Input string
* @param maxLineLength Max line length.
* @param delim String delimeter to be used for breaking the string like space or comma
* @return the formated sting
*/
public static String formatSummary(String summary, int maxLineLength, String delim) {
//convert long string to array of word by delim and discard empty string.
List<String> summarySplitByDelim = Arrays.stream(summary.split(delim)).filter(a->a.length() > 0)
.collect(Collectors.toList());
String newLine = System.getProperty("line.separator");
StringBuilder formattedSummary = new StringBuilder();
StringBuilder currentLine = new StringBuilder();
for (String currentWord : summarySplitByDelim) {
// Keep on adding word by word to currentline
currentLine.append(currentWord.trim()).append(delim);
//if current line execeed our line length, dump everything to formatedSummary and reset currentline
if(currentLine.length() > maxLineLength){
formattedSummary.append(currentLine).append(newLine);
currentLine = new StringBuilder();
}
}
//Add the last line if not added because the loop exited.
if(currentLine.length() > 0){
formattedSummary.append(currentLine).append(newLine);
}
return formattedSummary.toString();
}
/**
*将字符串格式化为特定的行长度。
*@param summary:输入字符串
*@param maxLineLength最大线长。
*@param delim String delimeter用于断开空格或逗号之类的字符串
*@返回格式化的刺
*/
公共静态字符串formatSummary(字符串摘要、int maxLineLength、字符串delim){
//通过delim将长字符串转换为单词数组,并丢弃空字符串。
List summarySplitByDelim=Arrays.stream(summary.split(delim)).filter(a->a.length()>0)
.collect(Collectors.toList());
字符串newLine=System.getProperty(“line.separator”);
StringBuilder formattedSummary=新建StringBuilder();
StringBuilder currentLine=新的StringBuilder();
for(字符串currentWord:summarySplitByDelim){
//继续逐字添加到当前行
currentLine.append(currentWord.trim()).append(delim);
//如果当前行执行我们的行长度,则将所有内容转储到FormattedSummary并重置currentline
如果(currentLine.length()>maxLineLength){
formattedSummary.append(当前行).append(换行符);
currentLine=新的StringBuilder();
}
}
//如果由于循环退出而未添加,请添加最后一行。
如果(currentLine.length()>0){
formattedSummary.append(当前行).append(换行符);
}
返回formattedSummary.toString();
}
调用像formatSummary(data,80,”)这样的方法;
将生成输出
伊利诺伊州芝加哥的邮政编码为:[60601、60602、60603、60604、60605、60606、60607、60608,
60609, 60610, 60611, 60612, 60613, 60614, 60615, 60616, 60617, 60618, 60619, 60620,
60621, 60622, 60623, 60624, 6062
/**
* Format a String to specific line length.
* @param summary : Input string
* @param maxLineLength Max line length.
* @param delim String delimeter to be used for breaking the string like space or comma
* @return the formated sting
*/
public static String formatSummary(String summary, int maxLineLength, String delim) {
//convert long string to array of word by delim and discard empty string.
List<String> summarySplitByDelim = Arrays.stream(summary.split(delim)).filter(a->a.length() > 0)
.collect(Collectors.toList());
String newLine = System.getProperty("line.separator");
StringBuilder formattedSummary = new StringBuilder();
StringBuilder currentLine = new StringBuilder();
for (String currentWord : summarySplitByDelim) {
// Keep on adding word by word to currentline
currentLine.append(currentWord.trim()).append(delim);
//if current line execeed our line length, dump everything to formatedSummary and reset currentline
if(currentLine.length() > maxLineLength){
formattedSummary.append(currentLine).append(newLine);
currentLine = new StringBuilder();
}
}
//Add the last line if not added because the loop exited.
if(currentLine.length() > 0){
formattedSummary.append(currentLine).append(newLine);
}
return formattedSummary.toString();
}