###前提・実現したいこと
ここに質問したいことを詳細に書いてください
度々恐れ入ります。
掲題の件について、Javaで実装を行ったところ以下のエラーが返ってきてしまいました。
基本的な文法ができていないのは重々承知ですが、改善点や、ご指摘を頂けると幸いです。
###発生している問題・エラーメッセージ
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 到達不能コード at traning.Test.main(Test.java:36)
###該当のソースコード
Java
1package traning; 2 3import java.nio.charset.Charset; 4import java.nio.file.Files; 5import java.nio.file.Path; 6import java.nio.file.Paths; 7import java.util.ArrayList; 8import java.util.List; 9import java.util.regex.Matcher; 10import java.util.regex.Pattern; 11 12public class Test{ 13 14 public static void main(String[] args) { 15 16 //自インスタンス生成 17 Test test = new Test(); 18 19 //引数の確認 20 if (args.length != 1) { 21 System.out.println("引数を指定してください"); 22 return; 23 } 24 25 //パスオブジェクトをgetメソッドで作成 26 Path path = Paths.get(args[0]); 27 if(!Files.exists(path));{ 28 System.out.println("ファイルが開けないか存在しません。"); 29 return; 30 } 31 32 //ファイル読込 33 List<String> list; 34 try{ 35 list = Files.readAllLines(path,Charset.forName("SJIS")); 36 37 }catch( IOException e ) { 38 e.printStackTrace(); 39 System.out.println("ファイルパスが正しくありません"); 40 return; 41 } 42 43 /*データ抽出呼び出し*/ 44 List<String> ProductList = rac.ExtractProduct(list); 45 List<Integer> PriceList = rac.ExtractPrice(list); 46 47 /*商品名と価格画面表示*/ 48 for(int count = 0; count<PriceList.size();count++){ 49 System.out.print(ProductList.get(count) + "は"); 50 System.out.println(PriceList.get(count)+ "円"); 51 } 52 53 int total = test.totalAmount(PriceList); 54 55 } 56 57 List<String> ExtractProduct(List<String> list){ 58 List<String> ProductList = new ArrayList<String>(); 59 //商品名抽出 60 for (int i = 0; i<list.size(); i++){ 61 String regexProductName = "\\[(.+)\\]"; 62 Pattern patternProduct = Pattern.compile(regexProductName); 63 Matcher matchProduct = patternProduct.matcher(list.get(i)); 64 if(matchProduct.find()){ 65 ProductList.add(matchProduct.group(0)); 66 //抽出不可 67 }else{ 68 ProductList.add(null); 69 } 70 } 71 return ProductList; 72 73 } 74 List<Integer> ExtractPrice (List<String> list){ 75 List<Integer> PriceList = new ArrayList<Integer>(); 76 //価格抽出 77 for (int j = 0; j<list.size(); j++){ 78 String regexPriceName = "\\\\(\\d+)"; 79 Pattern pattrenPrice = Pattern.compile(regexPriceName); 80 Matcher matchPrice = pattrenPrice.matcher(list.get(j).toString()); 81 if(matchPrice.find()){ 82 PriceList.add(Integer.parseInt(matchPrice.group(1))); 83 //抽出不可 84 }else{ 85 PriceList.add(0); 86 } 87 88 89 } 90 return PriceList; 91 } 92 93 /*合計金額算出処理*/ 94 int totalAmount(List<Integer> PriceList){ 95 int total =0; 96 for(int k =0; k<PriceList.size(); k++){ 97 total += PriceList.get(k); 98 } 99 System.out.println("合計金額は"+total+"円です"); 100 101 return total; 102 103 104 } 105}
###補足情報(言語/FW/ツール等のバージョンなど)
言語:Java 環境:eclipse Neon
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/06/08 04:44