0

I've tried looking up on other questions similar to mine but since I'm a newb I don't understand how to get Java to recognise white space in my input. I'm getting the data from user input.

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" );
            }
        }

    }
}
5gon12eder
  • 21,864
  • 5
  • 40
  • 85
Abhi Shan
  • 11
  • 1

2 Answers2

1

You can change the delimiter by default it's white space.

    Scanner in = new Scanner(System.in);

    public void whateverMethod(){
     in.useDelimiter("\\n");
     //rest of code here
   }
ZeroAccess
  • 171
  • 2
  • 3
  • 14
  • sorry for not adding my whole script.. if you don't mind i just added my script if you want to look through to see where I'im going wrong... cheers – Abhi Shan Dec 17 '15 at 01:16
  • Still using a delimiter would work for what you're wanting. put the in.useDelimiter("\\n"); in your createSong method. I recreated your Song class and tested this and it works fine for me so not exactly sure what you're trying to do if this doesn't work. – ZeroAccess Dec 17 '15 at 02:15
1

I don't know what you want to do exactly but this might help. read line and then convert the string to a String array.

    Scanner in=new Scanner(System.in);
    String input=in.nextLine();
    String []array=input.split(" ");
  • sorry for not adding my whole script.. if you don't mind i just added my script if you want to look through to see where I'im going wrong... cheers – Abhi Shan Dec 17 '15 at 01:16