私がこれまでに持っているコード:
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();
eolIndex = songs.indexOf("\n");
int songIndex = songs.indexOf(songNum);
song = songs.substring(songIndex);
System.out.println("\n\n" + song + "\n\n");
}//end main
}//end class
ユーザーが 1 ~ 4 の数字を入力する必要がある場合、出力は曲を含むその行だけになります。ユーザー入力: 1、出力: 1. Bow Wow - Fresh Azimiz。(私の選択ではありません。これは教科書から外れています)。
私の問題は、入力した番号から開始する必要があることをプログラムに認識させることができます。出力には、たまたま行だけでなくすべてが含まれています。
例: 入力: 3
出力: 3. Dave Matthews Band - Crash Into Me (\n) 4. Sheryl Crow - Leaving Las Vegas
これを修正する方法についてのアイデアはありますか?ありがとう!