我正在编写一个程序,使用字符串的特定部分写一封信。
这是我目前所拥有的(我只是一个初学者)
import java.util.Scanner;
public class AutoInsurance {
public static void main (String[] args)
{
Scanner scan=new Scanner (System.in);
System.out.print("Enter Name:");
String Name;
Name=scan.nextLine();
System.out.print("Enter Street Address:");
String Address;
Address=scan.nextLine();
System.out.print("Enter city, state, and zip code:");
String Location;
Location=scan.nextLine();
System.out.println();
System.out.println();
System.out.println("Dear "+Name+",");
System.out.println(" You have been selected to receive this offer of auto insurance from");
System.out.println("Allspam Insurance Company! Drivers from "++" saved an average "); // I just want it to print the city here, between ++
// I will finish coding once I figure this out, but I'm stumped
}
}
最佳答案
你在这里能做的最好的事情是用逗号分割你的地址字符串,并从结果数组中获取第一个值。
看看this question有关在 Java 中拆分字符串的更多详细信息。
我建议在您的代码中使用以下内容:
String[] AddressParts = Address.split(",");
String City = AddressParts[0];
System.out.println("Allspam Insurance Company! Drivers from "+City+" saved an average ");
干杯!
关于java - 如何在 java 中只打印字符串的特定部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23019070/