Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: incompatible types: void cannot be converted to String

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Location
    Pakistan , Islamabad
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default incompatible types: void cannot be converted to String

    I have coded three classes ,1 class containing the Main method.
    I have declared three private data fields in Staff class and two data fields in Address class.
    The problem is that I am assigning String type data field from the Staff and Address Class through the
    public set methods to String type array in a public void method in the Main class.
        public static void main(String[] args){
            // TODO code application logic here
            System.out.println("-------------------------------------------------");
            System.out.println("Press 1 - 6 To Perform Any One Following Action ");
            System.out.println("1. Insert New Contact ");
            System.out.println("2. Delete Contact By Name ");
            System.out.println("3. Replace Info From Contact ");
            System.out.println("4. Search Contact By Name ");
            System.out.println("5. Show All Contacts ");
            System.out.println("6. Exit Program ");
            System.out.println("-------------------------------------------------");
            System.out.println();
            System.out.println("Your Choice :");
     
            AddressBook adBook = new AddressBook();
            Staff staff = new Staff();
            Address adress = new Address();
     
            adBook.openFile();
            adBook.insertRecord(staff.getName(), staff.getDesignation(), staff.getAge(), adress.getAddress(), adress.getMobileNo());
            adBook.closeFile();
        }
     
        java.io.PrintWriter pw;
        static final int rows = 100;
        static final int cols = 5;
        String[][] record = new String[rows][cols];
     
        public AddressBook(){
     
        }
     
        public void openFile(){
          try{
     
               pw = new java.io.PrintWriter("EmployeeRecords.txt");
          } 
          catch(FileNotFoundException e){
     
              System.out.println("Error");
          }
        }
     
        public void insertRecord(String n, String d, String ad, String a, String m){
     
           Staff staf = new Staff();
           System.out.print(record[0][0] = "Name" + "\t");
           System.out.print(record[0][1] = "Designation" + "\t");
           System.out.print(record[0][2] = "Age" + "\t");
           System.out.print(record[0][3] = "Address" + "\t\t"); 
           System.out.print(record[0][4] = "MobileNo" + "\t"); 
     
           for(int r = 1; r < rows; r++){
               for(int c = 0; c < cols; c++){
     
                   record[r][c] = staf.setName(); // I will got error on that assignment stetement
     
               }
           }
     
        }
    class Staff {
        private String name;
        private String designation;
        private String age;
     
        public Staff(){
     
        }
        public void setName(){
     
            Scanner name = new Scanner(System.in);
            name.toString();
        }
        public String getName(){
     
            return name;
        }
        public void setDesignation(){
     
             Scanner designation = new Scanner(System.in);
             designation.toString();
        }
        public String getDesignation(){
     
            return designation;
        }
        public void setAge(){
     
             Scanner age = new Scanner(System.in);
             age.toString();
        }
        public String getAge(){
     
            return age;
        }
    }
     
     class Address {
        private String address;
        private String mobileNo;
     
        public Address(){
     
        }
        public void setAddress(){
     
             Scanner address = new Scanner(System.in);
             address.toString();
        }
        public String getAddress(){
     
            return address;
        }
        public void setMobileNo(){
     
             Scanner mobile = new Scanner(System.in);
             mobile.toString();
        }
        public String getMobileNo(){
     
            return mobileNo;
        }
    }
    Last edited by Nomi; July 26th, 2014 at 07:49 PM. Reason: Mentioning the exact line where error occured


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: incompatible types: void cannot be converted to String

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.
    The problem is that I am assigning String type data field from the Staff and Address Class through the public set methods to String type array in a public void method in the Main class.
    How is this a problem, and what's your question?

    Looking at your code to answer my own question, I see you have one comment in your code (more would be better) to highlight the problem and clarify what you're asking. This line of code;

    record[r][c] = staf.setName();

    assumes (or requires) that the method setName() returns a value that can be assigned to record[r][c], but it does not. If you'd commented your code, you might have described the purpose of setName() as a method that SETS the NAME of the Staff object. Similarly, if you'd commented your code, you might have described the purpose of getName() as a method that RETURNS the NAME of the Staff object.

    Which of those two methods sound more appropriate as the right side of the statement,

    record[r][c] = ??????;

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Location
    Pakistan , Islamabad
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types: void cannot be converted to String

    The data fields i.e name , designation etc were declared as private in Staff & Address class.So, in order to access outside, in Main class I have used public setters & getters. Basically I want to get input from user so i have used Scanner in setters method and return the input from getter method.
    The answer to your question is yes I thought and found getName() method appropriate with record[r][c].But the same error moves to the other line of code.
    To be precise to the problem i will explain the problem exact on which lines of code to sort out. I am just posting the some code of Main class with some updation.
    Do guide me on calling setters methods in insert method and pass it as an argument and assigns it to array.
    Inside setters method the input will be get from user through Scanner.
    I tried my best to give the details of problem i was facing.

    public static void main(String[] args){
    // TODO code application logic here
    System.out.println("-------------------------------------------------");
    System.out.println("Press 1 - 6 To Perform Any One Following Action ");
    System.out.println("1. Insert New Contact ");
    System.out.println("2. Delete Contact By Name ");
    System.out.println("3. Replace Info From Contact ");
    System.out.println("4. Search Contact By Name ");
    System.out.println("5. Show All Contacts ");
    System.out.println("6. Exit Program ");
    System.out.println("-------------------------------------------------");
    System.out.println();
    System.out.println("Your Choice :");
     
    AddressBook adBook = new AddressBook();
    Staff staff = new Staff();
    Address adress = new Address();
     
    adBook.openFile();
    //adBook.insertRecord(staff.getName(), staff.getDesignation(), staff.getAge(), adress.getAddress(), adress.getMobileNo());
    adBook.insertRecord(staff.setName()); //void type not allowed here
    adBook.closeFile();
    }
     
    java.io.PrintWriter pw;
    static final int rows = 100;
    static final int cols = 5;
    String[][] record = new String[rows][cols];
     
    public AddressBook(){
     
    }
     
    public void openFile(){
    try{
     
    pw = new java.io.PrintWriter("EmployeeRecords.txt");
    } 
    catch(FileNotFoundException e){
     
    System.out.println("Error");
    }
    }
     
    public void insertRecord(String s){
     
    //Staff staf = new Staff();
    System.out.print(record[0][0] = "Name" + "\t");
    System.out.print(record[0][1] = "Designation" + "\t");
    System.out.print(record[0][2] = "Age" + "\t");
    System.out.print(record[0][3] = "Address" + "\t\t"); 
    System.out.print(record[0][4] = "MobileNo" + "\t"); 
     
    for(int r = 1; r < rows; r++){
    for(int c = 0; c < cols; c++){
     
    record[r][c] = s ; // if ignoring the above error so setName() method value was passed to insertRecord() method parameter s. So s contains the value So should I have to use getName() 
     
    }
    }
     
    }
    Last edited by Nomi; July 27th, 2014 at 07:35 AM. Reason: Fixed code tags

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: incompatible types: void cannot be converted to String

    I fixed your code tags. Note there is no space around the '=' sign in the opening tag. Please copy properly indented code between the code tags. By properly indented, I mean that not all lines should begin at column 0, similar to your first post.

    It's not clear that you understood my first point, so I'll answer my own question. The statement makes more sense as:

    record[r][c] = staf.getName();

    But I think what you're really trying to do is assign the record[r][c] to the staf's name field, which would look like:

    staf.setName( record[r][c] );

    but that won't work in the for() loop you've constructed.

    I'm not really sure what you're trying to do in the insertRecord() method or what your question is. Can you describe what that method is supposed to do in plain words, not code, and then describe which of those parts you have questions about?

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Location
    Pakistan , Islamabad
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types: void cannot be converted to String

    My Question is that

    I want to read input i.e name etc and print it on Employee.txt file and manage record in array manner.So further on I will retrieve an info from txt file by an index.

    The lines of code which were producing errors and doubtful to me

    1. Error

    adBook.insertRecord(staff.setName());

    What to do with that error. I have to call setter method in order to get input from user.
    Now will you please guide me on these points
    1.Is this a correct place for calling setter method
    2.Void not allowed here

    2. Doubtful

    record[r][c] = staf.getName();

    Yes. I want to assign the staf's name field to this array. I mean to say that the name field should store in a specific row column of this array,
    which would be controlled by an for loop.
    But you have mentioned the for loop won't work.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: incompatible types: void cannot be converted to String

    Please post the updated code with any changes you've made (preferably runnable to demonstrate the problem) and the complete and exact text of the error messages, copied and pasted into a post.

Similar Threads

  1. incompatible types
    By stylelink in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 10th, 2014, 08:10 AM
  2. [SOLVED] incompatible types
    By muhammad93 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 19th, 2013, 02:20 PM
  3. Incompatible types.
    By miller4103 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 12th, 2013, 08:36 PM
  4. incompatible types!!
    By sneha343 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2011, 05:48 PM
  5. incompatible types
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2011, 10:40 AM