Announcement

Collapse
No announcement yet.

Java question (example included)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java question (example included)

    Good evening forum dwellers! I've been working on a small Java problem in my spare time and have come to a stand-still within a couple of minutes! Ugh. My code:

    Code:
    import	java.io.*;
    import	java.util.*;
    import	java.text.*;
    import	javax.swing.*;
    import	java.awt.*;
    import	java.awt.event.*;
    
    public class pgm02bhc	
    {							 
    	public static void main ( String[] args ) throws IOException
    	{
    		// initialize keyboard input
    			 	BufferedReader keyboard = new
    	    	   BufferedReader(new InputStreamReader(System.in));
    		//	initialize variables
    				char payCode;
    				String empName;
    				float commBase = 250f, commPCT = 0.057f, stdHrs = 40;
    				float piecesCompleted, amount, salaryPay, totalSales, hrlyRate, hrsWorked, total;
    				
    		// initialize report file
    				PrintWriter outFile = new PrintWriter(new FileWriter ( "pgm02bhc.rpt" ));
    				
    		//	insert statements here
    				headings ( outFile );
    			
    				getEmpName ( keyboard, empName );
    		// garbage collection and return to zero	
    				System.gc ();					
    				System.exit(0);
    		
    		// close report file
    				outFile.close();
    				
    	}	//	end main
    	
    		//	**************************************************methods********************************************
    				public static void headings ( PrintWriter outFile )
    								{
    								outFile.println("Employee Name \t Pay Code \t Total Pay");
    								outFile.println();
    								}
    				public static String getEmpName ( BufferedReader keyboard, String empName )
    								{
    								System.out.print( "Please enter employee name (enter to quit): " );
    								empName = keyboard.readLine();
    								return empName;
    								}		
    }	//	end public class pgm02bhc
    My compiling errors:

    Code:
    pgm02bhc.java:37: variable empName might not have been initialized
    				getEmpName ( keyboard, empName );
                                                           ^
    pgm02bhc.java:56: unreported exception java.io.IOException; must be caught or declared to be thrown
    								empName = keyboard.readLine();
    Any constructive responses are welcomed and appreciated. Please keep in mind that I am just starting Java and am merely dipping my toes in. Thanks!

  • #2
    For the first message, just initialize it to something.

    For the second message, look up the try-catch exception mechanism. Every Java resource dealing with files should provide an example.

    Comment


    • #3
      Originally posted by Voltage Spike
      For the first message, just initialize it to something.
      I initialized String empName at the beginning but empName will hold a value inputted by the user during the method. Thanks.

      Comment


      • #4
        I know what it does, and you know what it does, but the compiler has no manner of knowing what it does (i.e., input, output, both?).

        I just noticed something, though. That code is wrong. The String class in Java is immutable. You will pass in a copy of the reference to a string, your function will modify the local reference, and empName (from "main") will never change. You either have to use the return value in main, or you must use the ugly array-of-1 hack to simulate a reference-to-a-reference in Java.

        Comment


        • #5
          Originally posted by Voltage Spike
          I know what it does, and you know what it does, but the compiler has no manner of knowing what it does (i.e., input, output, both?).

          I just noticed something, though. That code is wrong. The String class in Java is immutable. You will pass in a copy of the reference to a string, your function will modify the local reference, and empName (from "main") will never change. You either have to use the return value in main, or you must use the ugly array-of-1 hack to simulate a reference-to-a-reference in Java.
          I understand what you're saying and find interest in it. Something I'm going to have to read up on. I was thinking this was cut and dry but as with all other languages there are multiple ways of messing up, haha. Thank you, Voltage. You have spiked my depressed intellect.

          Man, i'm so awesome.

          Comment

          Working...
          X