2016-09-22 39 views
3

首先,我对Java和本网站都是全新的。我会尽可能彻底地问我的问题。不过,如果您认为我遗漏了某些东西,请告诉我。Java:我如何确保或让用户在字符串中输入逗号?

我正在做一个学校作业,而我被困在第二部分。我能够提示用户,但不能为我的生活,弄清楚如何确保输入字符串包含逗号。我确实尝试过搜索这个网站,以及谷歌搜索,并没有找到任何东西。也许我不适当地提出这个问题。

(1)提示用户输入包含用逗号分隔的两个字符串的字符串。 (2)如果输入字符串不包含逗号,则报告错误。继续提示,直到输入有效的字符串。注意:如果输入包含逗号,则假定输入也包含两个字符串。

到目前为止,我有这样的:

public static void main(String[] args) { 

    Scanner scnr = new Scanner(System.in); // Input stream for standard input 
    Scanner inSS = null;     // Input string stream 
    String lineString = "";    // Holds line of text 
    String firstWord = "";     // First name 
    String secondWord = "";     // Last name 
    boolean inputDone = false;    // Flag to indicate next iteration 

    // Prompt user for input 
    System.out.println("Enter string seperated by a comma: "); 

    // Grab data as long as "Exit" is not entered 
    while (!inputDone) { 

     // Entire line into lineString 
     lineString = scnr.nextLine(); 

     // Create new input string stream 
     inSS = new Scanner(lineString); 

     // Now process the line 
     firstWord = inSS.next(); 

     // Output parsed values 
     if (firstWord.equals("q")) { 
      System.out.println("Exiting."); 

      inputDone = true; 

     if else (lineString != ",") {  // This is where I am stuck! 
      System.out.print("No comma in string"); 
     } 
     } else { 
      secondWord = inSS.next(); 


      System.out.println("First word: " + firstWord); 
      System.out.println("Second word: " + secondWord); 

      System.out.println(); 
     } 
    } 

    return; 
} 

}

我知道我的 “如果其他” 可能是不正确的。我只是不知道从哪里开始这个特定的命令。不幸的是我的电子书章节没有具体说明。任何想法将不胜感激。非常感谢!

+1

if(lineString.contains(“,”){do something – ivan

+1

您是否尝试过运行它?会发生什么?它是否工作或执行某些意外事件?在解释问题时,告诉我们您做了什么,应该发生什么,以及实际上发生了什么 –

+0

在Java中没有'if else'这样的语句,它是'if(...){...} else {...}'除此之外,请看'String.split() '用逗号分割字符串 –

回答

0

我怀疑你想断言如果输入包含逗号,和至少一个字母。为此,您需要正则表达式:

if (!input.matches("[a-zA-Z]+,[a-zA-Z]+")) { 
    System.out.print("Input not two comma separated words"); 
} 
0

既然你正在寻找它一个逗号的字符串,你想要得到的字符串“之前”中的逗号和字符串“之后的”逗号,然后string.split (',')就是你想要的。询问字符串“包含”一个逗号是否在逗号之前或之后不提供有关字符串的信息。这是string.split()帮助的地方。既然你不关心“你在哪里”,你只需要逗号前面的字符串和逗号后面的字符串。 string.split(',')方法将返回一个字符串数组,包含用逗号(或你的情况)或任何字符分隔的字符串。 例子:

string myString = “firstpart,secondpart”; 

...然后

string[] splitStringArray = myString.Split(‘,’) 

这将返回大小2,其中

splitStringArray[0] = “firstpart” 
splitStringArray[1] = “secondpart" 

这个信息,你也可以告诉我们,如果用户输入正确的输入的字符串数组...即...

如果splitStringArray.Length(或Size)= 0,那么用户di d不输入任何内容,如果splitStringArray.Length(或Size)= 1,那么用户输入1个没有逗号的字符串......可能会检查退出。如果splitStringArray.Length(或Size)= 2,则用户正确输入字符串。如果splitStringArray.Length(Size)> 2,那么用户输入一个多于一个逗号的字符串。

我希望这有助于描述string.split是如何工作的。然而

你的代码需要一些工作......没有进入太多细节下面是一个C#控制台while循环为例:

inputDone = false; 
while (!inputDone) 
{ 
    Console.Clear(); 
    Console.WriteLine("Enter string seperated by a comma: "); 
    lineString = Console.ReadLine(); 
    string[] splitStringArray = lineString.Split(','); 

    // check for user to quit 
    if (splitStringArray.Length == 1) 
    { 
    if (splitStringArray[0] == "q") 
    { 
     inputDone = true; 
     Console.Clear(); 
    } 
    else 
    { 
     // 1 string that is not "q" with no commas 
    } 
    } 

    if (splitStringArray.Length == 2) 
    { 
    // then there are exactly two strings with a comma seperating them 
    // or you may have ",string" or "string," 
    Console.WriteLine("First word: " + splitStringArray[0]); 
    Console.WriteLine("Second word: " + splitStringArray[1]); 
    Console.ReadKey(); 
    } 
    else 
    { 
    Console.WriteLine("Input string empty or input string has more than two strings seperated by commas"); 
    Console.ReadKey(); 
    } 

} 

希望有所帮助。

相关问题