チェイスR
オリジナルポスター- 2008年5月8日
- PDX
- 2009年6月2日
別の見積もりを計算する(Y / N):スレッドの例外 'main' java.lang.StringIndexOutOfBoundsException:文字列インデックスが範囲外です:0
java.lang.String.charAt(String.java:558)で
Test1.main(Test1.java:73)で
なぜこれをしているのですか!!!
ちなみにプログラムは次のとおりです。
コード:
public class Test1 { public static void main( String[] args ) { String customer = ' '; String customerUC = ' '; String type1 = ' '; String type2 = ' '; char type = ' '; String another1 = ' '; char another = 'y'; int fence = 0; int count = 0; double costFoot = 0; double costTotal = 0; double costGrand = 0; Scanner read = new Scanner(System.in); System.out.print( '
----------SIERRA FENCING COMPANY----------' ); while ( Character.toUpperCase(another) == 'Y' ) { System.out.print( '
Your Name: ' ); customer = read.nextLine(); customerUC = customer.toUpperCase(); System.out.print( '
New Installation (N) or Repair (R): ' ); type1 = read.nextLine(); type2 = type1.toUpperCase(); type = type2.charAt(0); System.out.print( '
Amount of Fencing Needed (linear feet): ' ); fence = read.nextInt(); System.out.print( '
Cost-Per-Foot of Fencing: ' ); costFoot = read.nextDouble(); if ( costFoot > 6 && type == 'N' ) { costFoot = 6; } else if ( costFoot > 5 && type == 'R' ) { costFoot = 5; } costTotal = fence * costFoot; costGrand += costTotal; count++; System.out.print( '
-------------------------------------------------------' ); System.out.print( '
Name: ' + customerUC ); System.out.print( '
Type: ' + type ); System.out.print( '
Linear Fencing: ' + fence + ' feet' ); System.out.print( '
Cost-Per-Foot: $' + costFoot ); System.out.print( '
Total: $' + costTotal ); System.out.print( '
-------------------------------------------------------' ); System.out.print( '
Calculate Another Estimate (Y/N): ' ); another1 = read.nextLine(); another = another1.charAt(0); } System.out.print( '
Grand Total: $' + costGrand ); System.out.print( '
You made ' + count + ' estimates' ); } }
Littleodie914
- 2004年6月9日
- ニューヨーク州ロチェスター
- 2009年6月2日
編集: nextLine()メソッドが現在の行を超えてポインタを進めることと関係があるようです。私自身はそれをあまり理解していませんが、これらのリンクは少し混乱を解消するのに役立つかもしれません:
APIスキャナー
関連するフォーラム投稿
HiRez
- 2004年1月6日
- 米国西部
- 2009年6月2日
gnasher729
一時停止
- 2005年11月25日
- 2009年6月2日
Chase Rは次のように述べています。Javaクラス用にこのプログラムを書き終えたところです。これは、柵を設置するのにかかる費用を計算する簡単なプログラムです。最後に、「別の見積もりを行いますか(Y / N)」と尋ねられ、ユーザーは「はい」または「いいえ」を選択することになります。しかし、私が私の実行を終えた後、私はそのオプションを取得しません、それはただ言う:
別の見積もりを計算する(Y / N):スレッドの例外 'main' java.lang.StringIndexOutOfBoundsException:文字列インデックスが範囲外です:0
java.lang.String.charAt(String.java:558)で
Test1.main(Test1.java:73)で
なぜこれをしているのですか!!!
おそらく行番号を表示するように作成できるプログラムエディタにソースコードがあるので、ファイルTest1.mainの73行目がどこにあるかを見つけることができるはずです。これにより、問題が発生する場所がわかります。
ここで、インデックス0が「範囲外」であるという例外があります。 n文字の文字列にアクセスする場合、有効なインデックスは何ですか?
では、インデックス0が有効なインデックスではない状況を正確に教えてください。
それはあなたの質問に答えるはずです。
SilentPanda
モデレーター名誉
- 2002年10月8日
- 竹林
- 2009年6月2日
gnasher729によると:おそらく行番号を表示するように作成できるプログラムエディタにソースコードがあるので、ファイルTest1.mainの73行目がどこにあるかを見つけることができるはずです。これにより、問題が発生する場所がわかります。
行は次のとおりです。
コード:
another1 = read.nextLine();
「別の見積もりを計算する(Y / N)」と表示された直後:
人気の投稿