使用substring()在Java中处理字符串
在这个程序中,我目前要求用户以mm/dd/yyyy格式输入日期,但我希望能够使用格式为m/d/yyyy、mm/d/yyyy或m/dd/yyyy的条目。我该怎么做使用substring()在Java中处理字符串,java,string,Java,String,在这个程序中,我目前要求用户以mm/dd/yyyy格式输入日期,但我希望能够使用格式为m/d/yyyy、mm/d/yyyy或m/dd/yyyy的条目。我该怎么做 import java.util.Scanner; public class BirthdayReminder { public static void main(String[] args) { // declare variables here String[] names = new Str
import java.util.Scanner;
public class BirthdayReminder
{
public static void main(String[] args)
{
// declare variables here
String[] names = new String[10];
String inputName;
String birthday;
int[] month = new int[10];
int[] day = new int[10];
int[] year = new int[10];
int count = 0;
final int MAX = 10;
final String QUIT = "ZZZ";
String inputMonth;
String inputDay;
String inputYear;
Scanner input = new Scanner(System.in);
System.out.print("Enter the name of a friend >> ");
inputName = input.nextLine();
System.out.print("Enter friend's birthday in format mm/dd/yyyy >> ");
birthday = input.nextLine();
// allow user to enter up to 10 friends, first their name, then their birthday with slashes
// between the month, day, and year, or until they enter ZZZ as a name
while(inputName.compareTo(QUIT) != 0)
{
names[count] = inputName;
inputMonth = birthday.substring(0, 2);
inputDay = birthday.substring(3, 5);
inputYear = birthday.substring(6, 10);
int intMonth = Integer.parseInt(inputMonth);
int intDay = Integer.parseInt(inputDay);
int intYear = Integer.parseInt(inputYear);
month[count] = intMonth;
day[count] = intDay;
year[count] = intYear;
++count;
if(count == MAX)
inputName = QUIT;
else
{
System.out.print("Enter next friend's name or " + QUIT + " to quit >> ");
inputName = input.nextLine();
if(inputName.compareTo(QUIT) !=0)
{
System.out.print("Enter friend's birthday in format mm/dd/yyyy >> ");
birthday = input.nextLine();
}
}
}
System.out.println("You have entered " + count + " names.");
System.out.println("The names you entered are ");
for(int y = 0; y < count; ++y)
{
System.out.print(names[y] + " ");
}
// prompt user to enter names of their friends and display the applicable birthday, until the
// user enters ZZZ as a name
if(count != 0)
{
System.out.println("\nEnter friend's name to see his/her birthday or ZZZ to quit >> ");
String friendName = input.nextLine();
for(int i = 0; i < friendName.length() && friendName.compareTo(QUIT) != 0; i++)
{
if(friendName.compareTo(names[i]) == 0)
{
System.out.println("The birthday is " + month[i] + "/" + day[i] + "/" + year[i]);
}
else
{
System.out.println("This name has not been previously entered.");
}
}
}
}
}
import java.util.Scanner;
公共班级生日提醒
{
公共静态void main(字符串[]args)
{
//在这里声明变量
字符串[]名称=新字符串[10];
字符串输入名;
弦乐生日;
整数[]月=新整数[10];
整数[]天=新整数[10];
整数[]年=新整数[10];
整数计数=0;
最终int最大值=10;
最后一个字符串QUIT=“ZZZ”;
字符串输入月份;
字符串输入日;
字符串输入年份;
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入朋友的姓名>>”;
inputName=input.nextLine();
System.out.print(“以mm/dd/yyyy>>格式输入朋友的生日”);
生日=输入.nextLine();
//允许用户最多输入10个朋友,首先输入他们的名字,然后用斜杠输入他们的生日
//在月、日和年之间,或直到输入ZZZ作为名称为止
while(inputName.compareTo(QUIT)!=0)
{
名称[计数]=输入名称;
inputMonth=生日。子字符串(0,2);
inputDay=生日。子字符串(3,5);
inputYear=生日。子字符串(6,10);
int intMonth=Integer.parseInt(inputMonth);
int intDay=Integer.parseInt(inputDay);
int intYear=Integer.parseInt(inputYear);
月[计数]=整数月;
天[计数]=整数天;
年份[计数]=年份;
++计数;
如果(计数=最大值)
inputName=退出;
其他的
{
System.out.print(“输入下一个朋友的姓名或“+QUIT+”退出>>”;
inputName=input.nextLine();
如果(inputName.compareTo(退出)!=0)
{
System.out.print(“以mm/dd/yyyy>>格式输入朋友的生日”);
生日=输入.nextLine();
}
}
}
System.out.println(“您已输入”+count+“名称”);
System.out.println(“您输入的名称是”);
对于(int y=0;y>”;
字符串friendName=input.nextLine();
对于(int i=0;i
我一直想做这样的事
int x = 0;
while(x < birthday.length())
{
if(birthday.charAt(x) == '/')
{
inputMonth = birthday.substring(0, x);
inputDay = birthday.susbtring(x + 1, something);
inputYear = birthday.substring(x + something, birthday.length());
}
++x;
}
intx=0;
而(x
但我不确定如何让它工作,因为用户条目将包含两个斜杠而不是一个,因此将有两个x
另外,我刚刚学习了基本的java编程。您可以使用如下代码:
String []myFormat = {"m/d/yyyy" , "mm/d/yyyy" , "m/dd/yyyy"};
birthday = input.nextLine();
for(int i=0 ; i < myFormat.length ; i++)
{
try
{
SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat(myFormat[i]);
Date date = datetimeFormatter1.parse(birthday);
System.out.println(date.toString());
}
catch (Exception e)
{
}
}
String[]myFormat={“m/d/yyyy”、“mm/d/yyyy”、“m/dd/yyyy”};
生日=输入.nextLine();
对于(int i=0;i
我怀疑这是一个学校问题,旨在证明您可以使用子字符串而不是SimpleDataFormat
最简单的方法是避免循环,只需使用String.indexOf(char,firstIndex)两次就可以获得第一个和第二个斜杠索引
int slash1 = birthday.indexOf('/', 0);
int slash2 = birthday.indexOf('/', slash1 + 1);
String inputMonth = birthday.substring(0, slash1);
String inputDay = birthday.substring(slash1 + 1, slash2);
String inputYear = birthday.substring(slash2 + 1, birthday.length());
请使用“好奇”,因为您已经让用户以您在提示中显示的格式作为字符串输入生日,为什么不为提供的生日设置一个字符串数组呢。为什么总是把它从字符串转换成整数呢。我想说的是:既然用户输入的是2017年12月16日的出生日期,为什么要对它进行剖析呢。它只是为了显示某人的生日而做了比需要更多的工作。对,我没有想到…我打算在月数组中存储月,在日数组中存储天,等等…但是你是对的,我可以简单地将每个日期存储在字符串数组中。非常感谢@VIN
SimpleDataFormat
已经过时多年,被java.time类取代。具体地说,DateTimeFormatter
.FYI,像、java.text.SimpleDateFormat
这样的麻烦的旧日期时间类现在被java 8和java 9中内置的类所取代。看见