1 Replies - 164 Views - Last Post: 06 October 2012 - 01:50 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:12 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

This program is suppose to take the information from the file, read it, and produce reports to a file and a console according to the results of Male/Female & married/single students as well as an eligible bachelors report.


[
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: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

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

Posted 06 October 2012 - 01:50 PM

Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1