6 Replies - 2189 Views - Last Post: 06 October 2012 - 01:46 PM Rate Topic: -----

#1 dmgracie901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-October 12

Java: Incompatible operand types String and char/ syntax error

Posted 06 October 2012 - 12:17 PM

Hi All,
I am trying to get this code to work and I can't seem to figure out what I'm doing wrong. i understand that I am trying to compare the string data in this text file that I've imported/attached to this program but it can't seem to go through. I originally had the code as ex : If maritalStatus == 'M' and realized this is wrong but now that I've changed it over it's still throwing this error message " Incompatible operand types String and char as well as Syntax Error for my IF/Else statements

The code I have is below and the txt file that I have imported to my program is labeled StudentFile.txt and has three rows of information in the following order: name, gender, age, marital status
Ex: Mick Jagger M 22 S


import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

public class StudentFile {
    public static void main(String[] args) throws Exception {
        // Declare and initialize variables
        String studentSummary = "Student Summary Report";
        String eligibleBachelorsReport = "Eligible Bachelors Report";

        // input record
        String firstName;
        String lastName;
        String gender;
        int age;
        String maritalStatus;

        // counters
        int marriedMen = 0;
        int singleMen = 0;
        int marriedWomen = 0;
        int singleWomen = 0;

        // create studentInput to read from StudentFile.txt
        File inputFile = new File("StudentFile.txt");
        Scanner input = new Scanner(inputFile);

        while (input.hasNext()) {
            // read name, gender, age, maritalStatus
            firstName = input.next();
            lastName = input.next();
            gender = input.next();
            age = input.nextInt();
            maritalStatus = input.next();


            if (gender.equals(F)){
            }if maritalStatus.equals(M)){
                marriedWomen = marriedWomen ++;
                } else {
                singleWomen = singleWomen ++;
            }else{
                }if maritalStatus.equals(M)){
                marriedMen = marriedMen ++
            }else{
                singleMen = singleMen ++
                if age > 30 {
                    eligibleBachelorsReport += ""firstName + ""lastName
    }
            System.out.println(firstName + " " + lastName + " " + gender + " " + age + " "
                    + maritalStatus);
        }

        // write studentSummary, eligibleBachelorsReport to StudentReport.txt
        PrintWriter outputFile = new PrintWriter("StudentReport.txt");
        outputFile.println(studentSummary);
        outputFile.println(eligibleBachelorsReport);

        // write studentSummary, eligibleBachelorsReport to the console
        System.out.println(studentSummary);
        System.out.println(eligibleBachelorsReport);

        input.close();
        outputFile.close();
 


Is This A Good Question/Topic? 0
  • +

Replies To: Java: Incompatible operand types String and char/ syntax error

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10770
  • View blog
  • Posts: 40,101
  • Joined: 27-December 08

Re: Java: Incompatible operand types String and char/ syntax error

Posted 06 October 2012 - 01:25 PM

Please post your syntax errors exactly as they are presented to you.
Was This Post Helpful? 0
  • +
  • -

#3 dmgracie901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-October 12

Re: Java: Incompatible operand types String and char/ syntax error

Posted 06 October 2012 - 01:36 PM

I actually got it to work, I had errors in the way I compared my strings (needed to be .equals ()) and keep my IF statements in (). Now I'm working on getting my console to print the correct info but that's for another thread. thank you!


import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

    public class StudentFile {
         public static void main(String[] args) throws Exception {
            // Declare and initialize variables
            String studentSummary = "Student Summary Report";
            String eligibleBachelorsReport = "Eligible Bachelors Report";

            // input record
            String firstName;
            String lastName;
            String gender;
            int age;
            String maritalStatus;

            // counters
            int marriedMen = 0;
            int singleMen = 0;
            int marriedWomen = 0;
            int singleWomen = 0;

            // create studentInput to read from StudentFile.txt
            File inputFile = new File("StudentFile.txt");
            Scanner input = new Scanner(inputFile);

            while (input.hasNext()) {
                // read name, gender, age, maritalStatus
                firstName = input.next();
                lastName = input.next();
                gender = input.next();
                age = input.nextInt();
                maritalStatus = input.next();
               
   
                if (gender.equals("F"))
                {
                	if(maritalStatus.equals("M"))
                	{
                    marriedWomen = marriedWomen ++;
                    }
                	
                    else {
                       singleWomen = singleWomen ++;
                     }
                }
                else if (maritalStatus.equals("M")){
                    marriedMen = marriedMen++;
                }else{
                    singleMen = singleMen++;
                }
                
                    if( age > 30) {
                        eligibleBachelorsReport += " "+firstName + " "+lastName;
         }
                System.out.println(firstName + " " + lastName + " " + gender + " " + age + " "
                        + maritalStatus);
            }

            // write studentSummary, eligibleBachelorsReport to StudentReport.txt
            PrintWriter outputFile = new PrintWriter("StudentReport.txt");
            outputFile.println(studentSummary);
            outputFile.println(eligibleBachelorsReport);

            // write studentSummary, eligibleBachelorsReport to the console
            System.out.println(studentSummary);
            System.out.println(eligibleBachelorsReport);
            JOptionPane.showMessageDialog(null, outputFile);

            input.close();
            outputFile.close();
         }}





Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10770
  • View blog
  • Posts: 40,101
  • Joined: 27-December 08

Re: Java: Incompatible operand types String and char/ syntax error

Posted 06 October 2012 - 01:36 PM

Feel free to continue on this thread. :)
Was This Post Helpful? 0
  • +
  • -

#5 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Java: Incompatible operand types String and char/ syntax error

Posted 06 October 2012 - 01:38 PM

Where are you getting the variable M, and F in your if else from, they don't appear to be declared anywhere, also your if else, is wrong. Assuming you properly declared M, and F somewhere that I don't see this is how your if else should look. This is also assuming that M means married, and F means female.
if(gender.equals("F") && maritalStatus.equals("M")){
		marriedWoman++;
	} else if(gender.equals("F") && !maritalStatus.equals("M")){
		singleWoman++;
	} else if(!gender.equals("F") && maritalStatus.equals("M")){
		marriedMen++;
	} else singleMen++;


This post has been edited by Ytry: 06 October 2012 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#6 dmgracie901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-October 12

Re: Java: Incompatible operand types String and char/ syntax error

Posted 06 October 2012 - 01:41 PM

Hi, i've been working on this code for a few days and I think I'm almost done. The only thing I can't seem to figure out is how to get my console to print out the correct information. this program is suppose to print to a the file and the console, right now only the file portion works. the console pops up so I get no errors but it just says

java.io.PrintWriter

The code is below and a txt file is attached to my prgram, when I try to add outputFile as a variable or save it to a string I get a compile error and the program will terminate. Any pointers? thank you!

import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

    public class StudentFile {
         public static void main(String[] args) throws Exception {
            // Declare and initialize variables
            String studentSummary = "Student Summary Report";
            String eligibleBachelorsReport = "Eligible Bachelors Report";

            // input record
            String firstName;
            String lastName;
            String gender;
            int age;
            String maritalStatus;

            // counters
            int marriedMen = 0;
            int singleMen = 0;
            int marriedWomen = 0;
            int singleWomen = 0;

            // create studentInput to read from StudentFile.txt
            File inputFile = new File("StudentFile.txt");
            Scanner input = new Scanner(inputFile);

            while (input.hasNext()) {
                // read name, gender, age, maritalStatus
                firstName = input.next();
                lastName = input.next();
                gender = input.next();
                age = input.nextInt();
                maritalStatus = input.next();
               
   
                if (gender.equals("F"))
                {
                	if(maritalStatus.equals("M"))
                	{
                    marriedWomen = marriedWomen ++;
                    }
                	
                    else {
                       singleWomen = singleWomen ++;
                     }
                }
                else if (maritalStatus.equals("M")){
                    marriedMen = marriedMen++;
                }else{
                    singleMen = singleMen++;
                }
                
                    if( age > 30) {
                        eligibleBachelorsReport += " "+firstName + " "+lastName;
         }
                System.out.println(firstName + " " + lastName + " " + gender + " " + age + " "
                        + maritalStatus);
            }

            // write studentSummary, eligibleBachelorsReport to StudentReport.txt
            PrintWriter outputFile = new PrintWriter("StudentReport.txt");
            outputFile.println(studentSummary);
            outputFile.println(eligibleBachelorsReport);

            // write studentSummary, eligibleBachelorsReport to the console
            System.out.println(studentSummary);
            System.out.println(eligibleBachelorsReport);
            JOptionPane.showMessageDialog(null, outputFile);

            input.close();
            outputFile.close();
         }}




Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10770
  • View blog
  • Posts: 40,101
  • Joined: 27-December 08

Re: Java: Incompatible operand types String and char/ syntax error

Posted 06 October 2012 - 01:46 PM

Related threads merged. You can just tag on since the problems are close enough and it's the same project.

Quote

JOptionPane.showMessageDialog(null, outputFile);

See this line of code. Remember that outputFile is your PrintWriter, not your information. You want to output the String information, not the PrintWriter.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1