因此,我一直在尝试编写一个程序,尽管长度很长,它仍然可以从字符串输入中获取任何整数。如果整数的长度是1,我只能得到它。这是我的代码到目前为止,我写的另一个类的方法。如何从字符串输入中获取任意长度的整数
Scanner input = new Scanner(System.in);
Object user1 = new Object();
String user1Input = null;
System.out.println("Please enter coins");
user1Input = input.nextLine();
while(!(user1Input.contains("end"))){
if(user1Input.contains("Quarters")){
user1.depositQuarters(Character.getNumericValue(user1Input.charAt(0)));
}
}
所以这个代码,我到目前为止,说我输入“2个季度”将返回我的$ 0.50平衡,但如果我输入“20个宿舍”将返回我$ 0.50为好。我已经试过有声明另一个变量
System.out.println("Please enter coins");
int coins = input.nextInt();
String user1Input = input.nextLine();
的另一种方式,然后在同一while循环与if语句如下这一点,并返回一个错误。 任何帮助,将不胜感激。谢谢
可能的重复[从String中获取int,也包含字母,在Java中](http://stackoverflow.com/questions/2338790/get-int-from-string-also-containing-letters-in-java) –
它看起来像你有这样的代码:'user1.depositQuarters(Character.getNumericValue(user1Input.charAt(0)));'它只是改变它看到的第一个索引。 – ryekayo
你的第二个方法是达不到这个:http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – khelwood