Scanner.nextInt()返回null

时间:2013-09-03 14:48:20

标签: java

编辑:是的,我知道ScanObj不是一个正确的名称。这是我的所有代码。它不完整所以有一些错误和一些故障,但你要求它:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.swing.JFrame;

public class MainClass {
    static String[][] medium = new String[5][4];

    static String names[] = { "Gatz ", "Cat ", "Women", "Mice ", "Robin",

    "Romeo ", "Summer ", "Tempest", "Hamlet ", "Lear ",

    "Titanic ", "StarWars", "DieHard ", "Elf     ", "RED     " };

    static String lexile[] = { "L:1070", "L:123 ", "L:9254", "L:1234",

    "L:534 ", "L:349", "L:632 ", "L:1097", "L:6453", "L:812 ",

    "L:NA ", "L:NA ", "L:NA ", "L:NA ", "L:NA ", "L:NA " };

    static String grade[] = { "G:9", "G:10", "G:12", "G:11", "G:9", "G:12",

    "G:11", "G:9", "G:9", "G:12", "G:12", "G:11", "G:11", "G9", "G:9" };

    static int max = 4;

    char sorttype = (Character) null;

    public static void main(String args[]) throws InterruptedException {

        System.

        out.println("Would you like to browse:\nB)Book C)CD or D)DVD");

        Scanner ScanObj =

        new Scanner(System.in);

        String[] input =

        new String[10];

        input[1] = ScanObj.nextLine();

        int store[] = { 6, 6, 6, 6, 6, 6 };

        if (input[1].equals("B") || input[1].equals("Book")

        || input[1].equals(

        "book") || input[1].equals("b")) {

            String temp[][] =

            new String[5][4];

            int random[] = new int[5];

            boolean repeat = true;

            int nums[] = new int[5];

            for (int i = 0; i < 5; i++) {

                temp[i][0] =

                "book";

                temp[i][1] =

                names[i];

                temp[i][2] =

                lexile[i];

                temp[i][3] =

                grade[i];

                for (int qwop = 0; qwop < 4; qwop++) {

                    medium[i][qwop] = temp[i][qwop];

                    if (qwop == 0)

                        System.

                        out.printf(i + 1 + " | ");

                    else if (qwop < 3)

                        System.

                        out.printf(medium[i][qwop] + " | ");

                    else

                        System.

                        out.println(medium[i][qwop] + "\n");
                    Thread.sleep(150);

                }

                // System.out.println(temp[i][i]);

                // random = store[i];

                if (i == 4)

                    System.

                    out.printf("\n\n\n");

            }

        }

        if (input[1].equals("C") || input[1].equals("c")

        || input[1].equalsIgnoreCase(

        "CD") || input[1].equals("cd")) {

            String temp[][] =

            new String[5][4];

            for (int i = 0; i < 5; i++) {

                int random1 = (int) (Math.random() * max) + 6;

                for (int p = 0; p < 4; p++) {

                    for (random1 = (int) (Math.random() * max) + 6; random1 == store[p];) {

                        random1 = (

                        int) (Math.random() * max) + 6;

                    }

                }

                temp[i][0] =

                "CD";

                temp[i][1] =

                names[i + 5];

                temp[i][2] =

                lexile[i + 5];

                temp[i][3] =

                grade[i + 5];

                for (int qwop = 0; qwop < 4; qwop++) {

                    medium[i][qwop] = temp[i][qwop];

                    if (qwop < 3)

                        System.

                        out.printf(medium[i][qwop] + " | ");

                    else

                        System.

                        out.println(medium[i][qwop] + "\n");
                    Thread.sleep(150);

                }

                // System.out.println(temp[i][i]);

                random1 = store[i];

                if (i == 4)

                    System.

                    out.printf("\n\n\n");

            }

        }

        if (input[1].equalsIgnoreCase("D") || input[1].equalsIgnoreCase("DVD")) {

            String temp[][] =

            new String[5][4];

            for (int i = 0; i < 5; i++) {

                int random1 = (int) (Math.random() * max) + 6;

                for (int p = 0; p < 4; p++) {

                    for (random1 = (int) (Math.random() * max) + 6; random1 == store[p];) {

                        random1 = (

                        int) (Math.random() * max) + 6;

                    }

                }

                temp[i][0] =

                "DVD";

                temp[i][1] =

                names[i + 10];

                temp[i][2] =

                lexile[i + 10];

                temp[i][3] =

                grade[i + 10];

                for (int qwop = 0; qwop < 4; qwop++) {

                    medium[i][qwop] = temp[i][qwop];

                    if (qwop < 3)

                        System.

                        out.printf(medium[i][qwop] + " | ");

                    else

                        System.

                        out.println(medium[i][qwop] + "\n");
                    Thread.sleep(150);

                }

                // System.out.println(temp[i][i]);

                random1 = store[i];

                if (i == 4)

                    System.

                    out.printf("\n\n\n");

            }

        }

        // for loop goes through each of the parameters and compltes the

        // instructions

        System.

        out

        .println(

        "How would you like to sort?\nA)Name B) Lexile C) Grade Level");

        input[2] = ScanObj.nextLine();

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 2; j++) {

                // if statement checks if the condition is true, then does the

                // instructions if it is

                // compareTo checks the ascii values of two strings. If the

                // second

                // is smaller (would come first in alphabetic order) it returns

                // -1

                boolean mismatch = true;

                while (mismatch) {

                    if (input[2].equalsIgnoreCase("a")) {

                        mismatch =

                        false;

                        // makes a new temporary String

                        String temp1 =

                        null;

                        // Switches the String at position i with the one above

                        // it

                        for (int z = 0; z < medium.length - 1; z++) {

                            for (int x = z + 1; x < medium.length; x++) {

                                if (medium[z][1].compareTo(medium[x][1]) > 0) {

                                    for (int qwop = 0; qwop < 4; qwop++) {

                                        temp1 =

                                        medium[z][qwop];

                                        medium[z][qwop] = medium[x][qwop];

                                        medium[x][qwop] = temp1;

                                    }

                                    mismatch =

                                    true;

                                }

                            }

                        }

                    }

                    if (input[2].equalsIgnoreCase("b")) {

                        mismatch =

                        false;

                        // makes a new temporary String

                        String temp3 =

                        null;

                        // Switches the String at position i with the one above

                        // it

                        for (int z = 0; z < medium.length - 1; z++) {

                            for (int x = z + 1; x < medium.length; x++) {

                                if (medium[z][2].compareTo(medium[x][2]) > 0) {

                                    for (int qwop = 0; qwop < 4; qwop++) {

                                        temp3 =

                                        medium[z][qwop];

                                        medium[z][qwop] = medium[x][qwop];

                                        medium[x][qwop] = temp3;

                                    }

                                    mismatch =

                                    true;

                                }

                            }

                        }

                    }

                    if (input[2].equalsIgnoreCase("c")) {

                        mismatch =

                        false;

                        // makes a new temporary String

                        String temp3 =

                        null;

                        // Switches the String at position i with the one above

                        // it

                        for (int z = 0; z < medium.length - 1; z++) {

                            for (int x = z + 1; x < medium.length; x++) {
                                // Here I encounter an error because compareTo()
                                // sorts Lexicographically, meaning anything
                                // starting with a 1 comes before anything
                                // starting with a 2, 3, etc. This means that
                                // Grade 11 comes before grade 9, and simply
                                // comparing greater than does not work due to
                                // the fact that they are Strings.
                                if (medium[z][3].compareTo(medium[x][3]) > 0) {

                                    for (int qwop = 0; qwop < 4; qwop++) {

                                        temp3 =

                                        medium[z][qwop];

                                        medium[z][qwop] = medium[x][qwop];

                                        medium[x][qwop] = temp3;

                                    }

                                    mismatch =

                                    true;

                                }

                            }

                        }

                    }

                }

            }

        }

        Thread.sleep(1000);

        for (int prt = 0; prt < medium.length; prt++) {

            for (int qwop = 0; qwop < 4; qwop++) {

                if (qwop < 3)

                    System.

                    out.printf(medium[prt][qwop] + " | ");

                else

                    System.

                    out.println(medium[prt][qwop]);

            }

        }

        Thread.sleep(1800);

        for (int t = 0; t < 1000; t++) {

            System.

            out.println("\n\n\nTo edit a selection, type edit\n"

            +

            "To sort by a different category, type sort\n"

            +

            "To browse another medium, type browse\n"

            +

            "To search the " + medium[1][0] + "s, type search");

            input[3] = ScanObj.nextLine();

            if (input[3].equalsIgnoreCase("browse")) {

                Thread.sleep(500);

                System.out.println("What would you like to browse?:");

                if (medium[1][0].equals("book")) {

                    System.out.println("CDs, or DVDs");

                    input[4] = ScanObj.nextLine();
                }
                if (medium[1][0].equals("CD")) {

                    System.out.println("Books, or DVDs");

                    input[4] = ScanObj.nextLine();
                }
                if (medium[1][0].equals("DVD")) {

                    System.out.println("Books, or CDs");

                    input[4] = ScanObj.nextLine();

                }
                if (input[4].equalsIgnoreCase("CD")
                        || input[4].equalsIgnoreCase("DVD")
                        || input[4].equalsIgnoreCase("Book")) {

                    for (int i = 0; i < 5; i++) {

                        String temp[][] = new String[5][4];
                        temp[i][0] =

                        "CD";
                        int addon = 0;

                        if (input[4].equals("CD"))
                            addon = 5;

                        if (input[4].equals("DVD"))
                            addon = 10;

                        temp[i][1] =

                        names[i + addon];

                        temp[i][2] =

                        lexile[i + addon];

                        temp[i][3] =

                        grade[i + addon];

                        for (int qwop = 0; qwop < 4; qwop++) {

                            medium[i][qwop] = temp[i][qwop];

                            if (qwop < 3)

                                System.

                                out.printf(medium[i][qwop] + " | ");

                            else

                                System.

                                out.println(medium[i][qwop] + "\n");
                            Thread.sleep(150);

                        }
                    }
                }
            }

            if (input[3].equalsIgnoreCase("edit")) {

                Thread.sleep(500);

                System.

                out.println("First type the number of the " + medium[1][0]

                +

                "\nyou want to edit");

                int input4 = ScanObj.nextInt();

                System.

                out

                .println(

                "Now, type the number of the column you want to edit");

                int input5 = ScanObj.nextInt();

                System.

                out.println("Now, make your edit:\n");

                input[6] = ScanObj.nextLine();

                medium[input4][input5] = input[6];

                System.

                out.println(medium[input4][input5]);

            }

            for (int y = 0; y < 10; y++) {
                if (input[3].equalsIgnoreCase("search")) {

                    Thread.sleep(2000);

                    System.

                    out.println("Would you like to search by 1) Title or 2) Lexile #");

                    int input4 = 1;

                    input4 = ScanObj.nextInt();

                    Thread.sleep(2500);

                    System.

                    out.println("Now Type your search");

                    input[5] = ScanObj.nextLine();

                    Thread.sleep(2500);

                    for (int i = 0; i < 5; i++) {

                        System.

                        out.println(input4 + ", " + input[5]);

                        System.

                        out.println(medium[i][input4]);

                        if (input[5].compareToIgnoreCase(medium[i][input4]) == 0) {
                            y = 10;
                            System.

                            out.println("why do you lie");

                            for (int r = 0; r < 4; r++) {

                                if (r < 3)

                                    System.

                                    out.printf(medium[i][r] + " | ");

                                else

                                    System.

                                    out.println(medium[i][r]);
                                Thread.sleep(150);

                            }
                        }

                        else

                            System.

                            out.println(medium[i][input4]);

                        System.

                        out.println("Couldn't find item, please search again:");

                    }

                }
            }

        }

    }
}

ScanObj是我的扫描仪。我只是想获得Integer输入,但它总是返回null。怎么了?

1 个答案:

答案 0 :(得分:0)

以下是使用扫描仪的示例 -

int choice;

do { 

    System.out.print("Would you like to search by (1) Title or (2) Lexile # : ");
    choice = scan.nextInt();   

} while(choice < 1 || choice > 2); // Keep asking the user for a valid integer

如果输入不是整数,则上面的代码会中断。

您可以使用 nextLine -

Scanner scan = new Scanner(System.in);

String response;

do { 

    System.out.print("Would you like to search by (1) Title or (2) Lexile # : ");
    response = scan.nextLine();   

} while(!response.equals("1") && !response.equals("2"));

int choice = Integer.parseInt(response);

try {
    System.out.print("Say cheeseeeee...");
    Thread.sleep(3000);
} catch(InterruptedException e){
    // handle exception
    e.printStackTrace();
}

System.out.println("\nChoice : " + choice);