如何让Java从用户输入中识别空白?

时间:2015-12-17 00:37:04

标签: java eclipse java.util.scanner whitespace

我已经尝试过查找与我类似的其他问题但是因为我是一个新手,我不明白如何让Java在我的输入中识别空白区域。我从用户输入中获取数据。

import java.util.Scanner;

import java.util.ArrayList;

public class Catalogue {

    public static Scanner in = new Scanner(System.in);
    public static ArrayList<Songs> mysongs = new ArrayList<Songs>();

    public static void main(String[]args) {

        int option =0;

        do{

            System.out.println("MP3 Catalogue");
            System.out.println("Menu");
            System.out.println("1. Add a song");
            System.out.println("2. List All Songs");
            System.out.println("3. Search Songs");
            System.out.println("4. Delete Songs");
            System.out.println("5. Shuffle Songs");
            System.out.println("");
            System.out.println("Enter an option to choose: ");

            option = in.nextInt();

            switch(option){

            case 1:

                createSong();
                break;

            case 2:
                listSongs();
                break;

            case 3:
                searchSong();
                break;

            }

        } while(option !=0);
    }

    private static void createSong(){

        Songs newsong = new Songs();

        System.out.println("Song Name: ");
        String songname = in.next();

        System.out.println("Song Artist: ");
        String songartist = in.next();

        System.out.println("Duration: ");
        int duration = in.nextInt();

        System.out.println("Song Number: ");
        int songnumber = in.nextInt();

        newsong.setSong(songname, songartist, duration, songnumber);

        mysongs.add(newsong);

    }

    private static void listSongs(){

        for(int i = 0 ; i< mysongs.size() ; i++){

            System.out.println(mysongs.get(i).getSongNumber() + "." + 
                        mysongs.get(i).getSongName() + " by " +
                        mysongs.get(i).getSongArtist() + " " +
                        mysongs.get(i).getDuration() + "mins");
            if(i ==(mysongs.size() - 1)){
                System.out.println();
            }
        }
    }

    private static void searchSong(){

        System.out.println("Enter Song Number: ");
        int songnum = in.nextInt();

        for (int i = 0; i< mysongs.size() ; i++){

            if (mysongs.get(i).getSongNumber() == songnum){

                System.out.println(mysongs.get(i).getSongNumber() + "." + 
                        mysongs.get(i).getSongName() + " by " +
                        mysongs.get(i).getSongArtist() + " " +
                        mysongs.get(i).getDuration() + "mins" + "\n" );
            }
        }

    }
}

2 个答案:

答案 0 :(得分:1)

您可以默认更改分隔符的空白区域。

    Scanner in = new Scanner(System.in);

    public void whateverMethod(){
     in.useDelimiter("\\n");
     //rest of code here
   }

答案 1 :(得分:1)

我不知道你想要做什么,但这可能会有所帮助。 读取行,然后将字符串转换为String数组。

    Scanner in=new Scanner(System.in);
    String input=in.nextLine();
    String []array=input.split(" ");