从 Java 方法,我如何 return 从字符串转换为整个 int?
From a Java method, how do I return an entire int converted from a string?
这个程序的目的是输入像“9/7/1994”这样的日期和 return 由正斜杠分隔的数字。
所以它应该是:
9
7
1994
问题是,一旦我输入日期,它只会 return
9
7
1
我考虑过循环 getYear() 方法,但我不知道该怎么做,也不知道这样做是否正确。
import java.util.*;
public class Testing
{
public static void main(String[] args)
{
getDate();
getMonth(args);
getDay(args);
getYear(args);
}
public static void getDate()
{
System.out.println("Please enter a date: ");
}
public static int getMonth(Object c)
{
char ch = getInputChar();
int month;
getInputChar();
String temp = Character.toString(ch);
month = Integer.parseInt(temp);
System.out.println(month);
return month;
}
public static int getDay(Object c)
{
char ch = getInputChar();
int day;
getInputChar();
String temp = Character.toString(ch);
day = Integer.parseInt(temp);
System.out.println(day);
return day;
}
public static int getYear(Object c)
{
char ch = getInputChar();
int year;
getInputChar();
String temp = Character.toString(ch);
year = Integer.parseInt(temp);
System.out.println(year);
return year;
}
static char getInputChar()
{
char c = '[=12=]';
try
{
c = (char) (System.in.read());
}
catch (java.io.IOException ioe) {}
return c;
}
}
import java.util.*;
public class Testing
{
public static void main(String[] args)
{
getDate();
Scanner sc = new Scanner(System.in);
String date = sc.nextLine();
String [] numbers = date.split("/");
getMonth(numbers[0]);
getDay(numbers[1]);
getYear(numbers[2]);
}
...
}
这会帮助您获得所需的东西吗?
整个程序有两行(如果包括输入格式检查,则为 5 行)解决方案:
System.out.println("Please enter a date: ");
Scanner s = new Scanner(System.in);
String input = s.nextLine();
if (!input.matches("(1[012]|[1-9])/(3[01]|[12][0-9]|[1-9])/\d{4}"))
throw new IllegalArgumentException();
System.out.println(input.replace("/", " "));
要生成正确的(字符串)输出,不需要先解析为整数再转换回字符串。
您可以利用 Java 的字符串正则表达式功能。尝试使用以下代码从输入字符串 date
.
中解析和提取三个整数日期
String date = "9/7/1994";
// the following pattern matches dates of the format DD/MM/YYYY
String pattern = "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\d\d)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(date);
if (m.find()) {
String day = m.group(0); // 9
String month = m.group(1); // 7
String year = m.group(2); // 1994
}
你只读到一位数字,因为你只读了一位数字。以下是 getYear() 的修改版本,它打印所有 4 位数字。
public static void getYear(Object c){
int year_digit=0;
String temp;
for(int i=0;i<4;i++){
char ch = getInputChar();
temp = Character.toString(ch);
year_digit = Integer.parseInt(temp);
System.out.println(year_digit);
}
}
我对你的算法有一个建议:
在 getMonth 和 getDay 中,您调用 getInputChar 两次以获得一个字符并跳过它后面的 '/'。您假设月份和日期只有一位数字。 10/10/1944 怎么样?一种可能的替代算法是从输入流中读取整个字符串,并使用 [输入字符串].split('/') 获取月、日和年的字符串数组。
这个程序的目的是输入像“9/7/1994”这样的日期和 return 由正斜杠分隔的数字。 所以它应该是: 9 7 1994
问题是,一旦我输入日期,它只会 return 9 7 1
我考虑过循环 getYear() 方法,但我不知道该怎么做,也不知道这样做是否正确。
import java.util.*;
public class Testing
{
public static void main(String[] args)
{
getDate();
getMonth(args);
getDay(args);
getYear(args);
}
public static void getDate()
{
System.out.println("Please enter a date: ");
}
public static int getMonth(Object c)
{
char ch = getInputChar();
int month;
getInputChar();
String temp = Character.toString(ch);
month = Integer.parseInt(temp);
System.out.println(month);
return month;
}
public static int getDay(Object c)
{
char ch = getInputChar();
int day;
getInputChar();
String temp = Character.toString(ch);
day = Integer.parseInt(temp);
System.out.println(day);
return day;
}
public static int getYear(Object c)
{
char ch = getInputChar();
int year;
getInputChar();
String temp = Character.toString(ch);
year = Integer.parseInt(temp);
System.out.println(year);
return year;
}
static char getInputChar()
{
char c = '[=12=]';
try
{
c = (char) (System.in.read());
}
catch (java.io.IOException ioe) {}
return c;
}
}
import java.util.*;
public class Testing
{
public static void main(String[] args)
{
getDate();
Scanner sc = new Scanner(System.in);
String date = sc.nextLine();
String [] numbers = date.split("/");
getMonth(numbers[0]);
getDay(numbers[1]);
getYear(numbers[2]);
}
...
}
这会帮助您获得所需的东西吗?
整个程序有两行(如果包括输入格式检查,则为 5 行)解决方案:
System.out.println("Please enter a date: ");
Scanner s = new Scanner(System.in);
String input = s.nextLine();
if (!input.matches("(1[012]|[1-9])/(3[01]|[12][0-9]|[1-9])/\d{4}"))
throw new IllegalArgumentException();
System.out.println(input.replace("/", " "));
要生成正确的(字符串)输出,不需要先解析为整数再转换回字符串。
您可以利用 Java 的字符串正则表达式功能。尝试使用以下代码从输入字符串 date
.
String date = "9/7/1994";
// the following pattern matches dates of the format DD/MM/YYYY
String pattern = "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\d\d)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(date);
if (m.find()) {
String day = m.group(0); // 9
String month = m.group(1); // 7
String year = m.group(2); // 1994
}
你只读到一位数字,因为你只读了一位数字。以下是 getYear() 的修改版本,它打印所有 4 位数字。
public static void getYear(Object c){
int year_digit=0;
String temp;
for(int i=0;i<4;i++){
char ch = getInputChar();
temp = Character.toString(ch);
year_digit = Integer.parseInt(temp);
System.out.println(year_digit);
}
}
我对你的算法有一个建议: 在 getMonth 和 getDay 中,您调用 getInputChar 两次以获得一个字符并跳过它后面的 '/'。您假设月份和日期只有一位数字。 10/10/1944 怎么样?一种可能的替代算法是从输入流中读取整个字符串,并使用 [输入字符串].split('/') 获取月、日和年的字符串数组。