Help creating a map.
I need a few pointers on how to create a map for my program.
I have to create a map in which the key is a String that is a phone number and the value is a List of all the words that have the key as that phone number. Then I have to find the longest list.
This is what I have so far. I just dont know if I am doing it correctly and how I am supposed to find the longest list.
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
publicclass Assignment2
{
publicstaticvoid print( String fileName )throws IOException
{
FileReader file =new FileReader( fileName );
Scanner scan =new Scanner( file );
Map<String, List><String>> numbers =new HashMap<String, List><String>>( );
while( scan.hasNextLine( ))
{
String word = scan.nextLine( );
String numSeq = getNumSeq( word );
List<String> theWord = numbers.get( numSeq );
if( theWord ==null )
{
theWord =new ArrayList<String>( );
numbers.put( numSeq, theWord );
}
theWord.add( numSeq );
}
printMap( numbers );
if( file !=null )
{
file.close( );
}
}
publicstaticvoid printMap( Map<String, List><String>> numbers )
{
Collection<List><String> > theValues = numbers.values( );
System.out.println("The following numbers have the most matching words in them:" );
?
}
privatestatic String letters ="abcdefghijklmnopqrstuvwxyz";
privatestatic String numbers ="22233344455566677778889999";
privatestaticchar getNumber(char letter){
int index = letters.indexOf(letter);
if (index < 0){
index = letters.toUpperCase().indexOf(letter);
}
if (index < 0){
return letter;
}else{
return numbers.charAt(index);
}
}
publicstatic String getNumSeq( String word )
{
String numbers ="";
for (int i = 0; i < word.length(); i++){
numbers += getNumber(word.charAt(i));
}
return numbers;
}
publicstaticvoid main( String[ ] args)
{
try
{
print("words.txt" );
}
catch( IOException e )
{
System.err.println("Error opening file." );
}
}
}
Ok, nevermind. I may have this one. Pointers would still be great.
Ok. I figured that map, but I cant figure out for the life of me how to write the code to print the longest list. I figure I have to compare the values in the map and go through the list until I get the longest one, I just have no clue how to write it.
Help would be much appreciated. Thanks.
Have you looked at the methods of Map. There is one that will return you a Set of keys. You can then use an Iterator to loop over the Set.
> Have you looked at the methods of Map. There is one
> that will return you a Set of keys. You can then use
> an Iterator to loop over the Set.
Yea. But I think I'm doing it wrong. I got it to return the values converted into numbers. So it is just showing me the key alone 10 times, but it wont show me the words that have that key.
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Assignment2
{
// This reads the chosen file into the map and prints the map.
public static void print( String fileName ) throws IOException
{
FileReader file = new FileReader( fileName );
Scanner scan = new Scanner( file );
Map< String, List< String > > numbers = new HashMap< String, List< String > >( );
while( scan.hasNextLine( ) )
{
String word = scan.nextLine( );
String numSeq = getNumSeq( word );
List< String > theWord = numbers.get( numSeq );
if( theWord == null )
{
theWord = new ArrayList< String >( );
numbers.put( numSeq, theWord );
}
theWord.add( numSeq );
}
printMap( numbers );
if( file != null )
{
file.close( );
}
}
/*Attempts to scan map and chooses the keys with the longest list
* of values
*/
public static void printMap( Map< String, List< String > > numbers )
{
Collection< List< String > > phNumbers = numbers.values( );
System.out.println( "The following is the longest list: ");
int i = 0;
for( List< String > x : phNumbers )
{
if( x.size( ) >= 10 )
{
i++;
String output = x.toString( );
System.out.println( output.substring( 1, output.length( ) - 1));
}
}
}
/*Creates an array of letters and an array of numbers.
* Takes the word from the file and converts it into characters.
* Converts the characters into its corresponding number.
*/
private static String letters = "abcdefghijklmnopqrstuvwxyz";
private static String numbers = "22233344455566677778889999";
private static char getNumber( char letter )
{
int index = letters.indexOf( letter );
if ( index < 0 )
{
index = letters.toUpperCase( ).indexOf( letter );
}
if ( index < 0 )
{
return letter;
} else {
return numbers.charAt( index );
}
}
public static String getNumSeq( String word )
{
String numbers = "";
for ( int i = 0; i < word.length( ); i++ )
{
numbers += getNumber( word.charAt( i ) );
}
return numbers;
}
//If a file exists, it sends it to be processed.
public static void main( String[ ] args)
{
try
{
print( "words.txt" );
}
catch( IOException e )
{
System.err.println( "Error opening file." );
}
}
}
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Assignment2
{
// This reads the chosen file into the map and prints the map.
public static void print( String fileName ) throws IOException
{
FileReader file = new FileReader( fileName );
Scanner scan = new Scanner( file );
Map< String, List< String > > numbers = new HashMap< String, List< String > >( );
while( scan.hasNextLine( ) )
{
String word = scan.nextLine( );
String numSeq = getNumSeq( word );
List< String > theWord = numbers.get( numSeq );
if( theWord == null )
{
theWord = new ArrayList< String >( );
numbers.put( numSeq, theWord );
}
theWord.add( numSeq );
}
printMap( numbers );
if( file != null )
{
file.close( );
}
}
/*Attempts to scan map and chooses the keys with the longest list
* of values
*/
public static String printMap( Map< String, List< String > > numbers )
{
StringBuffer result = new StringBuffer("");
String tab = "\t";
String lineSeparator = System.getProperty("line.separator");
for(List<String> l: numbers.values()) {
result.append(l+ tab + " " + lineSeparator);
}
System.out.println(result.toString());
return result.toString();
}
/*Creates an array of letters and an array of numbers.
* Takes the word from the file and converts it into characters.
* Converts the characters into its corresponding number.
*/
private static String letters = "abcdefghijklmnopqrstuvwxyz";
private static String numbers = "22233344455566677778889999";
private static char getNumber( char letter )
{
int index = letters.indexOf( letter );
if ( index < 0 )
{
index = letters.toUpperCase( ).indexOf( letter );
}
if ( index < 0 )
{
return letter;
} else {
return numbers.charAt( index );
}
}
public static String getNumSeq( String word )
{
String numbers = "";
for ( int i = 0; i < word.length( ); i++ )
{
numbers += getNumber( word.charAt( i ) );
}
return numbers;
}
//If a file exists, it sends it to be processed.
public static void main( String[ ] args)
{
try
{
print( "words.txt" );
}
catch( IOException e )
{
System.err.println( "Error opening file." );
}
}
}
That code wouldnt work because I am supposed to print out the keys with the longest lists and the values that make up the list. Thank you though. I appreciate the help.