Results 1 to 2 of 2
  1. #1
    Masyuyo is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Variable not initalizing

    I'm having trouble with a lab.
    It keeps on telling me Tpts is not initialized and i can't find a way to add pts to Tpts outside of the for loop.
    Program is to calculate GPA
    Java Code:
    import java.io.*;
    import java.text.*;
    import java.util.*;
    public class Lab6switch
    
    {
    	public static void main(String[]args)throws IOException
    	{
    		String  Student, 
    				course,
    				lg;
    		char g;				
    		int pts,
    			x,
    			Stunum,
    			Tpts,
    			coursnum;
    		double gpa;
    		DecimalFormat gpa = new DecimalFormat(""###.##"")
    		PrintWriter pw = new PrintWriter("gpa.txt");
    		Scanner console = new Scanner(System.in);		
    		System.out.println("Please enter student number");
    		Stunum = console.nextInt();
    		System.out.println("Pleas input Student name,");
    		Student = console.next();
    		pw.println("Bucks County Community College " + "\r\n" + Student + "\r\n" + Stunum + "\r\n" + "Course \t \t Grade \t \t Pts");
    		pw.flush();
    		System.out.println("Please input number of classes.");
    		coursnum = console.nextInt();		
    		for(x=1;x <= coursnum; x++ )
    		{
    		System.out.println("Please input Course and Section (ex. CISC115.94)");
    		course = console.next();
    		pw.print(course + "\t \t");
    		pw.flush();
    		System.out.println("please input Letter grade.");
    		lg = console.next();
    		pw.print(lg + "\t \t");
    		pw.flush();
    		g=lg.charAt(0);
    		switch(g)
    		{
    			case 'A': g = 4;
    			case 'a': g = 4;
    			break;
    		
    			case 'B': g=3;
    			case 'b': g=3;
    			break;
    		
    			case 'C': g=2;
    			case 'c': g=2;
    			break;
    		
    			case 'D': g=1;
    			case 'd': g=1;
    			break;
    		
    			case 'F': g=0;
    			case 'f': g=0;
    			break;
    		
    			default: g=0;
    			break;
    		}
    		pts=g;
    		pw.print(pts + "\r\n");
    		pw.flush();
    		Tpts = +pts;
    		gpa= Tpts/coursnum;
    		}
    		pw.println("\t \t \t \t \t \t GPA: ");
    		pw.flush();
    		pw.print(gpa.format(gpa));		
    		pw.close();
    	}
    
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: Variable not initalizing

    You need to give Tpts an initial value.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to compare variable with multiple variable?
    By mharckz009 in forum New To Java
    Replies: 5
    Last Post: 09-11-2012, 05:33 PM
  2. Replies: 1
    Last Post: 04-21-2012, 07:03 PM
  3. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 07:22 PM
  4. Replies: 11
    Last Post: 07-07-2010, 07:02 AM
  5. Variable name determined by another variable's value
    By Lumpkabob in forum New To Java
    Replies: 5
    Last Post: 04-14-2009, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •