Question

In: Computer Science

Every time I attempt to execute this, I get an inputmismatchexception, which looks like this: Oops!...

Every time I attempt to execute this, I get an inputmismatchexception, which looks like this:
Oops! Read Exception: java.util.InputMismatchExceptionInitial input:
Input sorted by grade
Input sorted selection

This is my code.

import java.util.*;  
import java.io.*;
import java.io.PrintStream;

public class jack {

PrintStream prt = System.out;
int n;
int i;
int j;
double grade[];
String name[];
jack() {

try {
Scanner inf = new Scanner(System.in);
n=inf.nextInt();
inf.nextLine();
grade=new double[n];
name = new String[n];
for (int i=0; i<n; i++){
name[i] = inf.next();
inf.nextLine();
grade[i] = inf.nextDouble();
inf.nextLine();
}
inf.close();
} catch (Exception e) {
prt.printf("\nOops! Read Exception: %s", e);
}
  
}

  
  
private void print() {

for(int i=0; i<n; i++)
{
System.out.println(name[i]+" "+grade[i]);
}
System.out.println();

}
private void sortbyname(){
String minStudent;
int index;
for(int i=0;i<n-1;i++){
minStudent=name[i];
index=i;
  
for(int j=i+1; j<n; j++){
if(name[j].compareTo(minStudent)<0)
{
minStudent = name[j];
index =j;
}
}
String beforewrd = name[index];
name[index]=name[i];
name[i]=beforewrd;   
}
  
}
private void sortbygrade(){
  
for(int i=0;i<n;i++){
int index=i;
for(int j=i+1; j<n; j++){
if(grade[j]<grade[i]){
index = j;
}
}
double smallNum = grade[index];
grade[index] = grade[i];
grade[i]=smallNum;
}
}

public static void main(String args[]) throws Exception{
jack p = new jack ();
System.out.print("Initial input:");
p.print();
  
p.sortbyname();
System.out.print("Input sorted by grade");
p.print();
  
p.sortbygrade();
System.out.print("Input sorted selection");
p.print();
  
  

}
}

Solutions

Expert Solution

OUTPUT OF YOUR CODE:

3                                                                                                                                               

abc                                                                                                                                             

98.79                                                                                                                                           

xyz                                                                                                                                             

99.65                                                                                                                                           

jkl                                                                                                                                             

97.86                                                                                                                                           

Initial input:abc 98.79                                                                                                                         

xyz 99.65                                                                                                                                       

jkl 97.86                                                                                                                                       

                                                                                                                                                

Input sorted by gradeabc 98.79                                                                                                                  

jkl 99.65                                                                                                                                       

xyz 97.86                                                                                                                                       

                                                                                                                                                

Input sorted selectionabc 97.86                                                                                                                 

jkl 98.79                                                                                                                                       

xyz 99.65                                                                                                                                       

                                                                                                                                                

...Program finished with exit code 0

I didn't make any changes to your code. No exceptions occurred.

Output2:

4                                                                                                                                               

John                                                                                                                                            

98.75                                                                                                                                           

Steve                                                                                                                                           

98.54                                                                                                                                           

Harry                                                                                                                                           

85.98                                                                                                                                           

Noah                                                                                                                                            

89                                                                                                                                              

Initial input:John 98.75                                                                                                                        

Steve 98.54                                                                                                                                     

Harry 85.98                                                                                                                                     

Noah 89.0                                                                                                                                       

                                                                                                                                                

Input sorted by gradeHarry 98.75                                                                                                                

John 98.54                                                                                                                                      

Noah 85.98                                                                                                                                      

Steve 89.0                                                                                                                                      

                                                                                                                                                

Input sorted selectionHarry 89.0                                                                                                                

John 85.98                                                                                                                                      

Noah 98.54                                                                                                                                      

Steve 98.75                                                                                                                                         

...Program finished with exit code 0


Related Solutions

How can I do the star pattern looks like: ( for example) ( you might not...
How can I do the star pattern looks like: ( for example) ( you might not notice the difference but there is space at the beginning of row 2 & 4) .* * * * * . * * * * * .* * * * * . * * * * * .* * * * * instead of : . * * * * * . * * * * * . * * * * * ....
6 C++ Questions 13. True/False: every while loop is guaranteed to execute at least one time....
6 C++ Questions 13. True/False: every while loop is guaranteed to execute at least one time. 14. Assume there is a file named "cards.txt" in the current directory which contains 3 ints. Write a code snippet which reads in the three ints and outputs their average (mean) to the screen. 15. True/False: the following function prototype takes an array and its size as its parameters. If you change the value of the array's elements within the function, the changes persist...
Explain why telomeres get shorter and shorter every time a cell divides?
Explain why telomeres get shorter and shorter every time a cell divides?
Can I get a breakdown line by line of the time complexity, I know it is...
Can I get a breakdown line by line of the time complexity, I know it is O(NlogN) but I am trying to better understand time complexity as a whole. Input :- N , Array[N]                                function findSmallestPair()                                sort the Array                                int minimumDiff := INFINITY , A := -1 , B := -1;                                  for(int i : 1 to N-1)                                begin for                currentDiff = Array[i]-Array[i-1]                if(currentDiff < minimumDiff)                begin if   ...
I would like to get som survey question for dry cleaning service , I want to...
I would like to get som survey question for dry cleaning service , I want to attract new people who live in the area like new homewoners.
Assume you have the following jobs to execute with one processor: i t(pi) Arrival Time 0...
Assume you have the following jobs to execute with one processor: i t(pi) Arrival Time 0 75 0 1 40 10 2 25 10 3 20 80 4 45 85 Using the table, assume the context switch time is five time units with RR scheduling. Create a Gantt chart illustrating the execution of these processes. What is the turnaround time for process p3? What is the average wait time for the processes?
A mass on a spring is subjected to an external force that looks like a triangular...
A mass on a spring is subjected to an external force that looks like a triangular wave: F(t) = 1+t from t=-1 to 0 F(t) = 1-t from t=0 to t=1 Periodicity = 2 The differential equation for the position of the mass (using Newton’s law) ends up being: x’’(t) + 2x’(t) + 101x(t) = F(t) 1. Find the generic homogeneous solution 2. Find the Fourier series of F(t) 3. Find the Fourier series of the particular solution (which is...
What it Looks Like to the User The program will loop, asking the user for a...
What it Looks Like to the User The program will loop, asking the user for a bet amount from 0 to 100 (assume dollars, you can use ints or longs). If the user types a 0 that means she wants to quit. Otherwise, accept the amount as their bet and simulate a slot machine pull. Your program will print out a line that looks like a slot machine result containing three strings. Some examples are:  BAR 7 BAR, 7 7 cherries,...
Why the rainbow appears in the atmosphere at specific air humidity? I would like to get...
Why the rainbow appears in the atmosphere at specific air humidity? I would like to get a detailed description for better understanding, some schemes, examples based on basic relations. I would like to get some additional articles, books, or sources to read more too.
I would like to get the step by step solution for the below question Question 11...
I would like to get the step by step solution for the below question Question 11 pts What is the difference between positive economics and normative economics? Group of answer choices Positive economics deals with dynamic systems, while normative economics focuses on static systems. Normative economics deals with how the world actually works, whereas positive economics focuses on what people ought to do. Positive economics requires making value judgments, while normative economics relies solely on factual statements. Normative economics applies...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT