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" );
}
}
}
}
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
}
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(" ");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.