Java 这里的InputStreamReader有什么问题?

Java 这里的InputStreamReader有什么问题?,java,java.util.scanner,datainputstream,inputstreamreader,Java,Java.util.scanner,Datainputstream,Inputstreamreader,我已经创建了一个Java购物车应用程序。我已经为它使用了InputStreamReader类。但它表现出奇怪的行为。我已经尝试了Scanner类和数据输入流类。但它们似乎不适合这个应用程序 有人能指出这门课有什么问题吗 同样如前所述,Scanner类和DIS类倾向于跳过用户输入,与这里使用ISR类时相同(参见输出:速率)。我厌倦了尝试每一个Java用户输入实用程序类,并一次又一次地修改代码 import java.util.ArrayList; //import java.util.InputM

我已经创建了一个Java购物车应用程序。我已经为它使用了InputStreamReader类。但它表现出奇怪的行为。我已经尝试了Scanner类和数据输入流类。但它们似乎不适合这个应用程序

有人能指出这门课有什么问题吗

同样如前所述,Scanner类和DIS类倾向于跳过用户输入,与这里使用ISR类时相同(参见输出:速率)。我厌倦了尝试每一个Java用户输入实用程序类,并一次又一次地修改代码

import java.util.ArrayList;
//import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.Throwable;


public class NewShop {


protected ArrayList<NewItem> ItemList;
ArrayList<NewItem> cartList ;

   // public DataInputStream dis = new DataInputStream(System.in);
   // private Scanner sc = new Scanner(System.in);
  private InputStreamReader isr = new InputStreamReader(System.in);

 public void addItem() {

    long aCode = 0;
    String aName ="";
    double aRate = 0;
    int aQuantity = 0;
    NewItem foundItem;

    System.out.println("Enter Item code:");
   /* try{
    String adddisString = dis.readLine();}
      catch(IOException e){e.printStackTrace();} */

    try{
    aCode = isr.read();
      System.out.println("code entered is : " + aCode);
       }
         catch(IOException e){e.printStackTrace();}

    foundItem = search(aCode);
    if (foundItem == null) {
        System.out.println("Item name : ");
        try{
        aName = dis.readLine();
          }
             catch(IOException e){e.printStackTrace();}
        System.out.println("Rate : "); 
        try{ aRate = isr.read(); }
           catch(IOException e){e.printStackTrace();}

        System.out.println("Quantity : ");
        try{aQuantity = isr.read();}
        catch(IOException e){e.printStackTrace();}


                  NewItem aItem = new NewItem(aName, aRate, aCode, aQuantity);
                  ItemList.add(aItem);
                  }
                  else {
                  System.out.println("Item exists");
                  }


  } 

}
你可以:

aCode = isr.read();
它返回输入的第一个字符的Int值(请参阅)。 所以对于你输入的“1”,你会得到Ascii码“1”,也就是49

如果要使用InputStreamReader读取字符串,可以按如下方式执行:

InputStreamReader isr = new InputStreamReader(System.in)
int c;
String input = "";
while((c = isr.read()) != -1){
        input+= (char) c;
}
public class NewShop
{
  protected ArrayList<NewItem> ItemList;
  ArrayList<NewItem> cartList ;
  private Scanner in = new Scanner(System.in);
  public void addItem()
  {
    long aCode = 0;
    String aName ="";
    double aRate = 0;
    int aQuantity = 0;
    NewItem foundItem;
    System.out.println("Enter Item code:");
    try
    {
      aCode = in.nextLong(); // this reads the to the first char that is not part of the 
      in.nextLine();         // long value, so we need to read till the end of the line...
      System.out.println("code entered is : " + aCode);
      foundItem = search(aCode);
      if (foundItem == null)
      {
        System.out.println("Item name : ");
        aName = in.nextLine();
        System.out.println("Rate : "); 
        aRate = in.nextDouble();
        in.nextLine();
        System.out.println("Quantity : ");
        aQuantity = in.nextInt();
        in.nextLine();
        NewItem aItem = new NewItem(aName, aRate, aCode, aQuantity);
        ItemList.add(aItem);
      }
      else
      {
        System.out.println("Item exists");
      }
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
  } 
}
但是,如果您使用扫描仪来扫描这类内容,您会得到更好、更容易的结果,请参见@AxelH answer。允许您使用
read
读取每个字符的流字符,从而获得
int

int
不是你想的值,它是一个数字的
char
表示,所以

'0' = 48
'1' = 49
...
更多关于这个

您可能希望在这里使用的是一个在您的情况下更简单的,或者一个可以在
字符串中获取整行内容的

Scanner sc = new Scanner(System.in);
int i = sc.nextInt(); //This will get the next number an parse it in an integer (or throw an exception)
//careful, the <ENTER> is still in the buffer, flush with sc.nextLine(); if needed
sc.close();

如果使用
Reader
s,则必须自己转换任何非字符串值。
DataInput
s使用二进制格式,不适合用户输入

因此,最简单的解决方案是
扫描仪
,请尝试以下方法:

InputStreamReader isr = new InputStreamReader(System.in)
int c;
String input = "";
while((c = isr.read()) != -1){
        input+= (char) c;
}
public class NewShop
{
  protected ArrayList<NewItem> ItemList;
  ArrayList<NewItem> cartList ;
  private Scanner in = new Scanner(System.in);
  public void addItem()
  {
    long aCode = 0;
    String aName ="";
    double aRate = 0;
    int aQuantity = 0;
    NewItem foundItem;
    System.out.println("Enter Item code:");
    try
    {
      aCode = in.nextLong(); // this reads the to the first char that is not part of the 
      in.nextLine();         // long value, so we need to read till the end of the line...
      System.out.println("code entered is : " + aCode);
      foundItem = search(aCode);
      if (foundItem == null)
      {
        System.out.println("Item name : ");
        aName = in.nextLine();
        System.out.println("Rate : "); 
        aRate = in.nextDouble();
        in.nextLine();
        System.out.println("Quantity : ");
        aQuantity = in.nextInt();
        in.nextLine();
        NewItem aItem = new NewItem(aName, aRate, aCode, aQuantity);
        ItemList.add(aItem);
      }
      else
      {
        System.out.println("Item exists");
      }
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
  } 
}
公共类新闻商店
{
受保护的ArrayList项目列表;
arraylistcartlist;
专用扫描仪输入=新扫描仪(System.in);
公共无效附加项()
{
长aCode=0;
字符串aName=“”;
双阿拉伯酸=0;
整数aQuantity=0;
纽芬兰项目;
System.out.println(“输入项目代码:”);
尝试
{
aCode=in.nextLong();//这将读取到不属于
in.nextLine();//长值,所以我们需要一直读到行的末尾。。。
System.out.println(“输入的代码为:+aCode”);
foundItem=搜索(aCode);
if(foundItem==null)
{
System.out.println(“项目名称:”);
aName=in.nextLine();
系统输出打印项次(“费率:”);
aRate=in.nextDouble();
in.nextLine();
系统输出打印项次(“数量:”;
aQuantity=in.nextInt();
in.nextLine();
NewItem aItem=新的NewItem(aName、aRate、aCode、aQuantity);
ItemList.add(aItem);
}
其他的
{
System.out.println(“项目存在”);
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
} 
}

还删除了内部的
try catch
es,就好像任何值都是错误的一样,它以后将不起作用…

您的预期输出是什么?您能否尝试将代码重新索引一点,并提供一个请求?(带有一些数据)请参见“输出”部分。我认为预期的输出是显而易见的。好吧,我看到的是,
输入项目代码:1输入的代码是:49
不是您想要的。。但是你看到它在哪里了吗??您正在使用
read()
读取每个字节的流字节,您将得到一个
char
值,1=49。请参见您想要实现的目标?
isr.read()。第一个输入的字符是
1
,因此变量得到了它的代码
49
…并且关于
input:30
?您将得到返回为Int的第一个字符,即“51”代表“3”,这是一个问题…;)非常感谢。关于ISR类在这里使用哪种方法以便获得预期的输出,有什么建议吗?@Shachindratipath我强烈建议您使用Scanner或BufferedReader。。。但是如果您坚持使用ISR来读取字符串。您可能需要这样的内容:请参阅更新的答案。@UsagiMiyamoto确实,一个字符可能超过一个字节,所以这句话说得很糟糕!谢谢他需要帮助。谢谢但是,你能解释一下,即使它不要求用户输入,它从何处将值“10.0”分配给“Rate”吗?@shachindratipathi好吧,你使用了一个不推荐的,我认为这个类不适合文本输入。但是该方法将您指向
BufferedReader
,请听文档;)基本上,
enter
相当于
0x13 0x10
字符,它使用
0x13
查找“新的行”,将
0x10
保留在缓冲区中,在下一个
read()
上使用值(用于速率)。解释你得到的值哦,第一个不是有注释吗?在注释中;)我没有读完整的代码,所以看不到(特别是大代码),但我想这对新手来说已经足够了。