[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ArrayList

Posted on 2004-11-08
6
Medium Priority
?
715 Views
Last Modified: 2008-03-17
I have an error of incompatiable types in the following code and just can't seem to figure it out.  Can anyone lend me a hand?

Here is the code:

/*
 * readDictionaryFile.java
 *
 * Created on November 3, 2004, 12:46 AM
 */

/**
 *
 * @author  Shannon DM  Rice
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class readDictionaryFile {

String readList;
String dictionaryWord;
String scrambledDictWord;
 
    /** Creates a new instance of readDictionaryFile */
    public readDictionaryFile() {
       
        readFile();
     
    }
   
    public void readFile() throws IOException {
                 
        File dictionaryFile = new File("dictionayWords.txt");//declare file      
        FileReader fReader = new FileReader(dictionaryFile);//dictionaryFile object
       
        if (dictionaryFile != null) {
           
            BufferedReader buffReader = new BufferedReader(fReader);//input from dictionaryFile
            String line;//word
       
            ArrayList word = new ArrayList();//create default word array
            ArrayList scrambled = new ArrayList();//create default scrambled array
           
        while (null != (line = buffReader.readLine())) {
            word.add(line);//add word to end of array list
            scrambled.add(scramble(line));//add scsrambled word to end of array list
            }
           
         
            String dictionaryWord[] = new String[word.size()];
            dictionaryWord = (String[])word.toArray(dictionaryWord);
           
            String scrambledDictWord[] = new String[scrambled.size()];
            scrambledDictWord = (String[])scrambled.toArray(scrambledDictWord);
     
        double randomNumber = (Math.random() * scrambledDictWord.length);//select random scrambed word

--------------------error is here------------------
--------------------Found:  java.lang.String  Required:  java.util.ArrayList---

           scrambled = scrambledDictWord[(int)randomNumber];//sets the scrambedWord field
           word = dictionaryWord[(int)randomNumber];//sets the unscrambledWorld field

----------------------------------------------------------------------------------------------------------------------            
           buffReader.close();
       }
       
        else {
            throw new IOException("File not found");
        }
    }      

    public String getunscrambledWord(){
        return dictionaryWord;
    }
   
    public String getscrambledWord(){
        return scrambledDictWord;
    }
   

    public String scramble(String word) {
        int size = word.length();//word length
        StringBuffer buffer = new StringBuffer();//creates empty buffer
       
        for (int i = 0; i + 1 < size; i += 2) {
            buffer.append(word.charAt(i + 1));
            buffer.append(word.charAt(i));
        }

        if (buffer.length() < word.length()) {
            buffer.append(word.charAt(word.length() - 1));
        }
   
    return buffer.toString();
    }
   
}
0
Comment
Question by:shannonrice
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12530276
>   scrambled = scrambledDictWord[(int)randomNumber];//sets the scrambedWord field

you cannot assign a String to an ArrayList
perhaps should be:

String word = scrambledDictWord[(int)randomNumber];

What is it you want to achieve?
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12530322
the correction is:

import java.io.*;
import java.util.*;
import java.lang.*;

public class readDictionaryFile {

String readList;
String dictionaryWord;
String scrambledDictWord;
 
    /** Creates a new instance of readDictionaryFile */
    public readDictionaryFile() {
        try
        {
            readFile();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
   
    public void readFile() throws IOException {
                 
        File dictionaryFile = new File("dictionayWords.txt");//declare file      
        FileReader fReader = new FileReader(dictionaryFile);//dictionaryFile object
       
        if (dictionaryFile != null) {
           
            BufferedReader buffReader = new BufferedReader(fReader);//input from dictionaryFile
            String line;//word
       
            ArrayList word = new ArrayList();//create default word array
            ArrayList scrambled = new ArrayList();//create default scrambled array
           
        while (null != (line = buffReader.readLine())) {
            word.add(line);//add word to end of array list
            scrambled.add(scramble(line));//add scsrambled word to end of array list
            }
           
         
            String dictionaryWord[] = new String[word.size()];
            dictionaryWord = (String[])word.toArray(dictionaryWord);
           
            String scrambledDictWord[] = new String[scrambled.size()];
            scrambledDictWord = (String[])scrambled.toArray(scrambledDictWord);
     
        double randomNumber = (Math.random() * scrambledDictWord.length);//select random scrambed word


           scrambled.add(scrambledDictWord[(int)randomNumber]) ;//sets the scrambedWord field
           scrambled.add(dictionaryWord[(int)randomNumber]);//sets the unscrambledWorld field

           buffReader.close();
       }
       
        else {
            throw new IOException("File not found");
        }
    }    

    public String getunscrambledWord(){
        return dictionaryWord;
    }
   
    public String getscrambledWord(){
        return scrambledDictWord;
    }
   

    public String scramble(String word) {
        int size = word.length();//word length
        StringBuffer buffer = new StringBuffer();//creates empty buffer
       
        for (int i = 0; i + 1 < size; i += 2) {
            buffer.append(word.charAt(i + 1));
            buffer.append(word.charAt(i));
        }

        if (buffer.length() < word.length()) {
            buffer.append(word.charAt(word.length() - 1));
        }
   
    return buffer.toString();
    }
   
}
0
 

Author Comment

by:shannonrice
ID: 12530344
I am trying to create a scramble word game.  I want to select a random word from the dictionaryWord.txt (45,500 words) and scramble it then output it to a JPanel.  I am new to programming and could be going about this all wrong.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:petmagdy
ID: 12530381
ahhhhh ok,
replace:

>>           scrambled = scrambledDictWord[(int)randomNumber];//sets the scrambedWord field
>>           word = dictionaryWord[(int)randomNumber];//sets the unscrambledWorld field

with:
       this.dictionaryWord = scrambledDictWord[(int)randomNumber];
        this.scrambledDictWord = dictionaryWord[(int)randomNumber];


0
 
LVL 13

Accepted Solution

by:
petmagdy earned 1000 total points
ID: 12530396
also don't forget to change readDictionaryFile() constructor to:

    public readDictionaryFile() {
        try
        {
            readFile();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
0
 

Author Comment

by:shannonrice
ID: 12533080
Thank you petmagdy!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question