Go Back   Java Programming Forums > Java Standard Edition Programming Help > What's Wrong With My Code?


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-03-2010, 11:09 PM
Junior Member
 

Join Date: Mar 2010
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
straw is on a distinguished road
Default returning a 2D array

hi so fare i have this but it doesnt seem to be working:

in one class i have:
Java Code
public class siwtchMenu{

public String[][] arrayA(int x, int y){
        String arrayTest[][] = {{"0", "1", "2"},{"0","1", "2"}};
        return arrayTest[x][y]; //error is here
    }
}
and then in another class i want to call arrayA:
Java Code
public class case1{

void case1(){
        switchMenu obj = new switchMenu();

        System.out.println("Array is:");

        for(int i=0; i<3; i++){
                System.out.println(obj.arrayTest[i][i]+ ", ");
             }
      }
}
when i do this it comes up with an error (at error is here) saying:

Quote:
incompatible types
required: java.lang.String[][]
found: java.lang.String
Please help me on this as i have know idea what im doing wrong!
thanks in advance! =]




Last edited by straw; 11-03-2010 at 03:38 PM. Reason: miss type
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 05-03-2010, 11:50 PM
copeg's Avatar
Moderator
 
9 Highscores

Join Date: Oct 2009
Posts: 570
Thanks: 7
Thanked 131 Times in 125 Posts
copeg will become famous soon enoughcopeg will become famous soon enough

I'm feeling Sleepy
Default Re: returning a 2D array

For your class SwitchMenu, you have defined the function arrayA to return a 2D array, but you actually just return a String value. There is also an error in your case1 with how you access the first class...you've defined a method in the first class but try to access a variable that does not exist from the case1 method. The solutions depend upon what behavior you want, here's a fix to the first class.

Java Code
public class siwtchMenu{

public String[][] arrayA(int x, int y){
        String arrayTest[][] = {{"0", "1", "2"},{"0","1", "2"}};
        return arrayTest; 
    }
}
Now you can correctly access the arrayA by calling that function and storing the returned 2D array in a local. As another side note, you can define your arrayTest as an object variable rather than redefining the 2D array for every call to arrayA
Reply With Quote
  #3 (permalink)  
Old 06-03-2010, 12:26 AM
Junior Member
 

Join Date: Mar 2010
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
straw is on a distinguished road
Default Re: returning a 2D array

hi thanks for the quick responce, now re the case1 error that was me just being tired and typing it out wrong i didnt do that in my actual project!

when i do as you say above it just comes up with:

Quote:
[[Ljava.lang.String;@130c19b
rather than the array

so how would i go about defining arrayTest as an object variable?
baring in mind that i do want to edit the array latter!
Reply With Quote
  #4 (permalink)  
Old 11-03-2010, 08:30 AM
Faz's Avatar
Faz Faz is offline
Member
 

Join Date: Mar 2010
Posts: 94
Thanks: 5
Thanked 14 Times in 14 Posts
Faz is on a distinguished road

I'm feeling Nerdy
Default Re: returning a 2D array

OK there are two problems I can see here first of all here:
Java Code
public class siwtchMenu{

public String[][] arrayA(int x, int y){
        String arrayTest[][] = {{"0", "1", "2"},{"0","1", "2"}};
        return arrayTest[x][y]; //error is here
    }
}
When you say pulic String[][] in that code you are saying you want to return a 2D array of strings but by saying arrayTest[x][y] you are only returning the string in the position [x][y]. Does that make sense to you? What you need to do is just replace String[][] with String, I believe that should work. However you haven't got the loop logic in case1 correct I think you should be able to figure it out once you get some feedback from the program with that change.

EDIT:

Ah just looked at your code again and maybe I misunderstood what you are trying to do. I assumed you want the loop in case1 to run through and call the method arrayA each time to return the value contained in it. What you are trying to do here :
Java Code
System.out.println(obj.arrayTest[1][1]+ ", ");
is ask for the value of a variable in the class(which doesn't exist as copeg said). If my assumption is right you will want to change this to a method call. Otherwise you could declare the array as a public global variable or use the method to pass the whole array to the case1 class and store it in a new array there and loop through that one. Is any of this making sense? Let us know exactly what you want each method in each class to do.

Last edited by Faz; 11-03-2010 at 08:38 AM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays.equals not returning correct answer. Anyone Collections and Generics 2 11-02-2010 02:12 PM
Object array to int array? rsala004 Collections and Generics 1 30-10-2009 08:09 AM
throwing and returning chronoz13 Exceptions 6 25-10-2009 04:00 AM
Storing an array into an array vluong Collections and Generics 4 22-09-2009 07:14 PM
Returning Random Strings from an Array cfmonster Collections and Generics 3 09-09-2009 04:13 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:57 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.