2017-08-31 175 views
0

對於我的Java功課,我需要創建一個腳本,返回字符串中的第一個字,並且,作爲第二部分,我還需要返回第二個字。我目前正在研究第一部分,我認爲我很接近,但是我也想知道是否我的代碼稍微複雜了一些。我在Java中簡單解決一個簡單的解決方案嗎?

public static void statements(){ 
    Scanner userInput = new Scanner(System.in); 
    char [] sentenceArray; 
    String userSentence; 
    char sentenceResult; 
    System.out.print("Enter a complete sentence: "); 
    userSentence = userInput.nextLine(); 
    for(int x = 0; x < userSentence.length(); x++){ 
     sentenceResult = userSentence.charAt(x); 
     sentenceArray = new char[userSentence.length()]; 
     sentenceArray[x] = sentenceResult; 
     if(sentenceArray[x] != ' '){ 
      System.out.print(sentenceArray[x]); 
      //break; This stops the code at the first letter due to != ' ' 
     } 
    } 
} 

我想我已經差不多了。我所需要做的只是當for循環識別有空格時退出,但它無論如何打印出整個消息。我只是好奇,如果這可以做得更簡單一點,以及也許暗示我可以做什麼,或者如何完成。

編輯:我能得到的分配使用拆分方法完成。這就是它現在看起來像

public static void statements(){ 
    Scanner userInput = new Scanner(System.in); 
    String userSentence; 
    System.out.print("Enter a complete sentence: "); 
    userSentence = userInput.nextLine(); 
    String [] sentenceArray = userSentence.split(" "); 
    System.out.println(sentenceArray[0]); 
    System.out.println(sentenceArray[1]); 
    } 
} 
+0

爲什麼不使用'Scanner.next()'? – shmosel

+1

掃描儀對此最適合。您只需將字符串傳遞給構造函數,然後再讀取。下一個最好的選擇是將輸入分割爲\ s +'空格。 – Tezra

+0

不知道需求是什麼(你沒有提到任何),但如果你可以使用庫函數請參閱https://stackoverflow.com/a/7683463/1566187的[ – Elyasin

回答

1

因爲這是你的家庭作業,我會很難給你代碼併爲你解決它。

好像你真的過於複雜這一點,大家都知道,所以這是很好的跡象。

我需要創建一個腳本,返回一個字符串, 內的第一個字,併爲第二部分,我還需要返回第二個字

所以,你有一個String對象,然後檢查自己,類的methods

它可以解決它在兩行代碼,但是:

  1. 你必須知道String類的一個特殊的方法,最有用的將是一個可以在某種程度上split字符串爲您
  2. 你需要有大約java regular expressions一些知識 - 單詞被space
  3. 分開你分裂string後,你應該通過一個數組的Wi index得到一個數組,訪問firstsecond元素會是足夠
+0

這幫助。我搜索了這些方法,正如你所提到的那樣,我使用了拆分。我完全不知道這件事,它完全符合我的需要。非常感謝! – HoneybunHero

1

就我個人而言,我認爲你正在超越它。爲什麼不在整行中閱讀並用空格分隔字符串?這不是一個完整的解決方案,只是一個關於如何獲得單詞的建議。

public static void main(String[] args) { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.print("Enter a complete sentence: "); 
    try { 
     String userSentence = reader.readLine(); 
     String[] words = userSentence.split(" "); 
     System.out.println(words[0]); 
     System.out.println(words[1]); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

如果內容會被一個以上的空格字符分隔,將無法工作,所以正則表達式'\\ S +'將在'split'方法 – DevDio

+0

好一點更好@Devdio – Jessica

0

以下是我想做到這一點。爲什麼不返回所有的話嗎?

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

/** 
* Add something descriptive here. 
* User: MDUFFY 
* Date: 8/31/2017 
* Time: 4:58 PM 
* @link https://stackoverflow.com/questions/45989774/am-i-over-complicating-a-simple-solution 
*/ 
public class WordSplitter { 

    public static void main(String[] args) { 
     for (String arg : args) { 
      System.out.println(String.format("Sentence: %s", arg)); 
      List<String> words = getWords(arg); 
      System.out.println(String.format("# words : %d", words.size())); 
      System.out.println(String.format("words : %s", words)); 
     } 
    } 

    public static List<String> getWords(String sentence) { 
     List<String> words = new ArrayList<>(); 
     if ((sentence != null) && !"".equalsIgnoreCase(sentence.trim())) { 
      sentence = sentence.replaceAll("[.!?\\-,]", ""); 
      String [] tokens = sentence.split("\\s+"); 
      words = Arrays.asList(tokens); 
     } 
     return words; 
    } 
} 

當我在命令行中輸入這個運行:

"The quick, agile, beautiful fox jumped over the lazy, fat, slow dog!" 

這裏的結果我得到:

Sentence: The quick, agile, beautiful fox jumped over the lazy, fat, slow dog! 
# words : 12 
words : [The, quick, agile, beautiful, fox, jumped, over, the, lazy, fat, slow, dog] 

Process finished with exit code 0 
+1

嘗試更換與數組列表。他可能還不知道它。 –

+0

它保持。一個伸展目標;容易弄清楚。 – duffymo

相關問題