2016-11-21 29 views
1

私は答えを探していましたが、私には非常に助けとなるものが見つかりました。Java:文字列を別の文字列と整数変数に分割する

文字列変数を2つの整数変数に分割します。とにかく私が発見した文字列分割メソッドと、それを他の方法でint変数に使用する方法があるのでしょうか?

コード:

import java.util.Scanner; 

public class StringInputStream { 
    public static void main (String [] args) { 
     Scanner inSS = null; 
     String userInput = "Jan 12 1992"; 
     inSS = new Scanner(userInput); 

     String userMonth = ""; 
     int userDate = 0; 
     int userYear = 0; 

     // Can modify anything below here 
     String[] temp = userInput.split(" ", 2); // "1" means stop splitting after one space 
     userMonth = temp[0]; 
     userDate = temp[1]; 
     userYear = temp[2]; 
     // Can modify anything above here 

     System.out.println("Month: " + userMonth); 
     System.out.println("Date: " + userDate); 
     System.out.println("Year: " + userYear); 

     return; 
    } 
} 

出力例:Scannerを使用して、あなたのコメントに基づいて

Month: Jan 
Date: 12 
Year: 1992 
+2

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.Stringを参照)を依頼することになる –

+0

最も簡単な方法ユーザは日、月、年を別々に入力し(スキャナへの3回の呼び出し)、それぞれを別々に検証する。 – alfasin

答えて

4

、およびコード。私は、あなたが

userMonth = inSS.next(); 
userDate = inSS.nextInt(); 
userYear = inSS.nextInt(); 

を望んでいた。しかしScannerがあなたの割り当てのために必要とされない場合、その後、あなたは確かにそれを排除し、

String userInput = "Jan 12 1992"; 
String[] tokens = userInput.split("\\s+"); // <-- split on one or more spaces 
String userMonth = tokens[0]; 
int userDate = Integer.parseInt(tokens[1]); 
int userYear = Integer.parseInt(tokens[2]); 
+0

次の方法を使用すると、私が宣言した値は何もスキップされます。 – Aramza

+0

@Aramzaあなたが求めていることは分かりません。あなたは 'inSS = new Scanner(userInput);を持っています。これはあなたの' Scanner'があなたの定義済み 'String'を読み込んでいることを意味します。 'next()'は 'String'(Jan)を読み込み、' nextInt'は12を読み込み、3番目の 'nextInt'は1992を読み込みます。2番目のバージョンでは' Scanner'をスキップして 'String.split'を使います。 –

-1

日付文字列を扱っているように思えるようなものを使用することができると思います特定のライブラリをうまく使いましょう。

のJava 8

のJava 8(java.text.SimpleDateFormatの)前
import java.time.format.DateTimeFormatter; 
import java.time.temporal.*; 

TemporalAccessor d = DateTimeFormatter.ofPattern("MMM DD yyyy").parse("Jan 12 1992") 
d.get(ChronoField.YEAR); // 1992 
d.get(ChronoField.MONTH_OF_YEAR); // 1 
d.get(ChronoField.DAY_OF_MONTH); // 12 

Date d = new SimpleDateFormat("MMM DD yyyy").parse("Jan 12 1992"); 
Calendar c = Calendar.getInstance(); 
c.setTime(d); 
c.get(Calendar.YEAR); // 1992 
c.get(Calendar.MONTH); // 0 - yeah, it starts from 0, embarrassingly 
c.get(Calendar.DAY_OF_MONTH); // 12 

または使用JodaTime

DateTimeFormat.forPattern("MMM DD yyyy").parseDateTime("Jan 12 1992"); 
+0

この回答は、インストールされたJavaのバージョンが1.8+の場合にのみ適用されます。@LukeLee –

+0

年の月はまだ文字列である必要があります –

+0

OPは日付時間を処理していますが、問題は複雑ではなく**適切な日付ライブラリを使用しません。 –

0

私が使用したくありませんナットを割るためのスレッジハンマー:-)しかし、提供された入力に関してあなたがアプリケーションに必要とする柔軟性によっては、あなたの問題にパーサーを使うことができます。以下の文法をとり、ANTLRに入れると、入力文字列を解析して、それから意味要素を抽出することができます。 Jan 1999 12,1999 Mar 12などの異なる入力順列を扱うことさえできます。

grammar SimpleMixed; 
fragment A:('a'|'A'); 
fragment B:('b'|'B'); 
fragment C:('c'|'C'); 
fragment D:('d'|'D'); 
fragment E:('e'|'E'); 
fragment F:('f'|'F'); 
fragment G:('g'|'G'); 
fragment H:('h'|'H'); 
fragment I:('i'|'I'); 
fragment J:('j'|'J'); 
fragment K:('k'|'K'); 
fragment L:('l'|'L'); 
fragment M:('m'|'M'); 
fragment N:('n'|'N'); 
fragment O:('o'|'O'); 
fragment P:('p'|'P'); 
fragment Q:('q'|'Q'); 
fragment R:('r'|'R'); 
fragment S:('s'|'S'); 
fragment T:('t'|'T'); 
fragment U:('u'|'U'); 
fragment V:('v'|'V'); 
fragment W:('w'|'W'); 
fragment X:('x'|'X'); 
fragment Y:('y'|'Y'); 
fragment Z:('z'|'Z'); 

s: userinput EOF; 
userinput: month date year 
    | month year date 
    | year month date 
    | year date month 
    | date year month 
    | date month year; 

month: Month; 
date: Date; 
year: Year; 
Month: J A N | F E B | M A R | A P R | M A Y | J U N | J U L | A U G | S E P  | 
O C T | N O V | D E C; 

Date: [1-9][0-2]?; 

Year: [1-9][0-9][0-9][0-9]; 


WS : [ \t\r\n]+ -> skip; 

ユーザーが入力jan 1999 12を提供したとします。上の文法からAntlrから生成されたパーサーで解析した後、データ型を推測して適切なオブジェクトを作成するための意味要素を簡単に抽出できるパースツリーが下にあります(例:年と日付のint 、String)を返します。

enter image description here

関連する問題