/************************************
*T'Nora Green
*Assignment 4, Exercise 7
*************************************/
import java.util.Scanner;
public class ExtractLine
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String songs =
"1. Bow Wow - Fresh Azimiz\n" +
"2. Weezer - Beverly Hills\n" +
"3. Dave Matthews Band - Crash Into me\n" +
"4. Sheryl Crow - Leaving Las Vegas\n";
String songNum; //song number that is searched for
int songIndex; //position of where song number is found
int eolIndex; //position of end of line character
String song; //the specified line
System.out.print("Enter song number: ");
songNum = stdIn.nextLine();
songIndex = songs.indexOf(songNum);
eolIndex = songs.indexOf("\n", songIndex);
song = songs.substring(songIndex, eolIndex);
System.out.println(song);
}//end main
}//end class ExtractLine
No comments:
Post a Comment