Results 1 to 4 of 4
  1. #1
    Chip is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default ArrayList problem, cant call method from object in array.

    Hello, I started taking a Java class earlier this year and figured I had learned enough to venture out onto my own and create a program outside the learned scope of my class. (this is also my first programming language).

    NOTE: this is NOT a class project, this is purely on my own, I am not submitting this for any type of credit whatsoever.

    I have taken Japanese classes in the past and figured I would try to recreate a program that tests the user on their knowledge of the kana, with the intention of later expanding it to include kanji and verb/adjective/noun conjugations.

    So with my copy of the comprehensive version of "Introduction to Java programming" by Y. Daniel Liang in my hands and my youthful vigor I figured I could work myself out of any problems I might encounter.

    And until now I have been successful of working myself out of the holes. I have been working on different parts of the programs in different steps, I have a program that has the GUI, but I wanted to work on the program innards using the terminal and get everything working in what I am familiar with before trying to put it into the GUI (we are just now working on the GUI in class).

    Right now I am working on the Kana portion of the program and decided to start small, with only 5 kana and get it fully functional with those 5 and then simply expand it.

    Now, in my java class we never touched on the ArrayList Class, instead being told to use Arrays of Objects, (but since I intend to randomize the order of which the kana are presented, I wanted to remove the objects from the array as the questions are answered, and to my very limited knowledge of the subject, it seemed that an ArrayList would be better suited for this.(besides I always want to try something not covered in class!)).

    So anyway, my problem is that, I can create the arrayList, populate it with my 5 kana, and get those kana to show up on the terminal (in order, i'm not randomizing yet!, one step at a time) and even input a response, but when I attempt to get the latin spelling of the kana (in the form of a string that is a variable on the object kana itself) to compare with the user's input I cant seem to figure out how to call a method from the kana using the arrayList. I have tried everything I can think of, I have tried different ways to getting at the bugger but alas, I am failing, and finally I was forced to admit that I may have overstepped my capabilities at this time.

    My error message is this;
    ChipJapaneseKana.java:117: cannot find symbol
    symbol : method getKanaRomaji()
    location: class java.lang.Object
    else if (reply.equals(hiraganaList.get(indexMarker).getKan aRomaji())) {
    ^
    Note: ChipJapaneseKana.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error


    My code is as follows;



    Java Code:
    import java.util.*;
    
    public class ChipJapaneseKana{
    	public static void main(String[] args) {
    	
    		Scanner input = new Scanner(System.in);
    		
    		//create array list to store kana
    		ArrayList hiraganaList = new ArrayList();
    		
    		createHiragana(hiraganaList);
    		
    		 boolean proceed = startTheFun(hiraganaList);
    		
    		//displayHiraganaList(hiraganaList);
    		
    		
    		if (proceed == true) {
    			
    			startTest(hiraganaList);
    		}//if true
    		else {
    			System.out.println("byebye!");
    			System.exit(0);
    		}//if false
    	
    	}//main
    	
    	public static void createHiragana(ArrayList hiraganaList) {
    		Hiragana a = new Hiragana("あ", "a");
    		hiraganaList.add(a);
    		Hiragana i = new Hiragana("い", "i");
    		hiraganaList.add(i);
    		Hiragana u = new Hiragana("う", "u");
    		hiraganaList.add(u);
    		Hiragana e = new Hiragana("え", "e");
    		hiraganaList.add(e);
    		Hiragana o = new Hiragana("お", "o");
    		hiraganaList.add(o);
    	}//Create Hiragana
    	
    	public static void displayHiraganaList(ArrayList hiraganaList){
    		System.out.println(hiraganaList.toString());
    	}//display hiragana list to console
    	
    	
    	
    	//begin interaction with person
    	public static boolean startTheFun(ArrayList hiraganaList){
    		Scanner input = new Scanner(System.in);
    		
    		String reply = "no reply yet";
    		
    		System.out.println("ready to begin? \n1.Yes");
    		//input.nextLine();
    		reply = input.nextLine();
    		return checkReply(reply);
    		
    	}//start thefun
    	
    	
    	//checks the reply from the user on whether to continue
    	public static boolean checkReply(String reply){
    		if (reply.equals("1")){
    			System.out.println("I made it past the if barrier!");
    			return true;
    			}//if
    		else{
    			System.out.println("I did not make it past the if barrier!");
    			return false;
    			}//else
    	}//check reply in Start the fun method
    	
    	
    	
    	public static void startTest(ArrayList hiraganaList){
    		Scanner input = new Scanner(System.in);
    		
    		int attempts = 0;
    		int correct = 0;
    		int indexMarker = 0;
    		String reply = "no reply yet";
    		Object kanaReference;
    		
    		System.out.println("\n\nThe kana will be displayed, you are to type the Romaji form of the kana, \n\n");
    
    		for (int i = 0; i < hiraganaList.size(); i++){
    		
    			//I was trying different ways to get the method to be called with
    			//these two methods, I could not get either to work.
    			indexMarker = getIndexMarker(hiraganaList, indexMarker);
    			kanaReference = randomKana(hiraganaList, indexMarker);
    			System.out.println(kanaReference);//this works
    			
    			
    			System.out.println("\n\nto exit, type stop");
    			reply = input.nextLine();
    			
    			//reason for so many variables bein passed, trying everything with
    			//no luck
    			correct = checkReply(hiraganaList, indexMarker ,correct, reply);
    				
    				
    			//ADD CHECKER for reply to see if reply matches given kana, this will RETURN int correct which will then be used by the result checker
    			attempts++;
    			indexMarker++;
    			outputResults(attempts, correct);
    		}//for loop
    		
    	}//start test
    	
    ***********************	//PROBLEM is in  'this' checkReply method************************************
    	
    	public static int checkReply(ArrayList hiraganaList, int indexMarker, int correct, String reply){
    		if (reply.equals("stop"))
    			System.exit(0);
    		else if (reply.equals(hiraganaList.get(indexMarker).getKanaRomaji())) {
    			correct++;
    			System.out.println("Correct!");
    		}//else if
    		else
    	
    			System.out.println("incorrect");
    		return correct;
    	}//reply checker	
    	
    	public static int getIndexMarker(ArrayList hiraganaList, int indexMarker){
    		indexMarker = indexMarker; //later random device here
    		return indexMarker;
    	}//get index marker
    	
    	
    	
    	public static void outputResults(int attempts, int correct){
    		System.out.println("Results are: \n\nAttempts: " + attempts + "\nCorrect: " + correct + "\nPercentage correct: " + (correct/attempts)*100 + "%");
    	}//output results
    	
    	
    	//not yet random
    	public static Object randomKana(ArrayList hiraganaList, int indexMarker){
    			
    		return hiraganaList.get(indexMarker);
    	
    	}//randomizes a kana and returns it
    	
    	
    }//ChipJapaneseKana
    
    
    class Hiragana {
    
    	//attributes
    	public String kanaChar = "default kanaChar";
    	public static String kanaRomaji = "default kanaRomaji";
    	
    	//constructors
    	
    	public Hiragana(){
    	}//default constructor
    	public Hiragana(String kana, String romaji){
    		kanaChar = kana;
    		kanaRomaji = romaji;
    	}//constructor with kana and romaji input
    
    
    //methods
    	public static String getKanaRomaji(){
    		return kanaRomaji;
    	}//getKanaRomaji
    	
    //tostring
    	public String toString(){
    		return "kana: " + kanaChar;/*for this part only want char to show + "\nromaji: " + kanaRomaji + "\n\n";*/
    	}//to string
    }//hiragana class






    anyway, Thankyou very much!

    -Chip

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: ArrayList problem, cant call method from object in array.

    Try breaking that compound method call on line 117 up into separate statements with one call per statement.
    That will help you isolate what datatype each method returns. Then knowing the datatype of the object, you will be able to call the methods for that object or cast that object to the datatype you need.

    Another thing to do would be to use Generics to define the arraylist to hold the dataypes you are putting into it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Chip is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: ArrayList problem, cant call method from object in array.

    Thankyou very much for your response!!

    I tried doing what you suggested, but with my limited expertise I managed to not have success ;) I will keep trying as I think of other ways to break it apart.

    I tried breaking the method call up by trying to isolate the object by assigning the new object 'kanaObject' to hold the reference to the object stored in the array and then using that reference to call the kana's method with the 'objectreference'.'method()' path (the only way I know at this time)

    but that is where I run into the problem, I am thinking that maybe it is not storing the string in the array (that is one way I interpreted your reply) and so have begun to learn about Generics and hopefully I can utilize that and figure out where I am going wrong.

    this is the new error message;
    ChipJapaneseKana.java:116: cannot find symbol
    symbol : method getKanaRomaji()
    location: class java.lang.Object
    kanaObject.getKanaRomaji();
    ^
    Note: ChipJapaneseKana.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error



    This is the code I attempted


    Java Code:
    	public static int checkReply(ArrayList hiraganaList, int indexMarker, int correct, String reply){
    		[B]Object kanaObject = hiraganaList.get(indexMarker);
    		kanaObject.getKanaRomaji();[/B]
    		
    		if (reply.equals("stop"))
    			System.exit(0);
    		[B]else if  (reply.equals(kanaObject)){[/B]
    		//else if (reply.equals(hiraganaList.get(indexMarker).getKanaRomaji())) {
    			correct++;
    			System.out.println("Correct!");
    		}//else if
    		else
    	
    			System.out.println("incorrect");
    		return correct;
    	}//reply checker




    Again, Thankyou very much for the reply!!

    I am looking forward to researching Generics!

    -Chip

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: ArrayList problem, cant call method from object in array.

    If you don't use Generics to define the arraylist, then you will have to cast the object you get from the arraylist to its correct type so that you can call its methods. The error message says you have an instance of the Object class which does not have the method you are trying to call.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. read or call an array from other class an method
    By ki_ha1984 in forum New To Java
    Replies: 5
    Last Post: 01-23-2012, 10:42 PM
  2. Replies: 1
    Last Post: 11-29-2011, 01:30 PM
  3. Method call problem.
    By Lufc in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 09:00 PM
  4. Replies: 2
    Last Post: 10-28-2010, 02:33 PM
  5. Replies: 3
    Last Post: 01-29-2009, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •