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 5 of 5

Thread: FileReader argument, compiler problem

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default FileReader argument, compiler problem

    I am using JGrasp.

    Here are the two compiler problems:


    Employee_pay_increases.java:22: error: ')' expected
    			Scanner inFile = new Scanner( new FileReader(C:\\Ch3_Ex7Data.txt));
    			                                              ^
    Employee_pay_increases.java:22: error: illegal character: \92
    			Scanner inFile = new Scanner( new FileReader(C:\\Ch3_Ex7Data.txt));
                                                                                              ^

    I haven't finished the program yet, I just wanted to make sure I was getting the right data from the file first. Here is my code:

     
    import java.util.*;
    import java.io.*;
     
     
    	public class Employee_pay_increases
    	{
    		static final float E1_PAY_INCREASE = .05;
    		static final float E2_PAY_INCREASE = .06;
    		static final float E3_PAY_INCREASE = .061;
     
    		public static void main(Strings[] args) 
    							throws FileNotFoundException
    		{
    			String employeeOne, employeeTwo, employeeThree, e1FirstName, e1LastName, e2FirstName, e2LastName,
                            e3FirstName, e3LastName, e1Salary, e2Salary, e3Salary, e1FinalSal, e2FinalSal, e3FinalSal;
     
    			double employee1Sal, employee2Sal, employee3Sal, employee1Final, employee2Final, employee3Final;
     
    			Scanner inFile = new Scanner( new FileReader(C:\\Ch3_Ex7Data.txt));
     
    			employeeOne = inFile.nextln();
    			employeeTwo = inFile.nextln();
    			employeeThree = inFile.nextln();
     
    			e1FirstName = String substring(0, 5);
    			e1LastName = String substring(7, 12);
     
    			System out.println(employeeOne);
    			System out.println(e1FirstName);
    			System out.println(e1LastName);
     
    		}
    	}

    thanks in advance!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: FileReader argument, compiler problem

    Scanner inFile = new Scanner( new FileReader(C:\\Ch3_Ex7Data.txt));

    See the API for FileReader - I presume you wish to use the String constructor (C:\\...etc... has not quotes to define it as a String)

  3. #3
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: FileReader argument, compiler problem

    I'm surprised that there are so few errors in the debug. I say this due to there being many more errors in your code than just those two.

    1. The first issue I see is that the numbers assigned to your final float variables are missing an f at the end.

    2. There is no built-in class named Strings unless you have built it yourself, this will prevent the program from running.

    3. The argument type for FileReader needs to be a string, therefore you will need quotes. I am basing that off of how you typed it in.

    4. Where you have inFile.nextln() will not work as nextln() is not a method. What you are looking for is nextLine();

    5. What you are trying to set elFirstName and elLastName to is invalid. The statement String substring() will return absolutely nothing. The proper way to use the substring method would be as follows [String Object].substring(x, y); . So, you need to figure out which string object you want to extract from (probably employeeOne, employeeTwo...etc) and then use that objects substring method to extract the information you require.


    That's what I can see at first glance. My final suggestion to you is change which IDE you use as jGrasp isn't very helpful when it comes to error correction (Although I haven't used that specific IDE very much), and I recommend eclipse basic as it's error highlighting on the fly is quite nice+helpful. Changing IDE's is definitely not necessary but I think it would help out.

    I do hope this helped.

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: FileReader argument, compiler problem

    Thanks you were both correct. Thanks for the added insight. I tried to compile before I read your post. I managed to comb through and get all that corrected though. I have used eclipse, but for class we have to use JGrasp. I think it is because if you can do good in JGrasp eclipse is easier, do to the errors and the auto completion.


    I have another question in regards to this program. I have finished it and it works. I tested the output printing it out its fine, but when outputted to a file
    the new line character doesn't work. Here is the whole program:
    import java.util.*;
    import java.io.*;
     
     
    	public class Employee_pay_increases
    	{
     
     
    		public static void main(String[] args) 
    							throws FileNotFoundException
    		{
    			String employeeOne, employeeTwo, employeeThree, e1FirstName, e1LastName, e2FirstName, e2LastName, e3FirstName, e3LastName,
    			e1Salary, e2Salary, e3Salary, e1FinalSal, e2FinalSal, e3FinalSal, e1Increase, e2Increase, e3Increase, outPut;
     
    			double employee1Sal, employee2Sal, employee3Sal, employee1Final, employee2Final, employee3Final, employee1Increase, employee2Increase, employee3Increase;
     
    			Scanner inFile = new Scanner( new FileReader("C:\\Ch3_Ex7Data.txt"));
     
    			employeeOne = inFile.nextLine();
    			employeeTwo = inFile.nextLine();
    			employeeThree = inFile.nextLine();
     
    			e1LastName = employeeOne.substring(0, 6);
    			e1FirstName = employeeOne.substring(7, 13);
    			e1Salary = employeeOne.substring(14, 22);
    			e1Increase = employeeOne.substring(23, 24);
     
     
    			//System.out.println(employeeOne);
    			//System.out.println(e1FirstName);
    			//System.out.println(e1LastName);
    			//System.out.println(e1Salary);
    			//System.out.println(e1Increase);
     
    			e2LastName = employeeTwo.substring(0, 5);
    			e2FirstName = employeeTwo.substring(6, 12);
    			e2Salary = employeeTwo.substring(13, 21);
    			e2Increase = employeeTwo.substring(22,23);
     
    			e3LastName = employeeThree.substring(0, 5);
    			e3FirstName = employeeThree.substring(6, 10);
    			e3Salary = employeeThree.substring(11, 19);
    			e3Increase = employeeThree.substring(20, 23);
     
    			employee1Sal = Double.parseDouble(e1Salary);
    			employee1Increase = Double.parseDouble(e1Increase);
    			employee1Final = employee1Sal * (employee1Increase / 100);
     
    			employee2Sal = Double.parseDouble(e2Salary);
    			employee2Increase = Double.parseDouble(e2Increase);
    			employee2Final = employee2Sal * (employee2Increase / 100);
     
    			employee3Sal = Double.parseDouble(e3Salary);
    			employee3Increase = Double.parseDouble(e3Increase);
    			employee3Final = employee3Sal * (employee3Increase / 100);
     
    			PrintWriter outFile = new PrintWriter("C:\\users\\remo\\downloads\\Ch3_Ex7Output.txt");
     
     
    			outPut = e1FirstName + " " + e1LastName + " " + String.format("%.2f", employee1Final) + "\n" +
    			         e2FirstName + " " + e2LastName + " " + String.format("%.2f", employee2Final) + "\n" +
    						e3FirstName + " " + e3LastName + " " + String.format("%.2f", employee3Final) + "\n";
     
    			System.out.printf(outPut);			
    			outFile.printf(outPut);
     
    			inFile.close();
    			outFile.close();			
     
    		}
    }

    Here is the printed output:
    Andrew Miller 3289.49
    Sheila Green 4553.55
    Amit Sethi 4568.93

    Here is the file output:
    Andrew Miller 3289.49Sheila Green 4553.55Amit Sethi 4568.93

    why the difference?

    thanks all!

  5. #5
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: FileReader argument, compiler problem

    I can not really explain as to why this is not working, it may have something to do with the printWriter class not recognizing \n as the proper newline character but I'm not sure. There is however a pretty quick fix that requires the use of the String object's replaceAll() method and System.getProperty("line.separator"). Saying [some string].replaceAll("\n", System.getProperty("line.separator") would return the string formatted to contain the correct new line delimiter. You would just pass that to the outFile.printf() statement. I have tried this and it seems to work correctly.

    Edit: It would seem that the reason \n does not work is that windows doesn't recognize it as a newline in a file. System.getPropterty("line.separator") returns whatever is correct for the current system being used.
    Last edited by SerratedMind; September 10th, 2011 at 06:09 PM.

  6. The Following User Says Thank You to SerratedMind For This Useful Post:

    mwr76 (September 10th, 2011)

Similar Threads

  1. Using the fileReader?
    By malconrhymes in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2011, 04:47 AM
  2. Scanner/FileReader help
    By JohnTravoltaire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 12:28 AM
  3. FileReader help
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 5th, 2010, 07:29 AM
  4. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 01:12 AM
  5. sql compiler
    By tsuki in forum JDBC & Databases
    Replies: 6
    Last Post: October 16th, 2009, 10:35 PM