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

Thread: Can someone help me with my code.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone help me with my code.

    import java.util.ArrayList;
    import javax.swing.*;
    import java.text.*;
    import java.awt.*;
     
    public class Student
    {
    	private String id = new String ();
    	private String name = new String ();
    	private double gpa;
    	private Student [] person;
    	private Student [] answers;
    	private Student [] questions;
     
    	Student() //Constructor
    	{
    		id = "Unknown ID";
    		name = "Unknown Name";
    		gpa = 0;
    	}
     
    	Student (String idnum, String n, double g)
    	{
    		id = idnum;
    		name = n;
    		gpa = g;
    	}
     
    	Student (int howManyKids, int howManyAnswers, String idnum, String n, double g)
    	{
    		id = new String(idnum);
    		name = new String(n);
    		double gpa = g;
     
    		int i = 0;
    		int a =0;
     
    		person = new Student[howManyKids];
    		for (i=0; i < howManyKids; ++i)
    		person [i] = new Student();
     
    		answers = new Student[howManyAnswers];
    		for (a=0; a < howManyAnswers; ++a)
    		answers [a] = new Student();
     
    		//populate (howManyKids);
    		//populate (howManyAnswers);
    	}
     
    	public void setAllStudentFields (int howManyKids, int howManyAnswers, String idnum, String n, double g)
    	{
    		id = new String(idnum);
    		name = new String(n);
    		double gpa = g;
     
    		int i = 0;
    		int a =0;
     
    		person = new Student[howManyKids];
    		for (i=0; i < howManyKids; ++i)
    		person [i] = new Student();
     
    		answers = new Student[howManyAnswers];
    		for (a=0; a < howManyAnswers; ++a)
    		answers [a] = new Student();
     
    		//populate (howManyKids);
    		//populate (howManyAnswers);
    	}// end of setter to setAllStudentFields
     
    	public static void teacherTest(ArrayList questions)
    	{
    		boolean valid = false;
     
    		ArrayList<Character> key = new ArrayList<Character>();
    		if ((one != 'T') || (one != 'F'))
    			questions.add(new Character(one));
    		else
    		{
    			System.out.println("Not an accpetable answer! Only enter a T or F.");
    			valid = false;
    			while (valid == false)
    			{
    				System.out.println("(Only enter a T or F)");
    				System.out.println("Question number " + i + " -->");
    				one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    				if ((one != 'T') || (one != 'F'))
    				{
    					questions.add(new Character(one));
    					valid = true;
    				}//end of if statement
    			}
    		}
    		while (one != 'X')
    		{
    			key.add(new Character(one));
    			System.out.println("Enter the answer to question -->");
    			one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    		}
    	}
     
    	public static void testForOneStudent(ArrayList answers)
    	{
    		char one = ' ';
    		Character torf;
    		int i = 0;
    		int correct = 0;
    		boolean valid = false;
     
    		for (i = 1; i <= 5; ++i)
    		{
    			System.out.println("(Only enter a T or F)");
    			System.out.println("Question number " + i + " -->");
    			one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    			if ((one != 'T') || (one != 'F'))
    				answers.add(new Character(one));
    			else
    			{
    				System.out.println("Not an accpetable answer! Only enter a T or F.");
    				valid = false;
    				while (valid == false)
    				{
    					System.out.println("(Only enter a T or F)");
    					System.out.println("Question number " + i + " -->");
    					one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    					if ((one != 'T') || (one != 'F'))
    					{
    						answers.add(new Character(one));
    						valid = true;
    					}//end of if statement
    				}//end of while loop
    			}//end of else path
    		}// end of for loop
     
     
    	}//end of public
    	public String getName()
    	{
    		return name;
    	}
     
    	public String getID()
    	{
    		return id;
    	}
     
    	public double getGPA()
    	{
    		return gpa;
    	}
     
    	public void setName(String n)
    	{
    		name = n;
    	}
     
    	public void setID(String idnum)
    	{
    		id = idnum;
    	}
     
    	public void setGPA(double g)
    	{
    		gpa = g;
    	}
     
    	public Student[] getPersons()
    	{
    		return person;
    	}
     
    	public Student[] getAnswers()
    	{
    		return answers;
    	}
     
    	public String toPrintPersonArray()
    	{
    		String pOutput = new String();
     
    		for (int i = 0; i < person.length; i++)
    		{
    			pOutput += "The number of kids are " + (i + 1) + "\nand they are " + person[i].toString() + "\n";
    		}
     
    		return pOutput;
    	}
     
    	public String toPrintAnswerArray()
    	{
    		String aOutput = new String();
     
    		for (int i = 0; i < answers.length; i++)
    		{
    			aOutput += "The number of answers received are " + (i + 1) + "\nand they are " + answers[i].toString() + "\n";
    		}
     
    		return aOutput;
    	}
     
    	public String toString()
    	{
    		return ("\nName --> " + getName() + " \t Id Number --> " + getID() +
    				" \t GPA --> " + getGPA() + "\n" + toPrintPersonArray() +
    				toPrintAnswerArray());
    	}
     
     
    }

    I'm creating a program that has a user input a teacher key and the student's answer to output both the key, the students answer, student name, id, and gpa. I'm in the process of writing a test class but need to figure out the errors to this one first. This needs to be array based as well.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Can someone help me with my code.

    We can't really help you with the errors until you tell us what those errors are. I suggest you throw together an SSCCE that demonstrates one problem at a time without any extra code, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me with my code.

    Quote Originally Posted by KevinWorkman View Post
    We can't really help you with the errors until you tell us what those errors are. I suggest you throw together an SSCCE that demonstrates one problem at a time without any extra code, and we'll go from there.
    I changed my code. I restarted because I couldn't figure out what the errors were.

    here is my main program so far.

    import java.util.ArrayList;
    import javax.swing.*;
    import java.text.*;
    import java.awt.*;
     
    public class NewStudent
    {
    	private String id = new String ();
    	private String name = new String ();
    	private double gpa;
    	private NewStudent [] person;
    	private NewStudent [] answers;
    	private NewStudent [] questions;
     
    	NewStudent() //Constructor
    	{
    		id = "Unknown ID";
    		name = "Unknown Name";
    		gpa = 0;
    	}
     
    	NewStudent (int howManyKids, int howManyAnswers, String idnum, String n, double g)
    	{
    		id = new String(idnum);
    		name = new String(n);
    		double gpa = g;
     
    		int i = 0;
    		int a =0;
     
    		person = new NewStudent[howManyKids];
    		for (i=0; i < howManyKids; ++i)
    		person [i] = new NewStudent();
     
    		answers = new NewStudent[howManyAnswers];
    		for (a=0; a < howManyAnswers; ++a)
    		answers [a] = new NewStudent();
     
    		//populate (howManyKids);
    		//populate (howManyAnswers);
    	}
     
    	public void setAllStudentFields (int howManyKids, int howManyAnswers, String idnum, String n, double g)
    	{
    		id = new String(idnum);
    		name = new String(n);
    		double gpa = g;
     
    		int i = 0;
    		int a =0;
     
    		person = new NewStudent[howManyKids];
    		for (i=0; i < howManyKids; ++i)
    		person [i] = new NewStudent();
     
    		answers = new NewStudent[howManyAnswers];
    		for (a=0; a < howManyAnswers; ++a)
    		answers [a] = new NewStudent();
     
    		//populate (howManyKids);
    		//populate (howManyAnswers);
    	}// end of setter to setAllStudentFields
     
    	public static void teacherTest(ArrayList questions)
    	{
    		boolean valid = false;
     
    		ArrayList<Character> key = new ArrayList<Character>();
    		System.out.println("(Only enter a T or F for the Teacher's Key)");
    		System.out.println("Enter the answer to question -->");
    		char one = ' ';
    		one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    		if ((one != 'T') || (one != 'F'))
    		questions.add(new Character(one));
    		else
    		{
    			System.out.println("Not an accpetable answer! Only enter a T or F.");
    			valid = false;
    			while (valid == false)
    			{
    				System.out.println("(Only enter a T or F)");
    				System.out.println("Enter the answer to question -->");
    				one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    				if ((one != 'T') || (one != 'F'))
    				{
    					questions.add(new Character(one));
    					valid = true;
    				}//end of if statement
    			}
    		}
    		while (one != 'X')
    		{
    			key.add(new Character(one));
    			System.out.println("Enter the answer to question -->");
    			one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    		}
    	}
     
    	public static void testForOneStudent(ArrayList answers)
    	{
    		char one = ' ';
    		Character torf;
    		int i = 0;
    		int correct = 0;
    		boolean valid = false;
     
    		for (i = 1; i <= 5; ++i)
    		{
    			System.out.println("(Only enter a T or F)");
    			System.out.println("Question number " + i + " -->");
    			one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    			if ((one != 'T') || (one != 'F'))
    				answers.add(new Character(one));
    			else
    			{
    				System.out.println("Not an accpetable answer! Only enter a T or F.");
    				valid = false;
    				while (valid == false)
    				{
    					System.out.println("(Only enter a T or F)");
    					System.out.println("Question number " + i + " -->");
    					one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
    					if ((one != 'T') || (one != 'F'))
    					{
    						answers.add(new Character(one));
    						valid = true;
    					}//end of if statement
    				}//end of while loop
    			}//end of else path
    		}// end of for loop
     
     
    	}//end of public
     
     
    	public String getName()
    	{
    		return name;
    	}
     
    	public String getID()
    	{
    		return id;
    	}
     
    	public double getGPA()
    	{
    		return gpa;
    	}
     
    	public void setName(String n)
    	{
    		name = n;
    	}
     
    	public void setID(String idnum)
    	{
    		id = idnum;
    	}
     
    	public void setGPA(double g)
    	{
    		gpa = g;
    	}
     
    	public String toPrintPersonArray()
    	{
    		String pOutput = new String();
     
    		for (int i = 0; i < person.length; i++)
    		{
    			pOutput += "The number of kids are " + (i + 1) + "\nand they are " + person[i].toString() + "\n";
    		}
     
    		return pOutput;
    	}
     
    	public String toPrintAnswerArray()
    	{
    		String aOutput = new String();
     
    		for (int i = 0; i < answers.length; i++)
    		{
    			aOutput += "The number of answers received are " + (i + 1) + "\nand they are " + answers[i].toString() + "\n";
    		}
     
    		return aOutput;
    	}
     
    	public String toString()
    	{
    		return ("\nName --> " + getName() + " \t Id Number --> " + getID() +
    				" \t GPA --> " + getGPA() + "\n" + toPrintPersonArray() +
    				toPrintAnswerArray());
    	}
    }

    That compiles ok but my test class can't run it.

    import java.util.ArrayList;
    import javax.swing.*;
    import java.text.*;
    import java.awt.*;
     
    public class TestStudent
    {
    	public static void main(String args[])
        {
    		String name;
    		String id;
    		double gpa;
     
    		NewStudent john = new NewStudent();
    		john.teacherTest();
     
     
     
     
     
     
     
     
     
     
    	}
    }

    Can anyone help me get this running?
    Last edited by you_know_what_it_is; February 23rd, 2012 at 04:05 PM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Can someone help me with my code.

    When you say that your test class can't run it, what exactly do you mean? Do you have compiler errors? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me with my code.

    It says there is a problem with this line of code. john.teacherTest();

  6. #6
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Can someone help me with my code.

    Left click the black screen that displays the error message.
    Click on the select all option.
    On your keyboard hit Ctrl + c.
    Come back here and open a new reply and hit Ctrl + v.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Can someone help me with my code.

    You are trying to use a non-static method called teacherTest that does not take any parameters. Where do you have that method defined? Hint: you don't.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Help merging program code with GUI code
    By Wilha in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:03 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM

Tags for this Thread