내 프로젝트에 대한 첫 번째 정보, 주요 목표가 오류 처리, 오류 처리와 함께 작동하는 방법을 배우는 튜토리얼 작업을 따르고 있지만 그 부분은 아직 꽤, 나는 주 수업 . 그게 내가 약간의 도움과지도가 필요하다. (그러나 오류는 올라와서 나중에 픽스를 배울 수 있도록 으로 감지된다. 그러나 여기에는 주된 질문이 아니다. 단지 정보이다.) 다음 중 3 가지가있다. 내가 완성한 수업들. 두 개의 비공개 필드가있는 학생 : 이름 이름 및 CourseCollection 코스. String firstName과 String surname의 두 필드가있는 이름입니다. CourseCollection with ArrayList 코스.Java에서 분할 문자열로 이름을 재구성하는 방법은 무엇입니까?
(정보 : 나중에 UserDatabase 클래스를 사용하여 학생 컬렉션을 수집하고로드하는데, + 클래스 DatavaseFormatException이 오류를 나타낼 수 있지만 처음 3 개 클래스를 끝내기가 더 쉬울 것이라고 생각하십니까? 내가 틀 렸습니다.)
질문 : 클래스 이름에 약간의 도움이 필요합니다. 내 작업이 정확하다고 생각하지 않습니다. 내 주요 문제는 문자열 인코딩 방법이며, 생성자 Name (String encodedIdentity) 이름을 반환하려는 곳은 ;;로 분할됩니다. 여기에 내가 대한
enter code here
public class Name
//instanciating the persons first and last name as Strings.
private String firstName;
private String surname;
/**
* Constructor for objects of class Name- setting the text values for a name
*/
public Name(String firstName, String surname)
{
firstName = this.firstName;
surname = this.surname;
}
/**
* Constructor for objects of class Name- reconstructing the fields
* from a coded name
*/
public Name(String encodedIdentity)
{
//Here, the name is supposed to be coded as a text value, with the first
//and last names split with a ;
this.encodedIdentity=encodedIdentity;
//I am getting my first error here
}
// 게터가 무엇
나는 내 일에 더 도움이 필요합니다 보장 할 수} firstName과 여기
/**
* This method will return the name, with the first and last name split
* by a ;
*/
public String encode()
{
//Could I use a split String here? If so how?
return firstName + ";" + surname;
}
성. 미리 감사드립니다. StackOverflow에있는 사람들이 내 책 이외의 다른 사람이 없기 때문에 매우 도움이됩니다. 도움을 요청하십시오. (나는 너희들이 자유 선생이 아니라는 것을 알고있다. 그러나 아무에게도이 일을 도와 줄 voulenteer가 있다면 나는 그것을 매우 높이 평가할 것이다. (죄송하다면 요청할 수 없다!)
EDIT : 너희들에게 감사한다. , 나의 수업은 지금 컴파일 중이다. 나는 아직 그것을 테스트하는 방법을 모르지만, 이것은 내가 지금 가지고있는 것입니다. 그것은 정확한가?
public Name(String encodedIdentity){
String[] names = split(";", encodedIdentity);
if(names.length != 2) throw new IllegalArgumentError("...");
firstName = names[0];
surname = names[1];
}
하나하지 않는 : 당신이 "인코딩 된 ID"를 부여하는 경우
public class Name
{
/**
* Constructor for objects of class Name- setting the text values for a name
*/
public Name(String firstName, String surname)
{
this.firstName = firstName;
this.surname = surname;
}
/**
* Constructor for objects of class Name- reconstructing the fields
* from a coded name
*/
public Name(String encodedIdentity)
{
String names = firstName + surname;
String[] fullname = names.split("\\;");
if(fullname.length != 2)
{
System.out.println(fullname);
}
}
/**
* Getting the firstname of the person
* @return firstName
*/
public String getFirstName()
{
return firstName;
}
/**
* Getting the surname of the person
* @return surname
*/
public String getSurname()
{
return surname;
}
/**
* This method will return the name, with the first and last name split
* by a ;
*/
public String encode()
{
String encode = (firstName + ";" + surname);
return encode;
}
}
필드에 encodedIdentity가 없습니다. 따라서 코드는 의미가 없습니다. 당신이 말했듯이, 당신은 주어진 encodedIdentity 인자를 나누고 firstName과 surName으로 나누어서 두 부분을 저장해야합니다. 그렇게해라. 또한 ('중괄호가 필요한 public class Name {'과 같은) 기본적인 구문 오류를 수정하고 코드를 들여 쓰기하여 구조를 이해할 필요가 있습니다. –
감사합니다. 하지만 어떻게이 목적으로 코드 (스플릿 문자열)를 성공적으로 작성할 수 있습니까? – Hermos
String의 javadoc을 읽습니다. https://docs.oracle.com/javase/8/docs/api/java/lang/String.html –