Per prior question, I included the bolded text, but still getting the below erro
ID: 3661739 • Letter: P
Question
Per prior question, I included the bolded text, but still getting the below error message when enter anything with a letter instead of a # as the answer to:
System.out.println("How should these songs be sorted? 1) title 2) artist 3) playing time");
sortMethod = input.nextInt();
Exception in thread "main" java.util.InputMismatchException
ÏÏ§Ï at java.util.Scanner.throwFor(Scanner.java:864)
ÏÏ§Ï at java.util.Scanner.next(Scanner.java:1485)
ÏÏ§Ï at java.util.Scanner.nextInt(Scanner.java:2117)
ÏÏ§Ï at java.util.Scanner.nextInt(Scanner.java:2076)
ÏÏ§Ï at RecordingSort.main(RecordingSort.java:40)
import java.util.Scanner;
public class RecordingSort
{
public static void main(String[] args)
{
Recording[] list = new Recording[5];
Scanner input = new Scanner(System.in);
int i = 0;
for (i = 0; i < list.length; i++)
{
int j = i + 1;
System.out.print("Enter song " + j + "'s title: ");
String title = input.nextLine();
System.out.print("Enter song " + j + "'s artist: ");
String artist = input.nextLine();
System.out.print("Enter song " + j + "'s playing time in seconds: ");
String playingTimeString = input.nextLine();
int playingTime = Integer.parseInt(playingTimeString);
list[i] = new Recording(title, artist, playingTime);
System.out.println();
}
int sortMethod;
do
{
System.out.println("How should these songs be sorted? 1) title 2) artist 3) playing time");
sortMethod = input.nextInt();
if (input.hasNextInt())
{
sortMethod = input.nextInt();
if (sortMethod > 0 && sortMethod < 4)
{
int a, b;
int highestVal = list.length - 1;
for (a = 0; a < highestVal; a++)
{
for (b = 0; b < highestVal; b++)
{
int c = b + 1;
if (sortMethod == 1)
{
if (list[b].getTitle().compareTo(list[c].getTitle()) > 0)
{
Recording temp = list[b];
list[b] = list[c];
list[c] = temp;
}
}
else if (sortMethod == 2)
{
if (list[b].getArtist().compareTo(list[c].getArtist()) > 0)
{
Recording temp = list[b];
list[b] = list[c];
list[c] = temp;
}
}
else if (sortMethod == 3)
{
if (list[b].getPlayingTime() > list[c].getPlayingTime())
{
Recording temp = list[b];
list[b] = list[c];
list[c] = temp;
}
}
}
}
}
else
{
System.out.println("Invalid choice. Please choose 1, 2, or 3.");
}
}
else
{
System.out.println("Invalid choice. Please choose 1, 2, or 3.");
}
}
while (sortMethod < 1 || sortMethod > 3);
System.out.println();
for (i = 0; i < list.length; i++)
{
System.out.println("Song: Title: " + list[i].getTitle() + ". Artist: " + list[i].getArtist() + ". Playing time: "
+ list[i].getPlayingTime() + " seconds.");
}
input.close();
}
}
import java.util.*;
class Recording
{
String title;
String artist;
int playingTime;
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getArtist()
{
return artist;
}
public void setArtist(String artist)
{
this.artist = artist;
}
public int getPlayingTime()
{
return playingTime;
}
public void setPlayingTime(int i)
{
this.playingTime = i;
}
public Recording(String title, String artist, int playingTime)
{
this.title = title;
this.artist = artist;
this.playingTime = playingTime;
}
}
Explanation / Answer
//Modified code is in bold letters
//RecordingSort.java
import java.util.Scanner;
public class RecordingSort
{
public static void main(String[] args)
{
Recording[] list = new Recording[5];
Scanner input = new Scanner(System.in);
int i = 0;
for (i = 0; i < list.length; i++)
{
int j = i + 1;
System.out.print("Enter song " + j + "'s title: ");
String title = input.nextLine();
System.out.print("Enter song " + j + "'s artist: ");
String artist = input.nextLine();
System.out.print("Enter song " + j + "'s playing time in seconds: ");
String playingTimeString = input.nextLine();
int playingTime = Integer.parseInt(playingTimeString);
list[i] = new Recording(title, artist, playingTime);
System.out.println();
}
int sortMethod;
do
{
System.out.println("How should these songs be sorted? 1) title 2) artist 3) playing time");
sortMethod = input.nextInt();
//Remove hasNextInt method , it stops program
//The method hasNextInt keep on checking in while loop
//if enter input is integer value.
if (sortMethod > 0 && sortMethod < 4)
{
int a, b;
int highestVal = list.length - 1;
for (a = 0; a < highestVal; a++)
{
for (b = 0; b < highestVal; b++)
{
int c = b + 1;
if (sortMethod == 1)
{
if (list[b].getTitle().compareTo(list[c].getTitle()) > 0)
{
Recording temp = list[b];
list[b] = list[c];
list[c] = temp;
}
}
else if (sortMethod == 2)
{
if (list[b].getArtist().compareTo(list[c].getArtist()) > 0)
{
Recording temp = list[b];
list[b] = list[c];
list[c] = temp;
}
}
else if (sortMethod == 3)
{
if (list[b].getPlayingTime() > list[c].getPlayingTime())
{
Recording temp = list[b];
list[b] = list[c];
list[c] = temp;
}
}
}
}
}
//Remove one else case from code
else
{
System.out.println("Invalid choice. Please choose 1, 2, or 3.");
}
}while (sortMethod < 1 || sortMethod > 3);
System.out.println();
for (i = 0; i < list.length; i++)
{
System.out.println(
"Song: Title: " +list[i].getTitle() +
". Artist: " + list[i].getArtist() +
". Playing time: "+ list[i].getPlayingTime()
+ " seconds.");
}
//close the input scanner
input.close();
}
}
---------------------------------------------------------------------------------------------
Sample output:
Enter song 1's title: Beat it
Enter song 1's artist: jackson
Enter song 1's playing time in seconds: 100
Enter song 2's title: voka
Enter song 2's artist: sharapova
Enter song 2's playing time in seconds: 120
Enter song 3's title: baby
Enter song 3's artist: biber
Enter song 3's playing time in seconds: 130
Enter song 4's title: dangerous
Enter song 4's artist: jackson
Enter song 4's playing time in seconds: 150
Enter song 5's title: my heart
Enter song 5's artist: titanic
Enter song 5's playing time in seconds: 160
How should these songs be sorted? 1) title 2) artist 3) playing time
4
Invalid choice. Please choose 1, 2, or 3.
How should these songs be sorted? 1) title 2) artist 3) playing time
3
Song: Title: Beat it. Artist: jackson. Playing time: 100 seconds.
Song: Title: voka. Artist: sharapova. Playing time: 120 seconds.
Song: Title: baby. Artist: biber. Playing time: 130 seconds.
Song: Title: dangerous. Artist: jackson. Playing time: 150 seconds.
Song: Title: my heart. Artist: titanic. Playing time: 160 seconds.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.