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

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

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

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

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

Q&A

4回答

6307閲覧

テキストファイルを読み込んで、指定した行を抽出したいです

zawazawa

総合スコア20

Java

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

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

0グッド

0クリップ

投稿2016/08/14 05:35

JAVA

1package java11; 2 3import java.io.File; 4import java.io.FileReader; 5import java.io.BufferedReader; 6import java.io.FileNotFoundException; 7import java.io.IOException; 8import java.text.DateFormat; 9 10public class J_11_2 { 11 public static void main (String args[]) throws ParseException { 12 try { 13 File file = new File("//テキストファイル"); 14 if (checkBeforeReadfile(file)) { 15 BufferedReader br = new BufferedReader(new FileReader(file)); 16 String str; 17 while ((str = br.readLine()) != null) { 18 int aindex = str.indexOf("会社名") + "会社名".lenght(); 19 int bindex = str.indexOf("設立日"); 20 if (aindex >=0) { 21 if (bindex >=0) { 22 String kaisha = str.substring(aindex,bindex); 23 System.out.println("会社名" + kaisha); 24 } 25 } 26 int cindex = str.indexOf("代表取締役") + "代表取締役".lenght(); 27 int dindex = str.indexOf("会社名"); 28 if (cindex >=0) { 29 if (dindex >=0) { 30 String daihyou = str.substring(cindex,dindex); 31 System.out.println("代表取締役:" + daihyou); 32 } 33 } 34 int eindex = str.indexOf("設立日") + "設立日:".lenght(); 35 int findex = str.indexOf("資本金"); 36 if (eindex >=0) { 37 if (findex >=0){ 38 String date = str.substring(eindex,findex); 39 Local local = new Local("ja","JP","JP"); 40 DateFormat japaneseFormat = new SimpleDateFormat("GGGGy年M月d日",local); 41 42 Date datestr = japaneseFormat.parse(date); 43 System.out.println("設立日:" + datestr); 44 }

閲覧ありがとうございます。

やりたいことは、
・テキストファイルを読み込ませる。
・1行ずつ読み込んで、指定した行を抽出させ、コンソール画面に出力させる。
・和暦変換。
・会社データクラスを作成。
・2つ以上の関数を使用。

以上の5つです。

出力方法は
会社名:xxxxx
代表取締役:xxxxx
設立日:平成xx年xx月xx日

です。

エラーの原因を調べながら2週間取り組んでいましたが、
正常に起動できませんでした。
もう何をしていいのかわかりません。
ご教授をいただきたいです。

よろしくお願い致します。

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

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

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

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

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

A-pZ

2016/08/14 08:29

読み込むテキストファイルの記載も必要ではないかと思います。
swordone

2016/08/14 08:36

何のエラーが出ているのですか?
guest

回答4

0

java

1 2import java.io.File; 3import java.io.FileReader; 4import java.io.BufferedReader; 5import java.io.FileNotFoundException; 6import java.io.IOException; 7import java.text.*; 8import java.io.*; 9import java.util.Date; 10 11 12import java.text.SimpleDateFormat; 13import java.util.Locale; 14import java.util.Calendar; 15public class J { 16 public static void main (String args[]){ 17 try { 18 File file = new File("ttt777.txt"); 19 if (checkBeforeReadfile(file)) { 20 BufferedReader br = new BufferedReader(new FileReader(file)); 21 String str; 22 while ((str = br.readLine()) != null) { 23 int aindex = str.indexOf("会社名:") + "会社名:".length(); 24 int bindex = str.indexOf("設立日:"); 25 if (aindex >=0) { 26 if (bindex >=0) { 27 String kaisha = str.substring(aindex,bindex); 28 System.out.println("会社名:" + kaisha); 29 } 30 } 31 int cindex = str.indexOf("代表取締役:") + "代表取締役:".length(); 32 int dindex = str.indexOf("会社名:"); 33 if (cindex >=0) { 34 if (dindex >=0) { 35 String daihyou = str.substring(cindex,dindex); 36 System.out.println("代表取締役:" + daihyou); 37 } 38 } 39 int eindex = str.indexOf("設立日:") + "設立日:".length(); 40 int findex = str.indexOf("資本金:"); 41 if (eindex >=0) { 42 if (findex >=0){ 43 String date = str.substring(eindex,findex); 44 45 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); 46 Date formatDate = sdf.parse(date); 47 Locale locale = new Locale("ja","JP","JP"); 48 49 SimpleDateFormat sd = new SimpleDateFormat("GGGGy年M月d日",locale); 50 String dateStr = sd.format(formatDate); 51 System.out.println("設立日:"+dateStr); 52 } 53 54 } 55 System.out.println(); 56 57 58 } 59 60 61 62 } 63 }catch(Exception e){ 64 65 66} 67 68} 69 70 71private static boolean checkBeforeReadfile(File file) 72 { if (file.exists()) 73 { if (file.isFile() && file.canRead()) 74 { return true; } 75 } 76 return false; 77 } 78 79 80} 81

投稿2016/10/30 23:59

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

プログラム内で読み込む7.txtの内容
代表取締役:tera田 tail美会社名:teratail設立日:2012年7月20日資本金:1000円
aaaaaaaaaaaaaaaa
代表取締役:tera国 tail木会社名:teratail2設立日:2015年2月2日資本金:100円

こういう感じの内容のファイルがあると一行ずつ抽出して
会社名:teratail
代表取締役:tera田 tail美
設立日:平成24年7月20日

会社名:teratail2
代表取締役:tera国 tail木
設立日:平成27年2月2日

こんな風に意味のある行を変換して表示するプログラムです 作ろうとしたものと同じかどうかは 分かりませんが

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.;
import java.io.
;
import java.util.Date;

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Calendar;
public class J {
public static void main (String args[]){
try {
File file = new File("7.txt");
if (checkBeforeReadfile(file)) {
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
while ((str = br.readLine()) != null) {
int aindex = str.indexOf("会社名:") + "会社名:".length();
int bindex = str.indexOf("設立日:");
if (aindex >=0) {
if (bindex >=0) {
String kaisha = str.substring(aindex,bindex);
System.out.println("会社名:" + kaisha);
}
}
int cindex = str.indexOf("代表取締役:") + "代表取締役:".length();
int dindex = str.indexOf("会社名:");
if (cindex >=0) {
if (dindex >=0) {
String daihyou = str.substring(cindex,dindex);
System.out.println("代表取締役:" + daihyou);
}
}
int eindex = str.indexOf("設立日:") + "設立日:".length();
int findex = str.indexOf("資本金:");
if (eindex >=0) {
if (findex >=0){
String date = str.substring(eindex,findex);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); Date formatDate = sdf.parse(date); Locale locale = new Locale("ja","JP","JP"); SimpleDateFormat sd = new SimpleDateFormat("GGGGy年M月d日",locale); String dateStr = sd.format(formatDate); System.out.println("設立日:"+dateStr); } } System.out.println(); } } }catch(Exception e){

}

}

private static boolean checkBeforeReadfile(File file)
{ if (file.exists())
{ if (file.isFile() && file.canRead())
{ return true; }
}
return false;
}

}

投稿2016/10/30 23:48

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

プログラム内で読み込む7.txtの内容
代表取締役:tera田 tail美会社名:teratail設立日:2012年7月20日資本金:1000円
aaaaaaaaaaaaaaaa
代表取締役:tera国 tail木会社名:teratail2設立日:2015年2月2日資本金:100円

こういう感じの内容のファイルがあると一行ずつ抽出して
会社名:teratail
代表取締役:tera田 tail美
設立日:平成24年7月20日

会社名:teratail2
代表取締役:tera国 tail木
設立日:平成27年2月2日

こんな風に意味のある行を変換して表示するプログラムです 作ろうとしたものと同じかどうかは 分かりませんが

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.;
import java.io.
;
import java.util.Date;

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Calendar;
public class J {
public static void main (String args[]){
try {
File file = new File("7.txt");
if (checkBeforeReadfile(file)) {
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
while ((str = br.readLine()) != null) {
int aindex = str.indexOf("会社名:") + "会社名:".length();
int bindex = str.indexOf("設立日:");
if (aindex >=0) {
if (bindex >=0) {
String kaisha = str.substring(aindex,bindex);
System.out.println("会社名:" + kaisha);
}
}
int cindex = str.indexOf("代表取締役:") + "代表取締役:".length();
int dindex = str.indexOf("会社名:");
if (cindex >=0) {
if (dindex >=0) {
String daihyou = str.substring(cindex,dindex);
System.out.println("代表取締役:" + daihyou);
}
}
int eindex = str.indexOf("設立日:") + "設立日:".length();
int findex = str.indexOf("資本金:");
if (eindex >=0) {
if (findex >=0){
String date = str.substring(eindex,findex);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); Date formatDate = sdf.parse(date); Locale locale = new Locale("ja","JP","JP"); SimpleDateFormat sd = new SimpleDateFormat("GGGGy年M月d日",locale); String dateStr = sd.format(formatDate); System.out.println("設立日:"+dateStr); } } System.out.println(); } } }catch(Exception e){

}

}

private static boolean checkBeforeReadfile(File file)
{ if (file.exists())
{ if (file.isFile() && file.canRead())
{ return true; }
}
return false;
}

}

投稿2016/10/30 23:47

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

エラー内容が不明なので、ソースをざっと見る限りでコメントします。

.lenght()とありますが、.length()が正しいですね。

投稿2016/08/16 03:41

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問