Car with Lowest Mileage - java - Spicy Coders

Recent

Tuesday, July 18, 2017

Car with Lowest Mileage - java

The name and mileage of certain cars is passed as the input. The format is CARNAME@MILEAGE and the input is as a single line, with each car information separated by a space. The program must print the car with the lowest mileage. (Assume no two cars will have the lowest mileage) 
Input Format: The first line contains the CARNAME@MILEAGE separated by a space. Output Format: The first line contains the name of the car with the lowest mileage.
Boundary Conditions: The length of the input string is between 4 to 10000. The length of the car name is from 1 to 50.
Example Input/Output 1: 
Input: Zantro@16.15 Zity@12.5 Gamry@9.8 
Output: Gamry
Code:
import java.util.*;
public class Hello {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        String s1=s.nextLine();
        String[] wrds=s1.split(" ");
        float t=99999.999f;
        String s2=" ";
        for(int i=0;i<wrds.length;i++)
        {
        String[] nxt=wrds[i].split("\\@");
        if(Float.parseFloat(nxt[1])<t)
        {
            t=Float.parseFloat(nxt[1]);
            s2=nxt[0];
        }
        }
        System.out.println(s2);   
        }

}

No comments:

Post a Comment