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

Thread: Using user input of characters rather than integers to assign values to variables

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using user input of characters rather than integers to assign values to variables

    // gpa.java
    // basic kind of accurate gpa calculator with rudimentary introduction/welcome
    import java.util.Scanner; // program uses scanner
    import java.math.*; // math stuff
     
    public class gpa
    {
    	//static double A; forget about this at the moment i dont know how to really use it yet
    	//static double A_minus;
    	//static double B_plus;
    	//static double B;
    	//static double B_minus;
    	//static double C_plus;
    	//static double C;
    	//static double C_minus;
    	//static double D;
    	//static double F;
     
       // main method begins program execution
       public static void main( String[] args )
       {
     
      	  // create Scanner to obtain input from command window 
      	  Scanner input = new Scanner ( System.in );
     
          // create a GradeBook object and assign it to myGradeBook
          gpacn myGradeBook = new gpacn();
     
    	  // display initial value of courseName
    	  System.out.printf ( "Initial course name is: %s\n\n",
    		myGradeBook.getCourseName () );
     
          // prompt for and input course name
          System.out.println ( "Enter semester number (1,2,3,4,5, etc) :" );
          String theName = input.nextLine(); // read a line of text
    	  myGradeBook.setCourseName ( theName ); // set the course name
          System.out.println(); // outputs a blank line
     
          // display welcome message after specifying course name
          myGradeBook.displayMessage();
     
         // end main
    		float c_num; // total number of courses calculated
    		float c1g; // course1 grade
    		float c2g; // course2 grade
    		float c3g; // course3 grade
    		float c4g; // course4 grade
    		float c5g; // course5 grade
    		float cum_prog; // program gpa 10scale
    		float cum_gpa; // total semester gpa 4 scale
    		float necvar; // equals 4
     
    		necvar = 4;
     
    		System.out.print ("Enter the total number of courses for the semester: "); // prompt for input
    		c_num = input.nextInt();
     
    		if (c_num == 3) {
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course one\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c1g = input.nextInt(); 
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course two\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c2g = input.nextInt();
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course three\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c3g = input.nextInt();
     
    		cum_prog = ((c1g + c2g +c3g) / ( c_num )); // calculate semester GPA for 3 courses
     
    		cum_gpa = ( necvar - (cum_prog / necvar) );
     
    		System.out.println ("Your GPA for this semester is " + cum_gpa);
     
     
    		} else if (c_num == 4) {
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course one\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c1g = input.nextInt(); 
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course two\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c2g = input.nextInt();
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course three\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c3g = input.nextInt();
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course three\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c4g = input.nextInt();
     
    		cum_prog = ((c1g + c2g + c3g + c4g) / ( c_num)); // calculate semester gpa for 4 courses
     
    		cum_gpa = ( necvar - (cum_prog / necvar) );
     
    		System.out.println ("Your GPA for this semester is " + cum_gpa);
     
     
    		} else if (c_num == 5) {
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course one\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c1g = input.nextInt(); 
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course two\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c2g = input.nextInt();
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course three\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c3g = input.nextInt();
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course three\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c4g = input.nextInt();
     
    			System.out.print ("Enter the number value that corresponds to the letter grade received for course three\n (1=A, 2=A-, 3=B+, 4=B, 5=B-,6=C+, 7=C, 8=C-, 9=D, 10=F: "); // prompt for input
    			c5g = input.nextInt();
     
    		cum_prog = ((c1g + c2g + c3g + c4g + c5g) / ( c_num)); // calculate semester gpa for 5 courses
     
    		cum_gpa = ( necvar - (cum_prog / necvar) );
     
    		System.out.println ("Your GPA for this semester is " + cum_gpa);
     
       }  // end main
     }// end class gpa.java
    }

    Alright here's the deal, I'm an amateur who's been building off of some of the sample things I've been shown and I tried to come up with a GPA calculator.

    I'm familiar with using scanner to fill in variables with various integers or whatever, initially I was trying to have users enter in "A+" or even "A_plus" and have it represent 4.0 and 3.7 respectively etc.

    Since I only know how to have it read integers the only way to make it semi-coherent was the really ugly system of type 1 for A+, 2 for A, 3 for A-, etc. Obviously the equation is easy if I were to have them enter 3.7, 4.0, 3.4, and stuff but that would defeat the entire purpose of the calculator since they are doing it to find out their grade in GPA by entering in their letter grades.

    What I'm left with is a pretty ugly equation (if you're wondering what the hell "necvar is for...) that leaves most calculations within 5% error (as small as <1% and large as 10%+.) But alas, I have no idea how to have them enter letter grades and have the code read them as their respective valuations. At one point I thought I was doing it with the static double stuff that I now have in // comment tags but I have since ditched that effort.

    I've only been doing this stuff for a week and a half now and this is my first programming language at all besides some really basic HTML so bear with me please
    Last edited by madiaz3; May 15th, 2012 at 07:49 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Using user input of characters rather than integers to assign values to variables

    how to have them enter letter grades and have the code read them as their respective valuations.
    Do you have a table of all the letter grades and their valuations?
    A Map would be one way to convert a letter grade to a valuation.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 13
    Last Post: December 6th, 2011, 01:17 PM
  2. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 08:42 PM
  3. how to assign values to (args) from Jcreator
    By amr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 02:41 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  5. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM