• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:Javaのコードのことで質問があります)

Javaで中置表記法を後置表記法に変換するプログラムを作成し、エラーが発生する

このQ&Aのポイント
  • Javaで作成した中置表記法を後置表記法に変換するプログラムがエラーを発生させて正常に実行できない状況です。
  • エラーメッセージは「Exception in thread 'main' java.lang.ArrayIndexOutOfBound...」ですが、エラーの具体的な原因がわかりません。
  • 31行目と150行目のコードを見てもエラーの原因が特定できません。質問者は助言を求めています。

質問者が選んだベストアンサー

  • ベストアンサー
  • tama_zou
  • ベストアンサー率57% (4/7)
回答No.2

敢えて答えを書きます。 main()メソッドの中の  stac stac1 = new stac(); だけでは、stacクラスのstac配列が初期化されないことが原因。 直後の行に次の1行を追加してみて。  stac1.initStack(); Java初心者のようですが、コードが汚過ぎて読む気が起きません。 他人に質問するときは、もう少しマシなコードにするよう心がけてください。

usausagina
質問者

お礼

解決しました どうもありがとうございます やっぱりコード汚いですよね^^; 次に質問することがあるときはもっとTabや改行をしっかり入れて見易くします どうも親切にありがとうございました

全文を見る
すると、全ての回答が全文表示されます。

その他の回答 (1)

回答No.1

ArrayIndexOutOfBoundException、です。 「配列の要素数を超えてアクセスした」場合に起きるエラーです。 at gotPriority.gotPriority(toPostfix.java:31) at toPostfix.main(toPostfix.java:150) は、要するに31行目でエラーが起きたと言うことです。 より詳しく言うと、31行目を含む gotPriority.gotPriority メソッドが、 toPostfix.main の150行目から呼ばれてエラーが起きた、という旨が書かれています。 ということで、 ・配列の範囲を超えるアクセスがあるか? ・あるとしたら、何が悪かったのか? を考えてみてください。 答えは敢えて言いません、なんか行数も少し違ってますから。コメント省略しました?

usausagina
質問者

お礼

はい 配列の内容に補足文をつけていたのですが文字数がオーバーしたのでそれらを省略しました

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • javaについて質問です。お願いします><

    javaについて質問です。お願いします>< シーザー暗号を実現するプログラムをじゃいたのですが、import java.io.*; class Prob6_3 { public static void main(String [] args)throws IOException { int key; //キー番号 String orgStr; //ターゲット文字列 String encStr; //暗号化文字列 String decStr; // 復号化文字列 String temp; BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.print("ターゲット文字列:"); orgStr=br.readLine(); System.out.print("キー番号:"); temp=br.readLine(); key=Integer.parseInt(temp); Cipher cip=new Cipher (); encStr=cip.encrypt(orgStr,key); decStr=cip.decrypt(encStr,key); System.out.println("[Original Code]"+orgStr); System.out.println("[Encrypted Code]"+encStr); System.out.println("[Decrypted Code]"+decStr); } } class Cipher { String encrypt(String str,int key) { String ret="";             //ココです1 for(int i=0;i<str.length();i++){ char c=str.charAt(i); c=(char)((int)c+key); ret+=c; } return ret; } String decrypt(String str,int key) { String ret="";              //ここです2 for(int i=0;i<str.length();i++){ char c=str.charAt(i); c=(char)((int)c-key); ret+=c; } return ret; } }   とこんな感じになり実行もできるのですが、class CipherのString encrypt(String str,int key)やString decrypt(String str,int key)のところで一つずつ文字をつなげて文字列にするにはStringBufferクラスのインスタンスを利用すると簡単だとききました。だけど記述方法がよくわからなく使用した場合のreturn文の書き方もイマイチわかりません><なのでできるだけ詳しく教えて頂けないでしょうか??お願いします。//ココですと書いてあるところです。お願いします><

    • ベストアンサー
    • Java
  • javaについて質問です。

    javaについて質問です。 シーザー暗号の暗号化と復号化のプログラムをつくりたいのですが... import java.io.*; class Prob6_2 { public static void main(String [] args)throws IOException { int key; //キー番号 String orgStr; //ターゲット文字列 String encStr; //暗号化文字列 String decStr; // 復号化文字列 String temp; BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.print("ターゲット文字列:"); orgStr=br.readLine(); System.out.print("キー番号:"); temp=br.readLine(); key=Integer.parseInt(temp); Cipher cip=new Cipher (); encStr=cip.encrypt(orgStr,key); decStr=cip.decrypt(encStr,key); System.out.println("[Original Code]"+orgStr); System.out.println("[Encrypted Code]"+encStr); System.out.println("[Decrypted Code]"+decStr); } } class Cipher { String encrypt(String str,int key) { for(int i=0;i<str.length();i++){ char c=str.charAt(i); c=(char)((int)c+key); /*この後どのように一つずつの文字をつなげて文字列にしたら良いか分かりません。StringBuffer クラスのインスタンス をつかうといいというヒントは問題集に書いてあるのですが....教えて下さい*/ } } String decrypt(String str,int key) { for(int i=0;i<str.length();i++){ char c=str.charAt(i); c=(char)((int)c-key); /*この後どのように一つずつの文字をつなげて文字列にしたら良いか分かりません。StringBuffer クラスのインスタンス をつかうといいというヒントは問題集に書いてあるのですが....教えて下さい*/ } } } class Cipherのところにコメントでも書いてあるのですが、一つずつの文字をつなげて文字列にしたら良いか分かりません。どのように実現したら良いのでしょうか?><教えてください>< 違っているところがあればそこも教えて頂けるとたすかります。 お願いします>< できたらStringBufferをつかったやり方を教えてください><」

    • ベストアンサー
    • Java
  • JAVA 引数の引数の取得について

    JAVA初心者です。的を得ていない書き込みをするかもしれませんが ご勘弁ください。今JUNITであるクラスのあるメソッド(MethodA)をチェックする テストプログラムを作成しております。 MethodA: public File MethodA(String name, File A) { int i = name.substring(0, i); String ret = name.substring(0, i + 1); return new File(A, ret); この場合に戻り値として返される引数の部分 「return new File(A, ret);」 の「A」と「ret」の値をJUNITで確認したいのですが、アイデアを 頂けますと大変幸いです。 また、戻り値の「return new File(A, ret);」に 「new」とついているのはなぜでしょうか?? 基本的な質問で大変申し訳ございませんが、よろしくお願いします。

    • ベストアンサー
    • Java
  • javaプログラムの作り方

    class HTMLparam extends IO_Data_D { String s; int max_lean; Applet a; HTMLparam(int _max_lean, String _s, Applet _a) { max_lean = _max_lean; s = _s ; a = _a;} //-------------------------------------- Object getParam() { String param; char separated[][]; for(int i=0; i<max_lean; i++) { param = a.getParameter( s + (i+1) ); for(int j=0; j<max_lean; j++) { if(param != null ){ separated[][] = new char [max_lean][ param.length() ]; ???????????????????????????; } } return ; } } すごい初歩的な質問で申し訳ありません。いまいちキャストの仕方がよくわかっていません。質問というのは、param = a.getParameter( s + (i+1) );でparamに文字列が入ります。max_leanの行数文の文字列を文字配列にして文字を管理し、オブジェクトでリターンしたいのですが、どのように作っていいのかわかりません。教えていただけないでしょうか?

  • java

    Base64にエンコードしたものをデコードするプログラムです。(汎用性が低いのは仕様です)コンパイルは通ったのですが、実行したら以下のエラーが出てきました。 C:\Users\Owner\Documents\javadev>java Base64Decode2 hello.dat hello2.txt java.lang.ArrayIndexOutOfBoundsException: 97 at Base64Decode2.decode(Base64Decode2.java:51) at Base64Decode2.main(Base64Decode2.java:23) 指定の行を見ても原因がよく分かりません。とても初歩的な質問なのかもしれませんが、お願いします。 以下がプログラムコードです import java.io.*; public class Base64Decode2 { public static void main(String[] args) { // 変換テーブル char[] table = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; InputStream in = null; // 入力データ OutputStream out = null; // 出力先 try { in = new FileInputStream(args[0]); out = new FileOutputStream(args[1]); char[] cs; while ((cs=read4(in)) != null) { int[] buf = decode(cs, table);//ここが問題? for (int i=0; i<buf.length; i++) { System.out.print(buf[i]+", "); } System.out.println(); int[] buf2 = convert6to8(buf); write3(out, buf2); } } catch (Exception e) { e.printStackTrace(); // 例外の情報を表示する } finally { // in, out を閉じる try { in.close(); out.close(); } catch (Exception e) { } } } /** * 8ビットの2進数の列を復号化する. * @param cs * @param table 符号テーブル * @return */ public static int[] decode(char[] cs, char[] table) { int[] buf = new int[cs.length]; for (int i=0; i<buf.length; i++) { buf[i] = table[cs[i]];//ここが問題? } return buf; } public static int[] convert6to8(int[] buf) { String b; int[] buf2; if (buf.length == 2) { b = toBinary(buf[0], 6); buf2 = new int[1]; buf2[0] = fromBinary(b.substring(0, 8)); } else if (buf.length == 3) { b = toBinary(buf[0], 6) + toBinary(buf[1], 6); buf2 = new int[2]; buf2[0] = fromBinary(b.substring(0, 8)); buf2[1] = fromBinary(b.substring(8, 16)); } else { b = toBinary(buf[0], 6) + toBinary(buf[1], 6) + toBinary(buf[2], 6); buf2 = new int[3]; buf2[0] = fromBinary(b.substring(0, 8)); buf2[1] = fromBinary(b.substring(8, 16)); buf2[2] = fromBinary(b.substring(16, 24)); } return buf2; } /** * バイト列 bt の数を順に出力する. * @param bt 数の配列。長さは 3以下. 各数は8ビットの整数 */ public static void write3(OutputStream out, int[] bt) throws IOException { for (int i=0; i<3; i++) { if (i<bt.length) { out.write(bt[i]); } } } /** * in から文字を最大4つ読み出す. * @param in 入力ストリーム * @return 文字の配列。配列長は最大4. 入力終了したときには null を返す. */ public static char[] read4(InputStream in) throws IOException { char[] bs; int n0=in.read(); int n1=in.read(); int n2=in.read(); int n3=in.read(); if (n0 < 0) { // 読み込み終了 bs = null; } else if (n2 < 0 || (char) n2=='=') { bs = new char[2]; bs[0] = (char) n0; bs[1] = (char) n1; } else if (n3 < 0 || (char) n3=='=') { bs = new char[3]; bs[0] = (char) n0; bs[1] = (char) n1; bs[2] = (char) n2; } else { bs = new char[4]; bs[0] = (char) n0; bs[1] = (char) n1; bs[2] = (char) n2; bs[3] = (char) n3; } return bs; } /** * 数を読み取って、nビットの2進数を表す文字列に変換する * @param bt 1バイトの数 * @param n 2進数のビット数 * @return 2進数を表す文字列 */ public static String toBinary(int bt, int n) { String s = Integer.toBinaryString(bt); for (int i=s.length(); i<n; i++) { s = "0" + s; } return s; } /** * 2進数を表す文字列を数に変換する * @param b 2進数を表す文字列 * @return b が表す数 */ public static int fromBinary(String b) { return Integer.parseInt(b, 2); } }

    • ベストアンサー
    • Java
  • STLについて

    VC++6を使っています。ベクタを戻り値とするプログラムを書いています。効率が悪く感じるのですが、STL?ではこういうやり方は正しいのでしょうか? また、一般的にSTLで引数や戻り値を扱う場合、どのようなタイプ(string?)を使えば、効率よく、きれいなプログラムが書けるのでしょうか? class A{ ... } vector<A> test(void){ vector<A> ret; for (int i = 0; i < 1000; i++){ ret.push_back( A(i) ); } return ret; } vector<A> a = test(); // 巨大なコンテナが返され、aにコピーされる? ※基本的に戻り値の仕組みが理解不足です。 char *の場合は、char配列のポインタが返され、新たな変数にポインタ値がコピーされるという解釈で結構ですか? char * sample(void){ char *p = new [1000]; return p; } char *q = sample();

  • javaというか文字列について少し質問です><

    javaというか文字列について少し質問です>< javaというか文字列について少し質問です>< お願いします。 シーザー暗号で文字列をずらすためのクラスのメソッドをつくったのですが・・・ String decrypt(String str,int key) { StringBuffer sb = new StringBuffer(); // もしくはStringBuilder for(int i=0;i<str.length();i++){ char c=str.charAt(i); c=(char)((int)c-key); sb.append( c ); } こんな感じなのですがアルファベットの小文字26文字のみとしたいので暗号化や復号化の際に越えてしまう場合はアルファベット内でループさせたいのですがいまいちやり方が浮かびません><越えてしまう場合はzからひいたものを表現させれば良いかと思いやってみたのですが全然違う文字が出てきてしまいました。何かいい方法を教えてください。お願いします><

    • ベストアンサー
    • Java
  • javaのlengthに対して質問です

    javaのlengthに対して質問です public class Gauss { public static void main(String[] args){ int[] ia = new int[101]; for (int i = 0 ; i<ia.length;i++); } } 今の場合 ia.lengthはどれくらいの長さですか? ia だから2?

    • ベストアンサー
    • Java
  • Java Applet での虫食い算での質問

    初めて利用させていただきます。 今、課題で虫食い算のプログラムを作成しているのですが、うまく動作してくれません。 もしよろしければご指摘をしてもらえればと思い書き込みのほうをさせていただきます。 import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Event; import java.awt.Font; import java.awt.Graphics; import java.awt.Panel; import java.applet.Applet; public class musikuizan_1 extends Applet { int A, B; int cnt; int sub(int n,int m) { int i; for (i=1; i<m; i++) n /= 10; return n % 10; } void puzzle() { int a; int b; int c; int d; int e; for (a=1; a<=99; a++) { for (b=1; b<=99; b++) { c = a * (b % 10); d = a * (b / 10); e = a * b; if (c>=100 && c<=999 && d>=10 && d<=99 && e>=100 && e<=999) /*桁数調整*/ { if (sub(b,1)==3 && sub(c,2)==1 && sub(d,2)==8) /*穴埋めされてある部分の数*/ { A = a; B = b; cnt++; } } } } } public void init() { setBackground(new Color(150,180,200)); setLayout(new BorderLayout()); Panel psouth = new Panel(); psouth.add(new Button("実行")); add("South", psouth); } public void paint(Graphics g) { int i, c, d, e; for (i=1; i<=2; i++) { g.drawRect(97-i*20, 5,16,16); g.drawRect(97-i*20,25,16,16); g.drawRect(77-i*20,65,16,16); } for (i=1; i<=3; i++) { g.drawRect(97-i*20,45,16,16); g.drawRect(97-i*20,85,16,16); } for (i=1; i<=4; i++) g.drawLine(30,43,100,43); g.drawLine(30,83,100,83); g.setFont(new Font("Helvetica",Font.BOLD,18)); g.drawString("x",40, 40); if (A == 0) { g.drawString("3",60, 40); g.drawString("1",60, 60); g.drawString("8",60, 80); g.setFont(new Font("Helvetica",Font.PLAIN,12)); g.drawString("cnt = ",30,115); } else { c = A * (B % 10); d = A * (B / 10); e = A * B; g.setFont(new Font("Helvetica",Font.BOLD,18)); for (i=1; i<=2; i++) { g.drawString(Integer.toString(sub(A,i)),100-i*20, 20); g.drawString(Integer.toString(sub(B,i)),100-i*20, 40); } for (i=1; i<=3; i++) { g.drawString(Integer.toString(sub(c,i)),100-i*20, 60); g.drawString(Integer.toString(sub(d,i)), 80-i*20, 80); } for (i=1; i<=4; i++) g.drawString(Integer.toString(sub(e,i)),100-i*20,100); g.setFont(new Font("Helvetica",Font.PLAIN,12)); g.drawString("cnt = "+Integer.toString(cnt),30,115); } } public boolean action(Event e, Object arg) { if ("実行".equals(arg)) { if (A == 0) puzzle(); else A = cnt = 0; repaint(); } return true; } }

    • ベストアンサー
    • Java
  • Javaのオーバーロードを使った問題

    class Book{ String title, size; int price; Book(String title, String size, int price){ this.title = title; this.size = size; this.price = price; } public String info(){ return title + " " + size; } public String info(String title){ return title + " " + size + " " + price +"円"; } public int info(String title, String size){ return price; } } ________________________________________________________ class Book_test{ public static void main(String[] args){ String[] title = {"図鑑","参考書","雑誌","地図"}; String[] size = {"B4", "A5", "A4", "A3"}; int[] price={4500, 1800, 600, 1400}; Book[] b = new Book[4]; for(int i=0; i<b.length; i++){ b[i] = ****; } for(int i=0; i<b.length; i++){ System.out.println(***); } for(int i=0; i<b.length; i++){ System.out.println(***); } for(int i=0; i<b.length; i++){ System.out.println(***); } } } 実行結果は、以下の通り 図鑑 B4 参考書 A5 雑誌 A4 地図 A3 図鑑 B4 4500円 参考書 A5 1800円 雑誌 A4 600円 地図 A3 1400円 4500 1800 600 1400 このように表示するために、javaのソースコードを書かなくてはならないのですが、***の部分に何を入れれば良いのか分かりません。 特に、for文の部分です。 classが苦手なので分かりやすく教えていただけると幸いです。

    • ベストアンサー
    • Java