On the below program, when it asks the question: System.out.println(\"How should
ID: 3661722 • Letter: O
Question
On the below program, when it asks the question:
System.out.println("How should these songs be sorted? 1) title 2) artist 3) playing time");
If I type a # other than 1-3 it correctly defaults to: System.out.println("Invalid choice. Please choose 1, 2, or 3.");
But if I type something other than a # such as the word “artist”, instead of using a number, then It errors out with the following:
Ï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)
How do I get this program to correctly apply the Else line System.out.println("Invalid choice. Please choose 1, 2, or 3."); if the operator enters a character instead of numeric response?
__________________________________________________________________
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 (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.");
}
}
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
I am posting RecordingSort.java with required changes in bold
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=0;
do
{
System.out.println("How should these songs be sorted? 1) title 2) artist 3) playing time");
//Check if given is integer and then only proceed with the otherwise prompt do input.next() in else and take //a new input
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.");
input.next();
}
}
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();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.