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

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

新規登録して質問してみよう
ただいま回答率
85.48%
ファイル

ファイルとは、文字列に基づいた名前又はパスからアクセスすることができる、任意の情報のブロック又は情報を格納するためのリソースです。

Java

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

Q&A

解決済

1回答

560閲覧

[Java]ファイルを使った簡単な問題に関する質問です。

ChieSuzuki

総合スコア5

ファイル

ファイルとは、文字列に基づいた名前又はパスからアクセスすることができる、任意の情報のブロック又は情報を格納するためのリソースです。

Java

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

0グッド

0クリップ

投稿2020/09/26 03:15

編集2020/09/26 04:18

前提・実現したいこと

ファイルから数字を引用して各桁の数字を足していくプログラムを作っています。

発生している問題・エラーメッセージ

例外java.io.FileNotFoundExceptionは報告されません。スローするには、捕捉または宣言する必要があります  >>fileInputのシンボルが見つけられません

該当のソースコード

Java

1import java.util.Scanner; 2import java.io.*; 3public class Lab4_3 { 4 5 public static void main (String[] args){ 6 //declair the valiables 7 int numberInput; 8 int integerInput; 9 int digitSum = 0; 10 int counter = 0; 11 String fileName = ""; 12 13 //get an input 14 Scanner input = new Scanner(System.in); 15 System.out.println("How many numbers in input"); 16 numberInput = input.nextInt(); 17 18 //see if the input is positive or negative 19 while(numberInput - 1 >= counter){ 20 21 File openFile = new File(fileName); 22 23 // it checks if the file exists 24 if (openFile.exists()) { 25 26 // it will then crate a scanner object for the file 27 Scanner fileInput = new Scanner(openFile); 28 29 // it then will check if there is no integers in the file 30 if (!fileInput.hasNextInt()) { 31 // it will give an error if there is no error 32 System.out.println("The file doesn't contain any integers. Exit program!"); 33 } 34 35 else { 36 // it will then loop through the integers until there is no more integers 37 // anymore 38 while (fileInput.hasNextInt()) { 39 40 // it will get the next integer from the file 41 int lineInput = fileInput.nextInt(); 42 43 if (lineInput <= 0){ 44 System.out.println("It is a negative integer."); 45 } 46 47 //if positive, stat a loop and get the sum of the gidits 48 else{ 49 while (lineInput != 0){ 50 51 } 52 digitSum += lineInput % 10; 53 lineInput = lineInput / 10; 54 } 55 56 System.out.println("The sum of the digits is " + digitSum); 57 } 58 59 counter++; 60 } 61 62 } 63 } 64 } 65 66} 67------------------------------------ 68import java.util.Scanner; 69import java.io.*; 70public class Lab4_3 { 71 72 public static void main (String[] args){ 73 //declair the valiables 74 int numberInput; 75 int integerInput; 76 int digitSum = 0; 77 int counter = 0; 78 String fileName = ""; 79 80 //get an input 81 Scanner input = new Scanner(System.in); 82 System.out.println("How many numbers in input"); 83 numberInput = input.nextInt(); 84 85 //see if the input is positive or negative 86 while(numberInput - 1 >= counter){ 87 88 try{ 89 File openFile = new File(fileName); 90 91 // it checks if the file exists 92 if (openFile.exists()) { 93 94 // it will then crate a scanner object for the file 95 Scanner fileInput = new Scanner(openFile); 96 } 97 } 98 99 catch (IOException e) { 100 System.out.println("IOExceptionが発生"); 101 } 102 103 // it then will check if there is no integers in the file 104 if (!fileInput.hasNextInt()) { 105 // it will give an error if there is no error 106 System.out.println("The file doesn't contain any integers. Exit program!"); 107 } 108 109 else { 110 // it will then loop through the integers until there is no more integers 111 // anymore 112 while (fileInput.hasNextInt()) { 113 114 // it will get the next integer from the file 115 int lineInput = fileInput.nextInt(); 116 117 if (lineInput <= 0){ 118 System.out.println("It is a negative integer."); 119 } 120 121 //if positive, stat a loop and get the sum of the gidits 122 else{ 123 while (lineInput != 0){ 124 125 } 126 digitSum += lineInput % 10; 127 lineInput = lineInput / 10; 128 } 129 130 System.out.println("The sum of the digits is " + digitSum); 131 } 132 133 counter++; 134 } 135 136 137 138 } 139 } 140 141}

試したこと

IOExceptionを入れようとしましたが、書き方だ正しくないのか、さらにエラーが出てしましました。

追記としてIOExeptionを入れたものです。

fileInputのシンボルが見つけられません というエラーが出ています。

補足情報(FW/ツールのバージョンなど)

NetBeanの12.0バージョンを使っています。

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

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

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

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

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

maisumakun

2020/09/26 03:27

> IOExceptionを入れようとしましたが、書き方だ正しくないのか、さらにエラーが出てしましました。 そちらのバージョンのコードも書いてください。
ChieSuzuki

2020/09/26 04:29

import java.util.Scanner; import java.io.*; public class Lab4_3 { public static void main (String[] args){ //declair the valiables int numberInput; int integerInput; int digitSum = 0; int counter = 0; String fileName = ""; //get an input Scanner input = new Scanner(System.in); System.out.println("How many numbers in input"); numberInput = input.nextInt(); //see if the input is positive or negative while(numberInput - 1 >= counter){ try{ File openFile = new File(fileName); // it checks if the file exists if (openFile.exists()) { // it will then crate a scanner object for the file Scanner fileInput = new Scanner(openFile); // it then will check if there is no integers in the file if (!fileInput.hasNextInt()) { // it will give an error if there is no error System.out.println("The file doesn't contain any integers. Exit program!"); } else { // it will then loop through the integers until there is no more integers // anymore while (fileInput.hasNextInt()) { // it will get the next integer from the file int lineInput = fileInput.nextInt(); if (lineInput <= 0){ System.out.println("It is a negative integer."); } //if positive, stat a loop and get the sum of the gidits else{ while (lineInput != 0){ } digitSum += lineInput % 10; lineInput = lineInput / 10; } System.out.println("The sum of the digits is " + digitSum); } counter++; } } } catch (IOException e) { System.out.println("IOExceptionが発生"); } } } } 正しいのかわかりませんが、このようにコードのオーダーを変えるとエラーが消えました!
guest

回答1

0

ベストアンサー

エラーメッセージのとおりです。Fileを受け取るScannerのコンストラクタは、当該のファイルが存在しなかった場合にFileNotFoundExceptionを投げます。これについてthrowsを宣言するかcatchするかの必要があります。

いちおうopenFile.exists()でのチェックは行っているようですが、運悪くチェックしてからScannerに投げるまでのタイミングでファイルが削除されると、実際にファイルが存在しない自体に遭遇してしまいます。

投稿2020/09/26 03:26

maisumakun

総合スコア145184

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

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

ChieSuzuki

2020/09/26 03:32

具体的にどのようにすればいいか教えていただけないでしょうか??
maisumakun

2020/09/26 03:38

「IOExceptionを入れようとし」たコードと、それでどのようなエラーが出たのかもご提示いただけないでしょうか?
ChieSuzuki

2020/09/26 04:18

遅くなりました。更新いたしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問