wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Incompatible types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incompatible types" Watch "Incompatible types" New topic
Author

Incompatible types

RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Quick background. The essence of this program was mostly a shell. Main would call other methods depending on what the user pressed and all that was required of me was to have that method say "Modify Member was called" as an example.

Now, Ive created arrays in main and am trying to get the Modify Member method to accept a name (eventually will need first name,..last name..etc) but i just want a first name for now. In my head this makes sense but JCreator is telling me that these are incompatible types??? Any advice?

output="Enter the Name";
fname=JOptionPane.showInputDialog(null,
output," ",
JOptionPane.QUESTION_MESSAGE);


return fname;

Here is the WHOLE code.


import javax.swing.JOptionPane;

public class ymcaProject {

public static void main(String[] args) {

start_program();

//Call to the menu method

String SValue;
String[]fname= new String[100];
String[]lname= new String[100];
int[] id = new int [3];
int menuAnswer=(int)Menu();



if(menuAnswer==1)
Modify_Member(fname);
if(menuAnswer==2)
Modify_Enrollment();
if(menuAnswer==3)
Report();
if(menuAnswer==4)
exit_program();





}//end of main method


public static void start_program() {

System.out.println("The start program Method has been called");
}//end of start program member method

//BEGINNING OF MENU METHOD
public static double Menu(){

double menuAnswer;

String output = "The Pittsburgh YMCA"+
"\n" +"1. Add/Modify Member Data";

String menuDataString =JOptionPane.showInputDialog(
"The Pittsburgh YMCA"+
"\n"+
"\n"+"1. Add/Modify Member Data"+
"\n"+"2. Add/Modify Class Enrollments"+
"\n"+"3. Report Section"+
"\n"+"4. Exit the System"+
"\n"+
"\n"+"Please Make Your Selection>"+
"\n");

menuAnswer = Double.parseDouble(menuDataString);
return menuAnswer;
}//end of menu method



public static int Modify_Member(String[]fname) {
String output;
output="Enter the Name";
fname=JOptionPane.showInputDialog(null,
output," ",
JOptionPane.QUESTION_MESSAGE);


return fname;


}//end of modify member method


public static void Modify_Enrollment() {
System.out.println("The Modify Enrollment Method has been called");
}//end of modify enrollment method

public static void Report() {
System.out.println("The Report Method has been called");
}//end of Report method

public static void exit_program() {
System.out.println("The exit program Method has been called");
}//end of exit method


}//end of public class ymcaProject
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You are trying to assign a single String to a String[] reference. Those are incompatible types - a single value of a type is not compatible to an array of the same type.

Does that make sense?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Makes sense but the only reason its like that is if i use [] brackets with fname i get "another" error.
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
didnt want to add a new topic BUT.......new problem. Im only trying to get "first name" or fname as i called it to print. To do this, the menu selection 1 should be hit which causes it to ask you for the first name. Then its passed back to main. My question is. After the compiler says "process completed", is that it? Do i have to find some way to keep it going so that name i enter "stays" in its element?

As i have it now,..when call up the program and the menu comes up, if I hit 1,...enter a name...hit 0 to stop that (so it doesnt keep going, i have to keep track of names) it returns control to main. I have it coded now so if you hit 3 it goes to the report method. Does that name stay???


import javax.swing.JOptionPane;

public class ymcaProject {

public static void main(String[] args) {

int count=-1;
start_program();
int i;
//Call to the menu method






int menuAnswer=(int)Menu();
String[]fname= new String[100];


if(menuAnswer==1)
count=Modify_Member(fname);
if(menuAnswer==2)
Modify_Enrollment();
if(menuAnswer==3)
Report(fname);
if(menuAnswer==4)
exit_program();





}//end of main method


public static void start_program() {

System.out.println("The start program Method has been called");
}//end of start program member method






//BEGINNING OF MENU METHOD
public static double Menu(){

double menuAnswer;

String output = "The Pittsburgh YMCA"+
"\n" +"1. Add/Modify Member Data";

String menuDataString =JOptionPane.showInputDialog(
"The Pittsburgh YMCA"+
"\n"+
"\n"+"1. Add/Modify Member Data"+
"\n"+"2. Add/Modify Class Enrollments"+
"\n"+"3. Report Section"+
"\n"+"4. Exit the System"+
"\n"+
"\n"+"Please Make Your Selection>"+
"\n");

menuAnswer = Double.parseDouble(menuDataString);
return menuAnswer;
}//end of menu method





//being modify member method
public static int Modify_Member(String[]fname) {
int count=-1;
int continueit;
String svalue;
String output="Enter 1 to continue, 0 to stop";
svalue=JOptionPane.showInputDialog(null,
output,"",
JOptionPane.QUESTION_MESSAGE );
continueit=Integer.parseInt(svalue);
while(continueit==1)
{
count=count+1;
output="Enter First Name";
fname[count]=JOptionPane.showInputDialog(null,
output," ",
JOptionPane.QUESTION_MESSAGE);
output="Enter 1 to continue 0, to stop";
svalue=JOptionPane.showInputDialog(null,
output," ",
JOptionPane.QUESTION_MESSAGE);
continueit=Integer.parseInt(svalue);

}//end while loop

return count;




}//end of modify member method









public static void Modify_Enrollment() {
System.out.println("The Modify Enrollment Method has been called");
}//end of modify enrollment method

public static void Report(String[]fname) {
System.out.println(fname[1]);


}//end of Report method

public static void exit_program() {
System.out.println("The exit program Method has been called");
}//end of exit method


}//end of public class ymcaProject
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Look at your program flow...

In main, you are calling Menu() and assigning the return value to menuAnswer. If that return value is 1, then Modify_Member(fname) is called.

Within Modify_Member, you could enter up to 100 names (the size of the fname array). When you're done entering names, then Modify_Member returns an int that's one less than the number of names entered. This value is assigned to the variable "count."

At that point, main is finished (there's no more code to execute), and so the program ends.

If you wanted to print the values of fname, then you would need to add some code. For example...

But I think what you really want is for the menu to appear again so that you can enter "3" for "Report." One way is to repeat your code...

But I think you can see that this approach isn't very practical. So you might consider moving all the "menu" code into a separate method that you can call as needed.

(Also note that fname[1] is actually the second name in the array, because array indexes start at 0.)
[ March 24, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Marc, thanks. If i were just going back to main, that would be great. But the menu option has to work. I have to be able to hit a (1) to "modify member" or a (3) (the only other one im working on at this time) and print out a report, which will eventually show first name, last name.....etc.

My problem as I see it is how does whatever i enter in as a first name stay there so when its run again and I hit a 3 for report, that name will print out?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I was just adding to my post as you were replying. See if it helps now.
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
What do you think about putting a while loop around main so it keeps running the menu even after other options are picked? I think im gonna try that.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by RJ Cavender:
What do you think about putting a while loop around main so it keeps running the menu even after other options are picked? I think im gonna try that.

I think putting a while loop inside (not around) main would be a good thing for you to experiment with. You should be able to get the behavior you want this way.

But from a more general perspective, I think you should consider using main only as the program's entry point, and avoid filling main with procedural code. In other words, look at this class as an object with "state" and "behavior." That is, what should it have in terms of fields? And what should it do in terms of methods?

As I mentioned above, a big chunk of code you have in main calls Menu and then handles the return value. This is a behavior: Asking the user what they want to do, and then handling the response. Once you define this as a separate method, then you can call it from your other code whenever needed.

In terms of state, it might make sense for fnames to be defined as an instance variable (a property of your object) instead of just a local variable inside main.

(To help you start thinking of this as an object, you might give this class a more descriptive name. It seems like this is for modifying data, so -- for example -- you might call it "AdminEditor" instead of "ymcaProject." Then you can start thinking in terms of what an AdminEditor should "have" and what it should "do.")
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Marc,
One quick question. First, everything up to now is working. Im trying to set up an array for gender using char[]gender={'M','F'}; The syntax, at least from what i read makes sense. Am i missing a cardinal rule or something? Can you not pass a char to a method? Im getting a "array required, but char found" error.




EDIT by mw: Added Code Tags.
[ March 26, 2007: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As for the error message, it looks like you just forgot the brackets in your agrument list for Modify_Member, so "gender" is defined as a char instead of a char[]. BUT...

Now that you've defined fname, lname, gender, and count as instance variables (instead of being local to the main method), their scope is the entire class. Therefore, any method in the class can access them, and there's no need to pass these as parameters.

Once that detail is fixed...

The method JOptionPane.showInputDialog returns a String, and you're trying to assign this String to a char reference with the line gender[count] = JOptionPane.showInputDialog... So you need a way to get a char out of that String. A quick-and-dirty way to do this to just take the first char using String's charAt method -- something like myString.charAt(0).

Finally, here's a big issue to consider: You've defined "gender" as an array with a fixed size of 2. gender[0] is initialized to 'M' and gender[1] is initialized to 'F'. Is that really what you want? As soon as your "count" gets above 1, you will get a ArrayOutOfBoundsException when you try to assign a char to gender[2].

(PS: Notice how much better code looks with Code Tags? )
[ March 26, 2007: Message edited by: marc weber ]
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
ahhhhh! CODE tags. Thank you. That DOES look better. Im gonna use them on a small part of my program so I dont waste your time in the rest of it. Ive bailed on gender in the program for now (and yes, im told i have to use F or M, or maybe im reading that wrong, maybe its just defining as a char. Heres the issue now. Im trying to get membership fee to work. Ive defined it in main as a double and passed it to modify_member.

In my head, asking for membership fee and using something else like memFee (a string) and then doing a double.Parsedouble, making memFee = my array of mFee should work. Then why am I getting a "class expected" error??

import javax.swing.JOptionPane;


public class ymcaProject {


public static void main(String[] args) {

int count=1;
int i;
String []fname = new String[100];
String []lname = new String[100];
double []mFee = new double [100];
String []memFee = new String[100];






start_program();

//Call to the menu method

int menuAnswer=(int)Menu();

while(menuAnswer !=4)
{

if(menuAnswer==1)
Modify_Member(fname,lname,count,memFee,mFee);
if(menuAnswer==2)
Modify_Enrollment();
if(menuAnswer==3)
Report(fname,lname,count);
menuAnswer=(int)Menu();

}
exit_program();
System.exit(0);



}//end of main method


public static void start_program() {

System.out.println("The start program Method has been called");
}//end of start program member method

//BEGINNING OF MENU METHOD
public static double Menu(){


double menuAnswer;

String output = "The Pittsburgh YMCA"+
"\n" +"1. Add/Modify Member Data";

String menuDataString =JOptionPane.showInputDialog(
"The Pittsburgh YMCA"+
"\n"+
"\n"+"1. Add/Modify Member Data"+
"\n"+"2. Add/Modify Class Enrollments"+
"\n"+"3. Report Section"+
"\n"+"4. Exit the System"+
"\n"+
"\n"+"Please Make Your Selection>"+
"\n");


menuAnswer = Double.parseDouble(menuDataString);

return menuAnswer;
}//end of menu method












public static void Modify_Member(String[]fname,String[]lname,int count,
String []memFee, double[] mFee) {

int continueit;
String svalue;
String output="Enter First Name";


fname[count]= JOptionPane.showInputDialog(null,
output," ",
JOptionPane.QUESTION_MESSAGE);

output="Enter Last Name";
lname[count]=JOptionPane.showInputDialog (null,
output," ",
JOptionPane.QUESTION_MESSAGE);








output="Enter 1 to continue, 0 to stop";
svalue=JOptionPane.showInputDialog(null,
output," ",
JOptionPane.QUESTION_MESSAGE);
continueit=Integer.parseInt(svalue);


}//end of modify member method




RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
never mind,..i got it. (i HAVE to learn to relax and just think)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by RJ Cavender:
never mind,..i got it. (i HAVE to learn to relax and just think)...

Yup, that helps.

Using 'M' and 'F' for gender is fine. The problem is that your array can only hold 2 elements. So instead of creating (and initializing) your array with char[] gender = {'M', 'F'}; (which sets the size at 2), you probably want to just create an "empty" array with a size corresponding to your other arrays: char[] gender = new char[100]; Then you can fill it with up to 100 'M' or 'F' chars. Does that make sense?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by marc weber:
...Now that you've defined fname, lname, gender, and count as instance variables (instead of being local to the main method), their scope is the entire class. Therefore, any method in the class can access them, and there's no need to pass these as parameters...

For example...
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
One question. You say;

"Now that you've defined fname, lname, gender, and count as instance variables (instead of being local to the main method), their scope is the entire class. Therefore, any method in the class can access them, and there's no need to pass these as parameters".

I understand what you mean BUT....if i dont pass those (lname,fname..etc) to the other methods, I will get errors that they are not defined. I guess I dont understand what you mean that there's no need to pass them when clearly if I leave them out, i get an error message because of that.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Whoops, my mistake. I thought you had moved these variables outside of the main method, but I see I misread it. (I'm going to blame this on the lack of Code Tag indentation. )

I thought you had this...

...instead of this...
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
DAMMIT!!! I keep forgetting. Well, i AM a Java greenhorn after all. You're more familiar with the customs here, if i have any more questions, tag them onto this post or start a new one?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by RJ Cavender:
...if i have any more questions, tag them onto this post or start a new one?

If the questions are in direct response to something in this thread, I would continue here. Otherwise, I would start a new topic with a descriptive subject line focusing on the new question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Incompatible types