Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'m having a difficult time solving this problem: Write a static method

ID: 3539504 • Letter: H

Question

Hello,


I'm having a difficult time solving this problem:


Write a static method named longestName that reads names typed by the user and prints the longest name (the name that contains the most characters) in the format shown below. Your method should accept a console Scanner and an integer n as parameters and should then prompt for n names.

The longest name should be printed with its first letter capitalized and all subsequent letters in lowercase, regardless of the capitalization the user used when typing in the name. If there is a tie for longest between two or more names, use the tied name that was typed earliest. Also print a message saying that there was a tie, as in the right log below. It's possible that some shorter names will tie in length, such as DANE and Erik in the left log below; but don't print a message unless the tie is between the longest names. You may assume that n is at least 1, that each name is at least 1 character long, and that the user will type single-word names consisting of only letters. The following table shows two sample calls and their output:



Here's my code as of right now:


import java.util.Scanner;

public class LongestName {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

longestName(input, 5);

}

public static void longestName(Scanner input, int num){

String name = ""; String longest = "";

int tieNum = 0;

for (int i = 1; i <= num; i++){

System.out.printf("name #%d? ", i);

name = input.nextLine();

if (longest.length() < name.length()){

longest = name;

}

if (longest.length() == name.length()){

tieNum++;   

}

}

longest = longest.toLowerCase();

longest = Character.toUpperCase (longest.charAt(0)) + longest.substring(1);

  

System.out.printf("%s's name is longest ", longest);

if (tieNum > 0)

System.out.println("(There was a tie!)");

  

}

}


My problem is with having a tie. It always prints out "(There was a tie!)" even when there isn't one. I'm guessing this comes up because of the if statement    if (longest.length() == name.length()){ tieNum++; }, but what can I do to change it?


Thanks for the help,


-B

Call
  Scanner console = new Scanner(System.in);  longestName(console, 5);  
  Scanner console = new Scanner(System.in);  longestName(console, 7);  
Output
  name #1? roy  name #2? DANE  name #3? Erik  name #4? sTeFaNiE  name #5? LaurA  Stefanie's name is longest  
  name #1? TrEnt  name #2? rita  name #3? JORDAN  name #4? craig  name #5? leslie  name #6? YUKI  name #7? TaNnEr  Jordan's name is longest  (There was a tie!)

Explanation / Answer

This is my answer. It works~!


public static void longestName(Scanner s, int n){

String maxString = "";

int max = 0;

boolean tie = false;

for (int i = 1; i <= n; i++) {

System.out.print("name #" + i + "? ");

String name = s.next();

if (name.length() == max){

tie = true;

}

if (name.length() > max){

max = name.length();

maxString = name;

tie = false;

}

}

System.out.println(maxString.substring(0,1).toUpperCase() + maxString.substring(1).toLowerCase() + "'s name is longest");

if (tie) {

System.out.println("(There was a tie!)");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote