我写了一个程序,提示用户输入他/她的全名First,middle,last。我试图通过查找字符串中的每个空格来使用子字符串方法将该名称分解为三个单独的部分。找到每个空格后,名称的以下部分将存储在新的字符串变量中。
我遇到的问题是,中间名是存储中间名和中间名,因为计数器没有正确停止,因为您将在我的下面的代码中看到,所以我的问题是如何解决这个问题。
请注意,我不想拆分字符串,我不想使用StringTokenizer,我不想使用Arrays,我已经知道如何这样做了。我需要知道的是如何修理我的计数器,我想要做的就是以这种方式工作我并不是在寻找新的方法来做到这一点;我只使用substring和charAt。
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
String name = "",
firstName = "",
middleName = "",
lastName = "";
Scanner in = new Scanner(System.in);
System.out.print("Please enter your name 'First Middle Last': ");
name = in.nextLine();
int i = 0;
while(i < name.length())
{
if(name.charAt(i) == ' ')
{
firstName = name.substring(0, i); // Starts at 0 ends at first occurence of ' '
/* middleName = name.substring(i + 1, i); Should start at i + 1,
or one postion after the previously located white space and
then end at the next occurence of ' '
^ The above does not work, and this is where the issue is
happening, how ever if I do the following */
middleName = name.substring(i + 1, name.length());
/* This does work but because the endIndex is name.length() it
runs the length of name and grabs both the middle and last name.*/
i = name.length();
}
++i;
}
System.out.print("\nDisplayed with last name first: " + "\nLast Name: " + lastName + "\nFisrt Name: " + firstName + "\nMiddle Name: " + middleName);
}
}
这产生的输出看起来像这样
Please enter your name 'First Middle Last': First Middle Last
Displayed with last name first:
Last Name:
First Name: First
Middle Name: Middle Last
如您所见,第一个名称正确显示。 中间名不是因为它包括&#34;最后&#34;而不只是&#34;中间&#34;
答案 0 :(得分:3)
您可以使用protocol Base {
typealias Element
func first() -> Element?
}
protocol Child: Base {
typealias Key
typealias Value
func last() -> (Key, Value)?
}
extension Child where Self.Element == (Self.Key, Self.Value) {
var firstKey:Key? { return self.first()?.0 }
}
struct ChildStruct: Child {
func first() -> (String, Int)? {
return ("Foo", 1)
}
func last() -> (String, Int)? {
return ("Bar", 2)
}
}
let c = ChildStruct()
let first = c.first()
let firstKey = c.firstKey
和indexOf(' ')
查找第一个空格和最后一个空格
lastIndexOf(' ')
firstname
lastname
伪代码:
middlename
答案 1 :(得分:2)
您可以使用substring
功能代替split
:
String string = "Peter Ralph Joseph"; //Here the string
String[] parts = string.split(" "); //Here the string it is divide taking as reference the space
System.out.println(parts[0]); //Peter
System.out.println(parts[1]); //Ralph
System.out.println(parts[2]); //Joseph
在我看来,使用split
函数更容易,更快捷。
答案 2 :(得分:0)
如果您真的想使用charAt
,而不是使用substring
循环,那么您可以这样做:
int firstSpace = fullName.indexOf(' ');
String firstName = fullName.substring(0, firstSpace);
int secondSpace = fullName.indexOf(' ', firstSpace+1);
String middleName = fullName.substring(firstSpace+1, secondSpace);
String lastName = fullName.substring(secondSpace+1);
答案 3 :(得分:0)
var mode = 0;
var last = 0;
for(i=0;i<name.length();i++)
{
if(name.charAt(i)==' '||i==(name.length()-1))
{
mode++;
var text = name.substring(last, i-last);
last = i+1;
if(mode==1) { firstName = text; }
else if(mode==2) { middleName = text; }
else if(mode==3) { lastName = text; break; }
}
}
答案 4 :(得分:0)
按以下方式进行: 无需运行while循环。
firstName=name.substring(0,name.indexOf(' '));
middleName=name.substring(name.indexOf(' ')+1,name.lastIndexOf(' '));
lastName=name.substring(name.lastIndexOf(' ')+1,name.length());