Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Code Help!

Code Help!

Scheduled Pinned Locked Moved Java
helpjavaquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10694570
    wrote on last edited by
    #1

    I cannot figure out what I am doing wrong. I just don't feel like I'm ever going to understand all of this. My prompts and loop are working fine. However, I am not getting any output. I'm sure it's a fairly simple thing, but I just don't see it so I can't know for sure what I need to fix. I have watched all the videos and looked through my textbook and the internet, but I need someone to "dumb" it down for me, I guess. Thank you, in advance, for your patience and help.

    import java.util.Scanner;

    public class StudentInfoTest
    {
    public static void main(String[] args)
    {
    //Initiate scanner.
    Scanner input = new Scanner(System.in);

    	//Declare Variables.
    	String answer = "";
    	double GPA = 0.0;
    	
    	//Create Object		
    	StudentInfo info = new StudentInfo(null, 0, 0);
    	
    	//Prompt for and store student name.
    	System.out.print("Enter student name: ");
    	String name = input.nextLine();
    	info.setName(name); 
    
    	//Prompt for class credits and letter grade.
    	//Loop until answer is "N".
    	do 
    	{
    	System.out.print("Enter class credits: ");
    	String credits = input.nextLine();
    	
    	System.out.print("Enter letter grade: ");
    	String grade = input.nextLine();
    
    	System.out.print("Add another class? (Y or N): ");
    	
    	answer = input.nextLine();
    	}
    	while (answer.equals("Y"));
    	
    	System.out.printf(name + ", Your GPA is %.2f/n.", GPA);
    	
    }//Ends Main
    

    }//Ends Class

    public class StudentInfo {

        //Variables
        private String studentName;
        private double totalPoints;
        private double totalCredits;
    
        //Constructor
        public StudentInfo(String name, double points, double credits)
        {
            //Initialize Variables
            studentName = name;
            totalPoints = points;
            totalCredits = credits;
        }
    
        //Create "Set" Functions
        public void setName(String name)
        {
            studentName = name;
        }
    
        //Create "Get" Functions
        public String getName()
        {
            return studentName;
        }
    
            public void addClass(double credits, String grade)
            {
                double points = 0.0;
    
                if(grade == "A" | grade == "a")
                    points = 4.0;
                if(grade == "B" | grade == "b")
                    points = 3.0;
                if(grade == "C" | grade == "c&quo
    
    W 1 Reply Last reply
    0
    • U User 10694570

      I cannot figure out what I am doing wrong. I just don't feel like I'm ever going to understand all of this. My prompts and loop are working fine. However, I am not getting any output. I'm sure it's a fairly simple thing, but I just don't see it so I can't know for sure what I need to fix. I have watched all the videos and looked through my textbook and the internet, but I need someone to "dumb" it down for me, I guess. Thank you, in advance, for your patience and help.

      import java.util.Scanner;

      public class StudentInfoTest
      {
      public static void main(String[] args)
      {
      //Initiate scanner.
      Scanner input = new Scanner(System.in);

      	//Declare Variables.
      	String answer = "";
      	double GPA = 0.0;
      	
      	//Create Object		
      	StudentInfo info = new StudentInfo(null, 0, 0);
      	
      	//Prompt for and store student name.
      	System.out.print("Enter student name: ");
      	String name = input.nextLine();
      	info.setName(name); 
      
      	//Prompt for class credits and letter grade.
      	//Loop until answer is "N".
      	do 
      	{
      	System.out.print("Enter class credits: ");
      	String credits = input.nextLine();
      	
      	System.out.print("Enter letter grade: ");
      	String grade = input.nextLine();
      
      	System.out.print("Add another class? (Y or N): ");
      	
      	answer = input.nextLine();
      	}
      	while (answer.equals("Y"));
      	
      	System.out.printf(name + ", Your GPA is %.2f/n.", GPA);
      	
      }//Ends Main
      

      }//Ends Class

      public class StudentInfo {

          //Variables
          private String studentName;
          private double totalPoints;
          private double totalCredits;
      
          //Constructor
          public StudentInfo(String name, double points, double credits)
          {
              //Initialize Variables
              studentName = name;
              totalPoints = points;
              totalCredits = credits;
          }
      
          //Create "Set" Functions
          public void setName(String name)
          {
              studentName = name;
          }
      
          //Create "Get" Functions
          public String getName()
          {
              return studentName;
          }
      
              public void addClass(double credits, String grade)
              {
                  double points = 0.0;
      
                  if(grade == "A" | grade == "a")
                      points = 4.0;
                  if(grade == "B" | grade == "b")
                      points = 3.0;
                  if(grade == "C" | grade == "c&quo
      
      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      If I understand your question correctly, you never get the correct GPA? If that is the case, it looks like you never call getGPA, you only use the initial (0.0) value. So instead of

      System.out.printf(name + ", Your GPA is %.2f/n.", GPA);

      Should you have

      System.out.printf(name + ", Your GPA is %.2f/n.", info.getGPA());

      The same applies to credits and grade. You never seem to call addClass

      U 1 Reply Last reply
      0
      • W Wendelius

        If I understand your question correctly, you never get the correct GPA? If that is the case, it looks like you never call getGPA, you only use the initial (0.0) value. So instead of

        System.out.printf(name + ", Your GPA is %.2f/n.", GPA);

        Should you have

        System.out.printf(name + ", Your GPA is %.2f/n.", info.getGPA());

        The same applies to credits and grade. You never seem to call addClass

        U Offline
        U Offline
        User 10694570
        wrote on last edited by
        #3

        I guess I don't understand what I am supposed to do to call them...

        W L 2 Replies Last reply
        0
        • U User 10694570

          I guess I don't understand what I am supposed to do to call them...

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Have a look at the example I wrote. The same way, after you have got the input from the user call the addClass method with your variables... Something like

          info.addClass(credits, grade);

          Just keep in mind that the next thing you should implement is proper conversion between the data types (from string to double)

          U 1 Reply Last reply
          0
          • W Wendelius

            Have a look at the example I wrote. The same way, after you have got the input from the user call the addClass method with your variables... Something like

            info.addClass(credits, grade);

            Just keep in mind that the next thing you should implement is proper conversion between the data types (from string to double)

            U Offline
            U Offline
            User 10694570
            wrote on last edited by
            #5

            I really appreciate your help... Apparently, I'm EXTREMELY dumb with this. So you mean... That I have:

            System.out.print("Enter class credits: ");
            String credits = input.nextLine();

            And I need to change it to:

            System.out.print("Enter class credits: ");
            double credits = input.nextDouble();

            Where would I put the:

            info.addClass(credits, grade);

            When I added it after the grade code, it messed up my prompts.

            1 Reply Last reply
            0
            • U User 10694570

              I guess I don't understand what I am supposed to do to call them...

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If you really don't understand then you should go back and work through your course notes. You can also find some excellent learning materials at http://download.oracle.com/javase/tutorial/index.html[^].

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups