###前提・実現したいこと
Javaにてダイアログに記入してもらったデータをSQLに追加するプログラムを作成しています。
①ダイアログへ正しくない情報が入力された際に再び入力してもらうこと、
②その際にエラーメッセージを表示してユーザーに間違っている項目や内容がわかりやすくなるように設定したいです。
現在入力のコードを書いていて、SQLへのデータ登録の部分をとりあえずコンソール出力にしています。
###発生している問題・エラーメッセージ
###該当のソースコード
Java
1//確認画面にて部署の情報を数字ではなく文字で出力 2//部署の入力を1、2、3のどれかでないとエラー 3//エラーをダイアログに出力 4 5import static javax.swing.JOptionPane.*; 6import javax.swing.JFrame; 7import javax.swing.JOptionPane; 8 9public class yobidashi{ 10 public static void main(String[] args) { 11 12 //記入の為のダイアログ作成 13 //社員コード、部署、郵便番号、電話番号、性別の型をint型に変更 14 //String msg; 15 JFrame parent = null; 16 17 while(true){ 18 19 //社員コード 20 //msg="社員コード"; 21 String cord = inputCord(); 22 //文字の長さが3じゃなければ再入力 23 if(cord.length() != 3){ 24 continue; 25 } 26 27 //社員名 28 String name = inputName(); 29 //未記入チェック 30 if(name.equals("")){ 31 continue; 32 } 33 34 //部署 35 String depa = inputDepa(); 36 //未記入チェック 37 if(depa.equals("")){ 38 continue; 39 } 40 //int型に変更 41 int d = Integer.parseInt(depa); 42 43 //郵便番号 44 String num = inputNum(); 45 //7桁入力されているかチェック 46 if(!(num.length()==7)){ 47 continue; 48 } 49 50 //住所 51 String add = inputAdd(); 52 //未記入チェック 53 if(add.equals("")){ 54 continue; 55 } 56 57 //電話番号 58 String cal = inputCal(); 59 //10桁か11桁で入力 60 if( !(cal.length()==10) || !(cal.length()==11) ){ 61 continue; 62 } 63 64 //性別 65 String kind = inputKind(); 66 //未記入チェック 67 if(kind.equals("")){ 68 continue; 69 } 70 //int型に変更 71 int k = Integer.parseInt(kind); 72 73 //結果確認 74 String message = "結果確認\n社員コード:"+cord+ 75 "\n社員名:"+name+ 76 "\n部署:"+depa+ 77 "\n郵便番号:"+num+ 78 "\n住所:"+add+ 79 "\n電話番号:"+cal+ 80 "\n性別:"+kind; 81 82 String message2 = "以上で間違いはないでしょうか?"; 83 int result = JOptionPane.showConfirmDialog(parent,message+"\n"+message2); 84 85 switch(result){ 86 case YES_OPTION: System.out.println("YES"); break; 87 case NO_OPTION: System.out.println("NO"); break; 88 case CLOSED_OPTION: System.out.println("CLOSED"); break; 89 default : System.out.println("Unknown value : "+result); 90 } 91 92 //depaメソッド呼び出し 93 depa = depa(d); 94 //kindメソッド呼び出し 95 kind = kind(k); 96 //checkメソッド呼び出し 97 //result = result(r); 98 99 //出力 100 System.out.println("社員コード:"+cord); 101 System.out.println("社員名:"+name); 102 System.out.println("部署(1:SCG 2:CCG 3:KKG):"+depa); 103 System.out.println("郵便番号:"+num); 104 System.out.println("住所:"+add); 105 System.out.println("電話番号:"+cal); 106 System.out.println("性別(1:男 2:女):"+kind); 107 108 109 //checkメソッドがNOの場合break 110 //if(check.equals("NO")) { 111 // break; 112 //} 113 114 } 115 } 116 117 static String depa(int d){ 118 //1,2,3それぞれ当てはまる部署をdepaに代入 119 if(d == 1){ 120 return "SCG"; 121 }else if(d == 2){ 122 return "CCG"; 123 }else if(d == 3){ 124 return "KCG"; 125 }else{ 126 return ""; 127 } 128 } 129 130 static String kind(int k){ 131 //1,2それぞれ当てはまる性別をkindに代入 132 if(k == 1){ 133 return "男"; 134 }else if(k == 2){ 135 return "女"; 136 }else{ 137 return ""; 138 } 139 } 140 141 static String check(int ch){ 142 //1,2それぞれ当てはまる項目をcheckに代入 143 if(ch == 1) { 144 return "YES"; 145 }else if(ch == 2) { 146 return "NO"; 147 }else { 148 return ""; 149 } 150 } 151 //試し書き↓ 152 153 //項目ごとにメソッドの作成の方がいいかもしれない 154 155 static String inputCord() { 156 String cord = JOptionPane.showInputDialog("社員コード(3桁数字)"); 157 return cord; 158 } 159 160 161 static String inputName() { 162 String msg = ""; 163 try { 164 String name = JOptionPane.showInputDialog("社員名"); 165 return name; 166 }catch(Exception e) { 167 JOptionPane.showMessageDialog(null, "入力に不備が発生しました\nもう一度記入し直してください"); 168 return YES_OPTION; 169 } 170 } 171 172 static String inputDepa() { 173 String depa = JOptionPane.showInputDialog("部署(1:SCG 2:CCG 3:KKG)"); 174 return depa; 175 } 176 177 static String inputNum() { 178 String num = JOptionPane.showInputDialog("郵便番号(7桁)"); 179 return num; 180 } 181 182 static String inputAdd() { 183 String add = JOptionPane.showInputDialog("住所"); 184 return add; 185 } 186 187 static String inputCal() { 188 String cal = JOptionPane.showInputDialog("電話番号"); 189 return cal; 190 } 191 192 static String inputKind() { 193 String kind = JOptionPane.showInputDialog("性別"); 194 return kind; 195 } 196}
###試したこと
JFrameやJOptionPaneの使い方がわかりにくく、学習の仕方などお勧めの方法があればご教授願いたいです。
よろしくお願いします。
###補足情報(言語/FW/ツール等のバージョンなど)
回答1件
あなたの回答
tips
プレビュー