我的输入在我的程序中有所不同

时间:2016-05-15 01:45:50

标签: java io

问题是我的输入是2,但根据我的程序,它是50.有一些我无法理解的错误。在这里,我的目标是创建一个联系人应用程序,用户可以继续添加他/她的联系人,并在哈希映射中保存和组织所有联系人。

对于IO领域的测试人员,我一直只使用方法contactList()。所以目前其他方法没有很多功能。

 package examples.hash.hashmap.IOintegration;
   import java.util.HashMap;
   import java.io.*;
   public class Contacts{
        /*Aim:
            *Takes input from the user to add, remove or read a contact's number
            *It also can show you all the contacts the user has added
            *What's more it is finally integrated with IO!! 
         */
        //Initializing some very crucial variables 
        HashMap contacts = new HashMap();
        InputStreamReader keyboardMethod = new InputStreamReader(System.in);
        BufferedReader readerMethod = new BufferedReader(keyboardMethod);
        public void contactList(){
            System.out.println(contacts.entrySet());
        }
         public void addContact(){
            System.out.println("Give contacts name");
        }
        public void removeContact(){}
        public int getNumber(){
            return 1;
        }
        public static void main(String[] args)throws IOException{
            InputStreamReader keyboardOption = new InputStreamReader(System.in);
            BufferedReader readerOption = new BufferedReader(keyboardOption);
            Contacts obj = new Contacts();
            System.out.print("Type in your option: ");
            int option = readerOption.read();
            System.out.println(option);
            if(option == 1){
                obj.addContact();
            }
            if(option == 2){
                System.out.println("HI");
                obj.contactList();
            }
            if(option == 3){
                obj.getNumber();
            }
            if(option == 4){
                 obj.removeContact();
            }

        }
    }

2 个答案:

答案 0 :(得分:0)

仅供参考:read()返回字符值,技术上为char,但必须为int才能允许额外-1 EOF的值。

Comment by azurefrog是正确的,char'2'是数字ASCII / Unicode值50。

如果您想阅读用户输入的号码,请执行以下操作:

int option = Integer.parseInt(readerOption.readLine());

注意糟糕的用户输入。

答案 1 :(得分:0)

安德烈亚斯是对的!

我也知道你的申请意图!

while {}循环可以解决你的问题!

我的英语不熟练,所以我改进了你的代码!

更改包裹名称

  package com.fan.component;

 import java.util.HashMap;
 import java.io.*;
 public class Contacts{
 /*Aim:
     *Takes input from the user to add, remove or read a contact's number
     *It also can show you all the contacts the user has added
     *What's more it is finally integrated with IO!! 
  */
 //Initializing some very crucial variables 
 HashMap contacts = new HashMap();
 InputStreamReader keyboardMethod = new InputStreamReader(System.in);
 BufferedReader readerMethod = new BufferedReader(keyboardMethod);
 public void contactList(){
     System.out.println(contacts.entrySet());
 }
  public void addContact() throws IOException{
     System.out.println("Give contacts name");
     //details

        String contactName = readerMethod.readLine();
        String contactNumber= readerMethod.readLine();
        contacts.put(contactName, contactNumber);

 }
 public void removeContact() throws IOException{
     //details
     System.out.println("Give contacts name to remove");
     String contactName = readerMethod.readLine();

     String contactNumber = (String) contacts.get(contactName);
     if(contactName != null)
         contacts.remove(contactNumber);
     else
         System.out.println("No this contact");
 }
 public void getNumber() throws IOException{
     //return 1;
     System.out.println("input contact name");
     String contactName = readerMethod.readLine();
     String contactNum = (String) contacts.get(contactName);
     if(contacts == null)
         System.out.println("No this contact named " + contactName);
     else
         System.out.println(contactName + " : " + contactNum );
 }
 public static void main(String[] args)throws IOException{
     InputStreamReader keyboardOption = new InputStreamReader(System.in);
     BufferedReader readerOption = new BufferedReader(keyboardOption);
     Contacts obj = new Contacts();
     /*
      * added code
      */
     while(true)
     {
         System.out.print("Type in your option: ");
         int option = Integer.parseInt(readerOption.readLine());
         System.out.println(option);
         //add new contact
         if(option == 1){
             obj.addContact();
         }
         //check contact list
         if(option == 2){
             System.out.println("HI");
             obj.contactList();
         }
         //get number
         if(option == 3){
             obj.getNumber();
         }
         //remove contact
         if(option == 4){
             obj.removeContact();
         }
     }

 }

}