Question

Using loops, and not arrays, design a Java program that asks the user for a series...

Using loops, and not arrays, design a Java program that asks the user for a series of names (in no particular order). After the final person's name has been entered, the program shold display the name that is first alphabetically and the name that is last alphabetically. For example, if the user enters the names Kristin, Joel, Beth, Zeb, and Chris, the program would display Adam and Zeb.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program


import java.util.Scanner;
public class FirstandLastNames
{

    public static void main(String[] args)
    {
       Scanner SC=new Scanner(System.in);
        String name,low="",high="";
       
        System.out.print("Please enter a name(Enter q to stop):");
        name=SC.next();
        if(!name.equals("q"))
        {
            low = name;
            high = name;
        }
        while(!name.equals("q"))
        {
        System.out.print("Please enter a name: ");
        name=SC.next();
        int x;
        x = name.compareToIgnoreCase(low);
        if(x<0)
        {
            low = name;
        }
        x = name.compareToIgnoreCase(high);
        if(x >0)
        {
              high = name;
        }
       
        }
   
        System.out.println("First name :"+low);
        System.out.println("Last name :"+high);


    }
   
}

Output


Please enter a name(Enter q to stop):Kristin
Please enter a name: Joel
Please enter a name: Adam
Please enter a name: Beth
Please enter a name: Zeb
Please enter a name: Chris
Please enter a name: q
First name :Adam
Last name :Zeb

Add a comment
Know the answer?
Add Answer to:
Using loops, and not arrays, design a Java program that asks the user for a series...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT