创建基本Java电话簿

时间:2016-06-18 01:19:22

标签: java

我正在学习Java并尝试制作一本简单的电话簿。对于这部分,我试图提示用户选择以下3个选项之一。

public class PhoneBook {

    public static void main (String[] args){
        options();      
        /*This method prompts the user to enter phone number
        String s;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Phone Number");
        s = in.nextLine();
        System.out.println("You entered phone number ");
        System.out.println(s);*/
    }

    public static void options (){
    //This method gives the user choices on what to do

        char choice;
        char enterNumber = 'n';
        char showNumber = 's';
        char closeBook = 'c';

        String read;
        String freeLine = "error";
        Scanner keyboard = new Scanner(System.in);
        while (true){

            System.out.println("Please select from the following");
            System.out.println("n to Enter the number");
            System.out.println("s to Show the number ");
            System.out.println("c to Close the Phone book");

        read = keyboard.nextLine(); 
        choice = read.charAt(0);
        switch (choice) {
        case 'n': enterNumber; 
                system.out.println();
        case 's':showNumber;
                system.out.println();
        case 'c': closeBook;
                    break;
        default: System.out.println("Invalid Entry");


        } 
        }
    }

}

当我编译它时,我在第37,39和41行上得到错误说"错误:不是声明"。我觉得缺少一些东西。如果有人可以提供帮助,将不胜感激。

4 个答案:

答案 0 :(得分:1)

我假设您想要在控制台中为n打印enterNumber字母,以下行:

case 'n': enterNumber; 
          system.out.println();

这不是正确的Java语法。您必须将变量值传递给System.out.println方法调用:

case 'n': System.out.println(enterNumber);

另请注意,Java区分大小写,因此您必须使用大写字母拼写System

在旁注中,您需要在每个break;语句后case,否则以下情况的代码也会执行:

switch (choice) {
    case 'n': System.out.println(enterNumber);
              break;
    case 's': System.out.println(showNumber);
              break;
    case 'c': System.out.println(closeBook);
              break;
    default: System.out.println("Invalid Entry");
} 

答案 1 :(得分:0)

你不必在“演员”之后写变量。言。

参考下面的代码。

import java.util.Scanner;

public class PhoneBook {


    public static void main (String[] args){
        options();      
        /*This method prompts the user to enter phone number
        String s;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Phone Number");
        s = in.nextLine();
        System.out.println("You entered phone number ");
        System.out.println(s);*/
    }

    public static void options (){
    //This method gives the user choices on what to do

        char choice;
        char enterNumber = 'n';
        char showNumber = 's';
        char closeBook = 'c';

        String read;
        String freeLine = "error";
        Scanner keyboard = new Scanner(System.in);
        while (true){

            System.out.println("Please select from the following");
            System.out.println("n to Enter the number");
            System.out.println("s to Show the number ");
            System.out.println("c to Close the Phone book");

        read = keyboard.nextLine(); 
        choice = read.charAt(0);
        switch (choice) {
        case 'n':  
        System.out.println();
        case 's':
        System.out.println();
        case 'c': 
                    break;
        default: System.out.println("Invalid Entry");


        } 
        }
    }

}

答案 2 :(得分:0)

我为你做了一个特别的答案。我不添加其他说明。这是一个很大的答案。我告诉的不仅仅是你问的问题,但是我已经尽力制作一个可读的代码,这样你就可以逐步分析,以便至少在尝试制作电话簿(控制台测试驱动应用程序)时了解你的需求。 。如果您需要更多解释,请在评论下写下。

首先制作PhoneEntry课程:

import java.util.Objects;

public class PhoneEntry implements Comparable<PhoneEntry> {
    // https://jex.im/regulex/#!embed=false&flags=&re=%5E%5Ba-zA-Z%5D%7B2%2C%7D((-%7C%5Cs)%5Ba-zA-Z%5D%7B2%2C%7D)*%24
    private static final String NAME_PATTERN = "^[a-zA-Z]{2,}((\\-|\\s)[a-zA-Z]{2,})*$";

    // https://jex.im/regulex/#!embed=false&flags=&re=%5E%5C%2B%3F%5Cd%2B((%5Cs%7C%5C-)%3F%5Cd%2B)%2B%24
    private static final String NUMBER_PATTERN = "^\\+?\\d+((\\s|\\-)?\\d+)+$"; //^\+?\d+((\s|\-)?\d+)+$

    private final String name;
    private final String number;

    public PhoneEntry(String name, String number) {
        if (!name.matches(NAME_PATTERN) || !number.matches(NUMBER_PATTERN)) {
            throw new IllegalArgumentException();
        }
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public String getNumber() {
        return number;
    }

    public boolean nameContainsIgnoreCase(String keyword) {
        return (keyword != null)
                ? name.toLowerCase().contains(keyword.toLowerCase())
                : true;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof PhoneEntry)) {
            return false;
        }
        PhoneEntry phoneEntry = (PhoneEntry) obj;
        return name.equalsIgnoreCase(phoneEntry.name)
                && number.equalsIgnoreCase(phoneEntry.number);
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 17 * hash + Objects.hashCode(this.name.toLowerCase());
        hash = 17 * hash + Objects.hashCode(this.number.toLowerCase());
        return hash;
    }

    @Override
    public int compareTo(PhoneEntry phoneEntry) {
        return name.compareToIgnoreCase(phoneEntry.name);
    }
}

然后是试驾

public class TestDrive {

    private static final String choices = "nspc";

    enum Choice {
        CREATE, READ, PRINT, CLOSE;

        static Choice getChoice(char c) {
            switch (c) {
                case 'n':
                    return Choice.CREATE;
                case 's':
                    return Choice.READ;
                case 'p':
                    return Choice.PRINT;
                case 'c':
                    return Choice.CLOSE;
            }
            return null;
        }
    }

    // Main
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        final Set<PhoneEntry> entries = new TreeSet<>();
        Choice choice;
        while ((choice = getChoice(kbd)) != Choice.CLOSE) {
            switch (choice) {
                case CREATE:
                    PhoneEntry entry = getPhoneEntry(kbd);
                    if (entry != null) {
                        entries.add(entry);
                    }
                    break;
                case READ:
                    print(readEntries(entries, kbd));
                    break;
                case PRINT:
                    print(entries);
                    break;
            }
        }
    }

    private static Choice getChoice(Scanner kbd) {
        System.out.println("\nPlease select from the following");
        System.out.println("\tn to Enter the number");
        System.out.println("\ts to Show numbers by keyword ");
        System.out.println("\tp to Show all numbers ");
        System.out.println("\tc to Close the Phone book");
        System.out.print("> ");
        String input = kbd.nextLine();
        Choice choice = null;
        if (!input.isEmpty()
                && choices.contains(input.toLowerCase())
                && ((choice = Choice.getChoice(input.toLowerCase().charAt(0))) != null)) {
            return choice;
        }
        System.out.println("ERR: INVALID ENTRY. TRY AGAIN");
        return getChoice(kbd);
    }

    private static PhoneEntry getPhoneEntry(Scanner kbd) {
        System.out.print("Type contact name: ");
        String name = kbd.nextLine();
        System.out.print("Type phone number: ");
        String number = kbd.nextLine();
        try {
            return new PhoneEntry(name, number);
        } catch (IllegalArgumentException ex) {
            System.out.println("\nERR: WRONG ENTRY");
        }
        return null;
    }

    private static void print(Set<PhoneEntry> entries) {
        System.out.println("\nPHONE NUMBERS\n");
        entries.stream().forEach(entry -> {
            System.out.printf("Name: %s%nPhone: %s%n%n",
                    entry.getName(), entry.getNumber());
        });
    }

    private static Set<PhoneEntry> readEntries(Set<PhoneEntry> entries, Scanner kbd) {
        System.out.print("Type keyword: ");
        return entries.stream().filter(entry
                    -> entry.nameContainsIgnoreCase(kbd.nextLine()))
                .collect(Collectors.toCollection(TreeSet::new));
    }
}

答案 3 :(得分:0)

而不是enterNumber;,您必须写enterNumber();

括号表示:调用方法。