-3

I have to enter cities in an array list and then analyze the inputted citites to find the city with the most upercase letters. But i cannot figure out the code to anylyze the enteries in the arraylist and find the word with the most uppercase letters.

package cirties2;
import java.util.Scanner;
import java.util.*;  

public class Cirties2 {

public static void main(String[] args) 
{
  Scanner city = new Scanner(System.in);  
  System.out.println("Enter the cities<enter stop to exit>");  
  List<String> cities = new ArrayList<String>( );  
  boolean thing = true;  
 while(thing)  
    {  
      String s = city.nextLine( );  

        if(s.equalsIgnoreCase("stop"))  
        {  
          System.out.println(cities);
          break;      
        }  
        else  
        {  
          System.out.println("Enter the cities<enter stop to exit>");  
          cities.add(s);  
        }  
    }  

}
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Cachecode
  • 1
  • 2
  • 11
    You may be doing your brain a disservice but not allowing it to try to write the code first itself, and you may be giving it an insult by not thinking that it is capable of doing so. I'm betting that it is. I say, give it a go first, and see what you come up with -- you may be pleasantly surprised. Even if you don't succeed, you'll be able to come back here with a much more specific and improved question, which will likely gain you much better and more specific answers. – Hovercraft Full Of Eels Dec 16 '14 at 16:34
  • 4
    Examine each item in the list, count the upper-case letters, keep track of the highest count you get. Figure out one step at a time. – Hot Licks Dec 16 '14 at 16:38
  • 3
    To point you in the right direction, look into `toCharArray()` and `Character.isUpperCase(your char)`. Hopefully you can figure it out. :) – Drew Kennedy Dec 16 '14 at 16:43
  • Psst: characters are just numbers representing position (or codepoint) in UnicodeTable, so you can for instance compare them like `yourCharacter<'Z'`. – Pshemo Dec 16 '14 at 16:50
  • additionally you could have a look at the new features of java 8: `list.sort((String a, String b) -> b.chars().filter((int i)->Character.isUpperCase(i)).reduce(0,(int x, int y)-> y+1)-a.chars().filter((int i)->Character.isUpperCase(i)).reduce(0,(int x, int y)-> y+1))` – Christian Dietrich Dec 16 '14 at 17:34

2 Answers2

0

It is too easy, I will give you a few hints though .

Looping through an Array List use the first way

To check whether a letter is Uppercase or not see this

You only care about the word with most Uppercase letters, so keep updating a variable (let's call it track)containing the index of the word with the highest number of uppercase letter whenever you find a word with more uppercase letter than the one track is pointing to.

You shouldn't ask such simple questions here, at least not without trying something first and showing us what you did so far. You will get a lot of down votes. I'm a fairly good programmer now but I'm talking from experience, feels good to be on the patronizing side lol.

Community
  • 1
  • 1
Roudy Tarabay
  • 449
  • 1
  • 4
  • 18
0

Assuming that your strings contain only alphabets, upper case alphabets ASCII values are less than their lower case alternatives. So if u're using String.hashCode(), the string which is having more Upper case alphabets will have the smallest hashcode among all the strings.

Scan through your list, get the hashcode of each string in every iteration, update the minimum if its less than the previous value.

Hope this helps :)

Arkantos
  • 6,530
  • 2
  • 16
  • 36
  • No one stated that the solution should be limited to ASCII characters. There are thousands of uppercase characters with codepoint values greater than 'z'. – Tom Blodget Dec 17 '14 at 00:40