• ベストアンサー

javaプログラミングの質問です。

ProKaseifuの回答

回答No.2

ctrl+alt+"O"を押してインポート文の編成してみるといいですよ。

関連するQ&A

  • サーバー上(外部)のテキストの読み込み

    以下のようなtest.txtから二次元配列を読み込むプログラムを 今度はクライアントがapplet上からサーバーのappletプログラムがあるフォルダにあるtest.txtからデータを読み込むようにしたいのですが どうしてもセキュリティ上のエラーが出てしまいます。 画像などはgetImageでうまくいくのですが・・・ BufferedReader reader = null; String strArray[][] = new String[5][2]; String fileName = "test.txt"; int i = 0; int j = 0; try { reader = new BufferedReader ( new FileReader(fileName) ); String line; while ( (line = reader.readLine() ) != null ) { StringTokenizer st = new StringTokenizer (line ); while ( st.hasMoreTokens() ) { strArray[i][j] = st.nextToken(); j++; } j = 0;// 横のindexをリセット i++;// 縦のindexを次に } } catch ( Exception e ) { System.out.println ( e.toString() ); } ---------test.txt(例) 200 200 30 60 301 33 11 66 100 390 自分でも調べていくつもりですが期限があるためもしご存知の方がいましたら是非ご教授よろしくお願いいたします。

    • ベストアンサー
    • Java
  • 配列の値の参照について

    下記のプログラムの-------------で囲まれた部分が、コンパイルは通るのですが実行すると"value[1][0]"以降が参照できません(ここではcount=3 となっています)。 for文を使わずに"System.out.println(value[1][0])"とすると値を取り出せるのですが…。 実行時のエラーは Exception in thread "main" java.lang.ArrayIndexOutOfBoundException: 3 です。 ソースコードをそのまま載せて長文になってしまい申し訳ありません。 プログラム初心者なので、些細なことでもいいので回答お願いします。 import java.util.*; import java.io.*; public class Search { static String dataFileName = "CityData.txt"; ArrayList<String> array = new ArrayList<String>(); GetDataCount gdc = new GetDataCount(dataFileName); int count = gdc.getCount(); int count2 = 0; String[][] value = new String[count][3]; public Search(String dataFileName){ try{ BufferedReader reader = new BufferedReader( new FileReader(dataFileName) ); String line = ""; while((line = reader.readLine()) != null){ Vector<String> variable = readLine(line); System.out.println(variable); count2++; } }catch(IOException e){ System.out.println(e); } //---------------------------------------------------------- for (int i=0; i<count2; i++){ for (int j=0; i<3; j++){ System.out.println(value[i][j]); } } //---------------------------------------------------------- } public Vector<String> readLine( String line ){ StringTokenizer st = new StringTokenizer( line, "," ); Vector<String> variable = new Vector<String>(); for( int i=0 ; i<3 ; i++ ){ String data = st.nextToken(); variable.addElement( data ); value[count2][i] = data; System.out.println(value[count2][i]); } return variable; } public class GetDataCount{ int count = 0; public GetDataCount(String dataFileName){ try{ BufferedReader reader = new BufferedReader( new FileReader(dataFileName) ); String line = ""; while((line = reader.readLine()) != null){ count++; } }catch(IOException e){ System.out.println(e); } } public int getCount(){ return count; } } public static void main(String[] args){ new Search(dataFileName); } }

  • javaのプログラミングについての質問です

    javaのプログラミングについての質問です 任意の複数の数値を入れたファイルを読み取りその数値の場所から次の数値まで線を描画するプログラミングを作成しました ここから発展問題としてタートルグラフィックスを 描画するプログラムを50歩進み右に90度曲がることを4度繰り返すという条件で作ってみようと思ったのですが上手くコンパイルが通りません 取りあえず分かっているのは (100.cos45,100.sin45)で90°曲がる repeat 4,forward 50right 90 end 0 だけです もし分かる方がいらっしゃいましたらご返答宜しくお願いします import java.awt.*; import java.awt.event.*; import java.io.*; public class mondai{ int x[] = new int[100]; int y[] = new int[100]; int len = 0; public static void main(String[] args){ new mondai(args[0]); } public mondai(String mondai2){ ReadFile rf = new ReadFile(mondai2); String line; while( (line = rf.gets()) != null) { String[] date = line.split(","); if(date.length == 2){ x[len] = Integer.parseInt(date[0]); System.out.println(date[0]); y[len]= Integer.parseInt(date[1]); System.out.println(date[1]); len++; } } Frame f = new Frame(); f.setTitle("Line"); f.setSize(640,480); f.setLayout(null); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); }}); DrawArea da = new DrawArea(); da.setBounds(0,0,640,480); f.add(da); f.setVisible(true); } private class DrawArea extends Canvas{ public void paint(Graphics g){ for(int i=0;i<len-1;i++) g.drawLine(x[i],y[i],x[i+1],y[i+1]); } } private class ReadFile{ FileInputStream fis; InputStreamReader isr; BufferedReader br; public ReadFile(String filename){ try{ fis = new FileInputStream(filename); isr = new InputStreamReader(fis); br = new BufferedReader(isr); }catch(Exception e){ e.printStackTrace(); } } public String gets(){ String line = ""; try{ line = br.readLine(); }catch(Exception e){ e.printStackTrace(); } return line; } } }

  • javaのプログラミングについてです!

    2次元配列をつくり、文字列型で名前、身長、体重をキーボード入力していき、それを出力するプログラミングを作っているのですが、今の私ではどこが間違っているのかよくわかりません。 どこが間違っているのか教えていただけると嬉しいです。 import java.io.*; class kadai4 { public static void main(String[] args) throws IOException { //入力するための準備 BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); //新しい配列の準備 String[][] pro; pro=new String[3][3]; System.out.println("名前・身長・体重の順に入力してください"); //入力 for(i=0;i<3;i++){ for(j=0;j<3;j++){ pro[i][j] = new String(br.readLine); } } for(i=0;i<3;i++){ for(j=0;j<3;j++){ System.out.print(pro[i][j]); } } } }

  • ファイル読み込みで

    市販のテキストでファイルを読み込むプログラムを作ったのですがうまくできません。 import java.io.*; public class ShowFile{ public static void main(String args[]){ if(args.length != 1){ System.out.println("使用法: java ShowFile ファイル"); System.out.println("例: java ShowFile ShowFile.java"); System.exit(0); } String filename = args[0]; try{ BufferedReader reader = new BufferedReader(new FileReader(filename)); String line; while((line = reader.readLine()) != null){ System.out.println(line); } reader.close(); }catch(FileNotFoundException e){ System.out.println(filename + "が見つかりません"); }catch(IOException e){ System.out.println(e); } } } javac ShowFile.javaでコンパイルはうまくいったのですが、 実行時にjava ShowFile input.txtとするとException in thread "main" java.lang.NoClassDefFoundError: ShowFile というエラーが出ます。input.txtというファイルは存在します。 アドバイスをお願いします。

    • ベストアンサー
    • Java
  • Javaのカウント方法について

    お伺い致します。 CSVで取り込んだデータの抽出をしたいのですが、方法が見出せません。どの点を直せば宜しいのでしょうか。 具体的には取り込んだ郵便番号をカウントして(例:京都市,34)CSVファイルに出力するように出したいのですがカウントがうまくできません。(以下、コメントアウトしたものがありますが、今までのソースを記載します) 宜しく御願いします。 import java.io.FileReader; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.*; public class test01 { static String fname ="26KYOUTO.CSV"; public static void main(String[] args){ if(args.length>0) fname = args[0]; try { BufferedReader reader = new BufferedReader(new FileReader(fname)); BufferedWriter pw = new BufferedWriter(new PrintWriter("orig.txt")); String line = reader.readLine(); System.out.println(line); pw.println(""); int n = 0; int count = 0; _/* while(true) { String line = reader.readLine(); if(line.equals("26101")) break; count++; } */ reader.close(); System.out.println("京都府北区=" +count ); } catch(FileNotFoundException e) { System.out.println("ファイルがありません。"); } catch(IOException e) { System.out.println("入出力エラーです。"); } } }

    • ベストアンサー
    • Java
  • 「プログラミング」JAVAのエラーが解決できません。

    ホームページを見たり、メールチェックをしたり、数分間このプログラムを走らせると、 Address already in use: connect java や Connection refused: connect といったエラーメッセージが出ます。 どうすれば、解決するでしょうか? やさしいJAVAやインターネットで調べたのですが良い方法が分かりません。ぜひ、ご教示宜しくお願い致します。 import java.net.*; import java.io.*; import java.util.*; import java.text.*; class Test { public static void main(String[] args) throws Exception { String helloURL; for(int a=1; a<3000; a++) { helloURL = "http://shopping.yahoo.co.jp/"; Th th1 = new Th(helloURL); th1.start(); Thread.sleep(100); } } } class Th extends Thread { String helloURL = null; String line; Th(String str) { this.helloURL = str; } public void run() { try { URL url = new URL(helloURL); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),"JISAutoDetect")); while ((line = in.readLine()) != null) { try { OutputStream os = new FileOutputStream("log.txt",true); BufferedWriter fr = new BufferedWriter(new OutputStreamWriter(os)); if(line.indexOf("ヤフー") != -1) { fr.write(line); } if(line.indexOf("あ") != -1) { fr.write(line); } fr.close(); os.close(); } catch(Exception e){System.out.println("エラー1 : "+e.getMessage());} } in.close(); } catch(Exception ex){System.out.println("エラー2 : "+ex.getMessage());} } }

    • ベストアンサー
    • Java
  • Java言語のプログラムをC言語にする場合

    次の2つのJava言語のプログラムをC言語にしたいのですが、C言語でプログラムを書いたことがありません。C言語にする場合はどう書けばいいのでしょうか? import java.io.*; public class Sort { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("整数値を入力してください"); String line = reader.readLine(); int a = Integer.parseInt(line); String line2 = reader.readLine(); int b = Integer.parseInt(line2); String line3 = reader.readLine(); int c = Integer.parseInt(line3); String line4 = reader.readLine(); int d = Integer.parseInt(line4); int[] data = {a, b, c, d}; for (int i = 0; i< data.length - 1 ; i++) { for (int j = i + 1; j< data.length; j++) { if(data[i] > data[j]) { int e = data[i]; data[i] = data[j]; data[j] = e; } } } System.out.println("昇順に並べ替えると、"); for (int i = 0; i< data.length; i++) { System.out.print(data[i] + " "); } System.out.println("です。"); } catch (IOException e){ System.out.println(e); } catch (NumberFormatException e) { System.out.println("数式の形式が正しくありません。"); } } } import java.io.*; public class Yakusu { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("2つの整数値を入力してください"); System.out.print("整数A : "); String line = reader.readLine(); int a = Integer.parseInt(line); System.out.print("整数B : "); String line2 = reader.readLine(); int b = Integer.parseInt(line2); if(a%b == 0){ System.out.println("BはAの約数です"); } else { System.out.println("BはAの約数ではありません"); } } catch (IOException e){ System.out.println(e); } catch (NumberFormatException e) { System.out.println("数式の形式が正しくありません。"); } } }

  • StringTokenizerについて

    JAVAの勉強をしているのですが、StringTokenizerの役割がよく分かりません。検索サイトを使って色々調べてみたのですが、「トークンを区切る」だとかよく分からない言葉が出てきて全く理解できません。以下はあるデータから項目を抽出するプログラムらしいのですが、参考としてこの例でStringTokenizerがどのような役割を果たしているのか教えて下さい。複雑で説明しにくければStringTokenizerそのものの説明でも十分です。よろしくお願いします。 BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); String line=""; try { int i=0; while ((line = reader.readLine()) != null) { int fcount=1; StringTokenizer st = new StringTokenizer(line); while (st.hasMoreTokens()) { switch (fcount) { case 2: //項目2:被説明変数Y data[i][0]=Integer.parseInt(st.nextToken()); break; case 4: //項目4:説明変数X data[i][1]=Integer.parseInt(st.nextToken()); break; default: // その他の項目 String dummy=st.nextToken(); break; } fcount++; } i++; } NofData=i;

    • ベストアンサー
    • Java
  • javaのプログラミング(連続する整数の和)

    javaのプログラミング(連続する整数の和) 大学で出た課題です。 「ある自然数nを入力し、n=○+○+…+○と連続する整数の和で表すプログラミングをつくれ。」というものです。 以下に、私が考えたものを載せます。これだと連続する2つの整数の和では表すことができるのですが、3つ以上の整数の和を表すことができません。どうすればよいでしょうか? import java.io.*; public class  n { public static void main(String[] args) { int n; BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); try{ String line; System.out.print("整数:"); line = rd.readLine(); n = Integer.parseInt(line); } catch(IOException e){ System.out.println("入力エラーが発生しました。"); return; } catch(NumberFormatException e){ System.out.println("整数を入力してください。"); return; } for(int i=1;i<=n;i++){ int k=i+1; if(i+k==n) System.out.println(i+"+"+k); } } }