我对没有出现摘要感到很困难。我应该通过添加一个对象数组来修改以前的Java赋值。在循环中,实例化每个单独的对象。确保用户不能继续添加超出数组大小的另一个外部转换。用户从菜单中选择“退出”后,如果用户希望显示摘要报告,请提示。如果他们选择‘Y’,那么,使用您的对象数组,显示以下报告:
Item Conversion Dollars Amount
1 Japanese Yen 100.00 32,000.00
2 Mexican Peso 400.00 56,000.00
3 Canadian Dollar 100.00 156.00等。
倒置数=3
当我运行程序时,没有错误,除非我点击0来结束转换,然后让它问我是否想看一个总结。此错误显示:
线程"main“java.lang.StringIndexOutOfBoundsException中的异常:超出范围的字符串索引:0 at java.lang.String.charAt(String.java:658)在Lab8.main(Lab8.java:43)
我的代码:
导入java.util.Scanner;导入java.text.DecimalFormat;公共类Lab8 {公共静态空主(String[] args) {最终int Max = 10;字符串a;char摘要;int c= 0;Foreign[] Exchange =新ForeignMax;扫描器键盘=新扫描器(System.in);Foreign.opening();do { Exchangec =新外文();Exchangec.getchoice();Exchangec.dollars();Exchangec.amount();Exchangec.vertical();System.out.println("\n“+ Exchangec);c++;System.out.println("\n”+“请选择1到4,或0退出”+ >"\n");c= Keyboard.nextInt();} while (c != 0);System.out.print(“\n您想要转换的摘要吗?(Y/N):");a= Keyboard.nextLine();汇总= a.charAt(0);汇总=Character.toUpperCase(摘要);if (汇总== 'Y') {Character.toUpperCaseSystem.out.println("========\t\t=======\t\t=======\t\t=========");for (int i=0;i< Exchange.length;i++) System.out.println(Exchangei);Foreign.counter();}}
我看了第43行和它的这一行:汇总= a.charAt(0);
但我不知道它有什么问题,有人能指出吗?谢谢。
发布于 2012-11-03 04:41:41
问题不在于这一行,而在于前一段时间的最后一行。您已阅读您的int使用:-
c= Keyboard.nextInt();如果您看到Scanner#nextInt方法的文档,它将从用户输入中读取下一个令牌。因此,当您传递一个整数值时,在末尾的linefeed (也是由于您按下enter而传递的)不会被Keyboard.nextInt读取,然后Keyboard.nextInt将剩余的部分读入:-
a = Keyboard.nextLine();在while退出之后。因此,这个语句基本上是通过prevoius Keyboard.nextInt调用读取行提要的左边,因此a包含一个empty字符串,最后是一个newline。因此,您将得到那个Exception。
解决方案:-
您可以在这个语句之前触发一个空的Keyboard.nextLine,它将使用linefeed作为它的输入,这样您的下一个用户输入就会在它之后开始。
// Your code before this while
} while (c != 0);
Keyboard.nextLine(); // Add this line before the next line
System.out.print("\nWould you like a summary of your conversions? (Y/N): ");
a = Keyboard.nextLine();或者,的另一种方式是您也可以使用Keyboard.nextLine读取整数值。然后使用Integer.parseInt方法将其转换为整数。但是要小心,你必须做一些异常处理。所以,如果你还在学习Exception Handling,那么你就可以走第一条路。所以,在你的do-while里,你可以这样做:
try {
c = Integer.parseInt(Keyboard.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}发布于 2012-11-03 05:02:19
这是常见的问题。当您使用c= Keyboard.nextInt();时,它会读取int。如果在此语句中按下返回键,则a = Keyboard.nextLine();将从输入缓冲区中的前一条语句中读取一个空字符串。
你有两个选择:
Keyboard.nextLine();读取为a = Keyboard.nextLine();之前,添加一个额外的a = Keyboard.nextLine();来清除缓冲区a在退出循环时具有string length >1。有了真正的输入,它就不会进行任何迭代。发布于 2012-11-03 05:38:33
这里是我的控制台类,我用它来代替扫描器来读取命令行应用程序中的键盘输入。注意,类“包装”了在Java1.6中引入的标准java.io.Console。
您可以从我的控制台类中挖掘出所需的方法,将它们直接放在您的Lab8类中。但我鼓励你尽快习惯于将关注点“分离”到不同的类中,创建自己的“键盘”类,作为我的控制台的精简版本。
package krc.utilz.io;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
* A static library of helper methods to read keyboard input.
* <p>
* <strong>usage:</strong>
* <code>
* import krc.utilz.io.Console;
* while ( (score=Console.readInteger("Enter an integer between 0 and 100 (enter to exit) : ", 0, 100, -1)) != -1) { ... }
* </code>
*/
public abstract class Console
{
private static final java.io.Console theConsole = System.console();
static {
if ( theConsole == null ) {
System.err.println("krc.utilz.io.Console: No system console!");
System.exit(2); // 2 traditionally means "system error" on unix.
}
}
private static final DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
private static final DateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss");
public static String readString(String prompt) {
String response = readLine(prompt);
if(response==null) throw new NullPointerException("response cannot be null");
return response;
}
public static String readLine(String prompt) {
if (!prompt.endsWith(" ")) prompt += " ";
System.out.print(prompt);
return theConsole.readLine();
}
public static String readString(String prompt, String regex) {
while(true) {
String response = readString(prompt);
if ( response.length() > 0 ) {
if ( response.matches(regex) ) {
return response;
}
}
System.out.println("Oops: A match for "+regex+" is required!");
}
}
public static Date readDate(String prompt) {
while(true) {
try {
return dateFormatter.parse(readString(prompt+" (dd/MM/yyyy) : "));
} catch (java.text.ParseException e) {
System.out.println("Oops: "+e);
}
}
}
public static Date readTime(String prompt) {
while(true) {
try {
String response = readWord(prompt+" (HH:mm:ss) : ");
return timeFormatter.parse(response);
} catch (java.text.ParseException e) {
System.out.println("Oops: "+e);
}
}
}
public static String readWord(String prompt) {
while(true) {
String response = readString(prompt);
if(response.length()>0 && response.indexOf(' ')<0) return response;
System.out.println("Oops: A single word is required. No spaces.");
}
}
public static String readWordOrNull(String prompt) {
while(true) {
String response = readLine(prompt);
if(response==null||response.length()==0) return null;
if(response.indexOf(' ')<0 ) return response;
System.out.println("Oops: A single word is required. No spaces.");
}
}
public static char readChar(String prompt) {
while ( true ) {
String response = readString(prompt);
if ( response.trim().length() == 1 ) {
return response.trim().charAt(0);
}
System.out.println("Oops: A single non-whitespace character is required!");
}
}
public static char readLetter(String prompt) {
while(true) {
String response = readString(prompt);
if ( response.trim().length() == 1 ) {
char result = response.trim().charAt(0);
if(Character.isLetter(result)) return result;
}
System.out.println("Oops: A single letter is required!");
}
}
public static char readDigit(String prompt) {
while(true) {
String response = readString(prompt);
if ( response.trim().length() == 1 ) {
char result = response.trim().charAt(0);
if(Character.isDigit(result)) return result;
}
System.out.println("Oops: A single digit is required!");
}
}
public static int readInteger(String prompt) {
String response = null;
while(true) {
try {
response = readString(prompt);
if ( response.length()>0 ) {
return Integer.parseInt(response);
}
System.out.println("An integer is required.");
} catch (NumberFormatException e) {
System.out.println("Oops \""+response+"\" cannot be interpreted as 32-bit signed integer!");
}
}
}
public static int readInteger(String prompt, int lowerBound, int upperBound) {
int result = 0;
while(true) {
result = readInteger(prompt);
if ( result>=lowerBound && result<=upperBound ) break;
System.out.println("An integer between "+lowerBound+" and "+upperBound+" (inclusive) is required.");
}
return(result);
}
public static int readInteger(String prompt, int defaultValue) {
String response = null;
while(true) {
try {
response = readString(prompt);
return response.trim().length()>0 ? Integer.parseInt(response) : defaultValue;
} catch (NumberFormatException e) {
System.out.println("Oops \""+response+"\" cannot be interpreted as 32-bit signed integer!");
}
}
}
public static int readInteger(String prompt, int lowerBound, int upperBound, int defaultValue) {
int result = 0;
while(true) {
result = readInteger(prompt, defaultValue);
if ( result==defaultValue || result>=lowerBound && result<=upperBound ) break;
System.out.println("An integer between "+lowerBound+" and "+upperBound+" (inclusive) is required.");
}
return(result);
}
public static double readDouble(String fmt, Object... args) {
String response = null;
while(true) {
try {
response = System.console().readLine(fmt, args);
if ( response!=null && response.length()>0 ) {
return Double.parseDouble(response);
}
System.out.println("A number is required.");
} catch (NumberFormatException e) {
System.out.println("\""+response+"\" cannot be interpreted as a number!");
}
}
}
public static double readDouble(String prompt, double lowerBound, double upperBound) {
while(true) {
double result = readDouble(prompt);
if ( result>=lowerBound && result<=upperBound ) {
return(result);
}
System.out.println("A number between "+lowerBound+" and "+upperBound+" (inclusive) is required.");
}
}
public static boolean readBoolean(String prompt) {
String response = readString(prompt+" (y/N) : ");
return response.trim().equalsIgnoreCase("Y");
}
public static java.io.Console systemConsole() {
return theConsole;
}
}干杯。基思。
PS:练习就容易多了.你进展得很好..。继续干卡车吧。
https://stackoverflow.com/questions/13205990
复制相似问题