質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

2回答

14847閲覧

式の開始が不正ですと出てしまう。

shou_hitotose

総合スコア66

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2018/11/08 05:55

編集2018/11/08 06:12

先程も質問させていただいたJavaの初心者で自己紹介プログラムを書いているものです。先ほど質問させていただいたプログラムをメソッドをもちいて完結にしようとしたのですが、タイトルの通り式の開始が不正です。というエラー文が出てしまいます。このエラー分についていろいろと調べてみたのですがそれぞれで言ってる内容が違い初心者の私には何が正解かわかりませんでした。

Java

1コード 2class DateOfBirth { 3 4 int birth_year; 5 int birth_month; 6 int birth_date; 7 boolean known; 8 9} 10class Myfriend { 11 12 String name; 13 DateOfBirth the_date_of_birth; 14 String phone_number; 15 16} 17class Person { 18 String name; 19 20 DateOfBirth the_date_of_birth; 21 String phone_number; 22 int n_of_friends; 23 Myfriend[] friend; 24 25} 26 27class ex6_2 { 28 public static void main(String args[]) { 29 Person person_one; 30 Person person_two; 31 person_one = new Person(); 32 person_two = new Person(); 33 34 /* for protection of personal data, pseudo values are used */ 35 person_one.name = "A"; 36 person_one.the_date_of_birth = new DateOfBirth(); 37 person_one.the_date_of_birth.known = true; 38 Birth birth_year1=new Birth(); 39 40 birth_year1.birthday(2004,3,3); 41 42 43 person_one.phone_number = "080-1234-5678"; 44 45 person_two.name = "Adolf Brain Charles David Frederic John"; 46 person_two.the_date_of_birth = new DateOfBirth(); 47 person_two.the_date_of_birth.known = true; 48 Birth birth_year2=new Birth(); 49 birth_year2.birthday(2004,1,23); 50 person_two.phone_number = "090-8765-4321"; 51 52 /* person_one believes that the 'person_two' is a friend */ 53 person_one.n_of_friends = 1; 54 person_one.friend = new Myfriend[1]; 55 person_one.friend[0] = new Myfriend(); 56 person_one.friend[0].name = person_two.name; 57 person_one.friend[0].the_date_of_birth = new DateOfBirth(); 58 person_one.friend[0].the_date_of_birth.birth_year 59 = person_two.the_date_of_birth.birth_year; 60 person_one.friend[0].the_date_of_birth.birth_month 61 = person_two.the_date_of_birth.birth_month; 62 person_one.friend[0].the_date_of_birth.birth_date 63 = person_two.the_date_of_birth.birth_date; 64 person_one.friend[0].the_date_of_birth.known 65 = person_two.the_date_of_birth.known; 66 person_one.friend[0].phone_number = person_two.phone_number; 67 68 /* however, person_two does not think so. */ 69 person_two.n_of_friends = 0; 70 71 /* print the input data */ 72 print_personal_info(person_one); 73 print_personal_info(person_two); 74 } 75 static void print_personal_info(Person p) { 76 int i; 77 78 System.out.print("NAME: " + p.name + "\n"); 79 80 System.out.print("Number of friends = " + p.n_of_friends + "\n"); 81 for( i = 0; i < p.n_of_friends; i++ ) { 82 System.out.print("Name of friend " + i 83 + ": " + p.friend[i].name + "\n"); 84 if( p.friend[i].the_date_of_birth.known == true ) { 85 System.out.print("Date of Birth: " 86 + p.friend[i].the_date_of_birth.birth_year 87 + " " 88 + p.friend[i].the_date_of_birth.birth_month 89 + " " 90 + p.friend[i].the_date_of_birth.birth_date 91 + "\n"); 92 } else { 93 System.out.print(" Date of birth is unknown.\n"); 94 } 95} 96 static void birthday(int year,int month,int day){ 97 System.out.print("Date of Birth: " + this.year 98 + " " + this.month 99 + " " + this.day + "\n"); 100} 101 102 103 104 105} 106 107

(実行結果)
ex6_2.java:96: エラー: 式の開始が不正です
static void birthday(int year,int month,int day){
^
エラー1個

このようになってしまいます。public ,privateなどいろいろと試したのですが結局なにが悪いのかがわかりませんでした。回答および意見をいただけると幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ソースの中身は読んでませんが

Java

1 static void print_personal_info(Person p) {

が閉じられてないからじゃないですか。

投稿2018/11/08 05:58

kunai

総合スコア5405

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shou_hitotose

2018/11/08 06:05

すみません。閉じてみましたがまだエラーが出てしまいます。
kunai

2018/11/08 06:08

ソースがどうなったのか、今回表示されているエラーはなんなのかを提示いただけないとこれ以上はなんとも。
shou_hitotose

2018/11/08 06:09

そうでした。すみません。もう一度編集しなおします。
kunai

2018/11/08 06:16

インデントを正しく揃えて書いてみて下さい。 今show_hitotoseさんが「閉じた」のは[ for( i = 0; i < p.n_of_friends; i++ ) { ] であって、[ static void print_personal_info(Person p) { ] はまだ開いたままです。 というか、取り敢えず閉じておけ、ということではなく、ちゃんとコードを理解して「ここでfor文を終了する」という意思をもって閉じてくださいね?
shou_hitotose

2018/11/08 06:17

すみません、不慣れなもので。ご指摘ありがとうございます。
shou_hitotose

2018/11/08 06:25

なんとか式の開始が不正です。を乗り越えることができました。しかし、今度はまた別のエラーが出てしまい今また少し考えたいと思います。
shou_hitotose

2018/11/08 06:27

2回でてしまいました。すみません。とりあえず今回はご回答頂きありがとうございます。
guest

0

ベストアンサー

kunaiさんも言う通り、メソッドが正しく閉じられていません。

java

1 static void print_personal_info(Person p) { 2 int i; 3 4 System.out.print("NAME: " + p.name + "\n"); 5 6 System.out.print("Number of friends = " + p.n_of_friends + "\n"); 7 for( i = 0; i < p.n_of_friends; i++ ) { 8 System.out.print("Name of friend " + i 9 + ": " + p.friend[i].name + "\n"); 10 if( p.friend[i].the_date_of_birth.known == true ) { 11 System.out.print("Date of Birth: " 12 + p.friend[i].the_date_of_birth.birth_year 13 + " " 14 + p.friend[i].the_date_of_birth.birth_month 15 + " " 16 + p.friend[i].the_date_of_birth.birth_date 17 + "\n"); 18 } else { 19 System.out.print(" Date of birth is unknown.\n"); 20 } // <- ここはif-elseの閉じかっこで、まだfor文の中。当然print_personal_infoの途中 21 // ↓つまりこれは「メソッドの中にメソッドを定義している」状態のため、エラーが出ている。 22 static void birthday(int year,int month,int day){ 23 System.out.print("Date of Birth: " + this.year 24 + " " + this.month 25 + " " + this.day + "\n"); 26} 27

投稿2018/11/08 06:11

swordone

総合スコア20649

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shou_hitotose

2018/11/08 06:22

ご指摘ありがとうございます。ご指摘どおり閉じましてなんとか指揮の開始が不正ですとは表示されなくなりました。
shou_hitotose

2018/11/08 06:24

ただそのあとでシンボルが見つけられないなど新たなエラーが発生しこれはまた考えたいと思います。また質問をこのこーどで書いてしまうかもしれませんがそのときはまた助言してくださると幸いです。今回はありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問