Grading Method

try catch problem

Page 1 of 1

1 Replies - 590 Views - Last Post: 15 April 2009 - 01:22 AM Rate Topic: -----

#1 Kasi Karra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-March 09

Grading Method

Post icon  Posted 14 April 2009 - 10:00 PM

So we're supposed to create a program that prompts for grades and then writes them into a new text file. We aren't allowed to use the "throws" method cause we're supposed to be learning the try catch stuff.
My problem is that when I go to type in scores for the labs and I type in a number that is too big it will stop and go through the while loop, but when I do letters after typing in to big of a number it just moves on.

import java.util.Scanner;
import java.io.*;
import java.util.InputMismatchException;

public class WritingGradesb
{
	public static void main (String[] args)
	{
		Scanner scan = new Scanner(System.in);
		String answer = "y";
		Boolean pass = true;
		int necessary = 1;
		int labnumber = 1;
		int exams = 1;
		int palcement = 0;
		int names = 1;
		int breaker = 1;

		while (answer.equalsIgnoreCase("y"))
		{
			int Score = 1;
		   try
		   {
			if (names == 1)
			{
				System.out.print("Student name?  (Use the underscore to add on a last name)\n");
				
			}
			String name = scan.next();
			names++;
			String file = name + ".txt";
			int days = -10;
			
			new File(file).createNewFile();
			FileWriter fw = new FileWriter (file);
			BufferedWriter bw = new BufferedWriter (fw);
			PrintWriter outFile = new PrintWriter (bw);
			
			outFile.print (name + "\t");


				while (labnumber <= 10)
				{
					if(palcement == 0)
					{
						labnumber = necessary;
						System.out.print("\nScore for Lab " + labnumber + " ");
						outFile.print ("Lab" + labnumber + "\t");
						Score = scan.nextInt();
						breaker = 1;
						palcement ++;
					}
					while (labnumber == 1 && breaker == 1)
					{
						while (Score > 10 || Score < 0)
						{
							System.out.println("lab 1 is only worth 10 points.  Please reenter score.");
							Score = scan.nextInt();
						}
						
						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while (days > 1 || days < -1)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}

						outFile.print(days + "\t");	
						palcement = 0;
						breaker = 0;
					}
	
					while (labnumber == 2 || labnumber == 3 || labnumber == 8 || labnumber == 9 && breaker == 1)
					{
						while (Score > 20 || Score < 0)
						{
							System.out.println("lab 2 is only worth 20 points.  Please reenter score.");
							Score = scan.nextInt();	
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while (days > 2 || days < -2)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}
					
						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
						labnumber = -1;
					}
	
					while (labnumber == 4 || labnumber == 5 && breaker == 1)
					{
						while (Score > 30 || Score < 0)
						{
							System.out.println("lab 5 is only worth 30 points.  Please reenter score.");
							Score = scan.nextInt();	
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while (days > 3 || days < -3)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}

						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
						labnumber = -1;
					}
				
					while (labnumber == 6 && breaker == 1)
					{
						while (Score > 40 || Score < 0)
						{
							System.out.println("lab 6 is only worth 40 points.  Please reenter score.");
							Score = scan.nextInt();
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while (days > 4 || days < -4)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
					
						}
	
						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
					}

					while (labnumber == 7 && breaker == 1)
					{
						while(Score > 50 || Score < 0)
						{
							System.out.println("lab 7 is only worth 50 points.  Please reenter score.");
							Score = scan.nextInt();
						}
					
						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while (days > 5 || days < -5)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}

						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
					} 
				
					while (labnumber == 10 && breaker == 1)
					{
						while(Score > 60 || Score < 0)
						{
							System.out.println("lab 10 is only worth 60 points.  Please reenter score.");
							Score = scan.nextInt();
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();
	
						while (days > 6 || days < -6)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}
						
						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
					}
					labnumber++;
					necessary++;
				}//while

				while (exams <= 3)
				{
					palcement = 0;
					if (palcement == 0)
					{
						System.out.print("Score for Exam " + exams + " ");	
						outFile.print ("\nExam" + exams + "\t");
						Score = scan.nextInt();
						palcement++;
					}
	
					while (Score > 100)
					{
						System.out.print("Error please reenter score");
						Score = scan.nextInt();
					}
	
					outFile.print(Score + "\t");
					System.out.print("\nEarly or late days? ");
					days = scan.nextInt();

					while (days > 3 || days < 0)
					{
						System.out.print("Too many days.  Please reenter. ");
						days = scan.nextInt();
					}

					outFile.print(days + "\t");
					palcement = 0;
					exams++;
				}

				outFile.close();
				System.out.print("\nCreate a new student file? (y/n)");
				exams = 1;
				palcement = 0;
				labnumber = 1;
				necessary = 1;
				names--;
				answer = scan.next();
		   }

		   catch (InputMismatchException exception)
		   {
			   System.err.print("\nError: InputMismatchException");
		   }

		   catch (IOException exceptional)
		   {
			System.err.print("\nError: IOException");
		   }


		}//while
	}//void
}//class 


Is This A Good Question/Topic? 0
  • +

Replies To: Grading Method

#2 krizjaz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 99
  • Joined: 07-October 07

Re: Grading Method

Posted 15 April 2009 - 01:22 AM

import java.util.Scanner;
import java.io.*;
import java.util.InputMismatchException;

public class WritingGradesb
{
	public static void main (String[] args)
	{
		Scanner scan = new Scanner(System.in);
		String answer = "y";
		Boolean pass = true;
		int necessary = 1;
		int labnumber = 1;
		int exams = 1;
		int palcement = 0;
		int names = 1;
		int breaker = 1;
		boolean looped = true;

		while (looped == true)
		{
			int Score = 1;
		   try
		   {
			if (names == 1)
			{
				System.out.print("\nStudent name?  (Use the underscore to add on a last name)\n");

			}
			String name = scan.next();
			names++;
			String file = name + ".txt";
			int days = -10;

			new File(file).createNewFile();
			FileWriter fw = new FileWriter (file);
			BufferedWriter bw = new BufferedWriter (fw);
			PrintWriter outFile = new PrintWriter (bw);

			outFile.print (name + "\t");


				while (labnumber <= 10)
				{
					if(palcement == 0)
					{
						labnumber = necessary;
						System.out.print("\nScore for Lab " + labnumber + " ");
						outFile.print ("Lab" + labnumber + "\t");
						Score = scan.nextInt();
						breaker = 1;
						palcement ++;
					}
					while (labnumber == 1 && breaker == 1)
					{
						while ((Score > 10 && Score < 2147483647)|| Score < 0)
						{
							System.out.println("lab 1 is only worth 10 points.  Please reenter score.");
							Score = scan.nextInt();
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while ((days > 1 && days < 2147483647) || days < -1)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}

						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
					}

					while ((labnumber == 2 || labnumber == 3 || labnumber == 8 || labnumber == 9) && breaker == 1)
					{
						while ((Score > 20 && Score < 2147483647) || Score < 0)
						{
							System.out.println("lab "+ labnumber +"  is only worth 20 points.  Please reenter score.");
							Score = scan.nextInt();
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while ((days > 2 && days < 2147483647) || days < -2)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}

						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
						labnumber = -1;
					}

					while ((labnumber == 4 || labnumber == 5) && breaker == 1)
					{
						while ((Score > 30 && Score < 2147483647) || Score < 0)
						{
							System.out.println("lab " + labnumber + " is only worth 30 points.  Please reenter score.");
							Score = scan.nextInt();
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while ((days > 3 && days < 2147483647) || days < -3)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}

						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
						labnumber = -1;
					}

					while (labnumber == 6 && breaker == 1)
					{
						while ((Score > 40 && Score < 2147483647) || Score < 0)
						{
							System.out.println("lab 6 is only worth 40 points.  Please reenter score.");
							Score = scan.nextInt();
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while ((days > 4 && Score < 2147483647) || days < -4)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();

						}

						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
					}

					while (labnumber == 7 && breaker == 1)
					{
						while((Score > 50 && Score < 2147483647) || Score < 0)
						{
							System.out.println("lab 7 is only worth 50 points.  Please reenter score.");
							Score = scan.nextInt();
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while ((days > 5 && Score < 2147483647) || days < -5)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}

						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
					}

					while (labnumber == 10 && breaker == 1)
					{
						while((Score > 60 && Score < 2147483647) || Score < 0)
						{
							System.out.println("lab 10 is only worth 60 points.  Please reenter score.");
							Score = scan.nextInt();
						}

						outFile.print(Score + "\t");
						System.out.print("\nEarly or late days? ");
						days = scan.nextInt();

						while ((days > 6 && Score < 2147483647) || days < -6)
						{
							System.out.print("Too many days.  Please reenter. ");
							days = scan.nextInt();
						}

						outFile.print(days + "\t");
						palcement = 0;
						breaker = 0;
					}
					labnumber++;
					necessary++;
				}//while

				while (exams <= 3)
				{
					palcement = 0;
					if (palcement == 0)
					{
						System.out.print("Score for Exam " + exams + " ");
						outFile.print ("\nExam" + exams + "\t");
						Score = scan.nextInt();
						palcement++;
					}

					while ((Score > 100 && Score < 2147483647))
					{
						System.out.print("Error please reenter score ");
						Score = scan.nextInt();
					}

					outFile.print(Score + "\t");
					System.out.print("\nEarly or late days? ");
					days = scan.nextInt();

					while ((days > 3 && Score < 2147483647) || days < 0)
					{
						System.out.print("Too many days.  Please reenter. ");
						days = scan.nextInt();
					}

					outFile.print(days + "\t");
					palcement = 0;
					exams++;
				}

				outFile.close();
				System.out.print("\nCreate a new student file? (y/n)");
				exams = 1;
				palcement = 0;
				labnumber = 1;
				necessary = 1;
				names--;
				boolean loop = true;
				answer = scan.next();
				while(loop == true){
					if(answer.charAt(0) == 'n'){
						looped = false;
						break;
					}else if(answer.charAt(0) == 'y'){
						break;
					}else{
						System.out.print("\nPlease enter only 'y' or 'n'\nCreate a new student file? (y/n)");
						answer = scan.next();
					}
				}
		   }

		   catch (InputMismatchException exception)
		   {
			   System.err.print("\nError: InputMismatchException");
		   }

		   catch (IOException exceptional)
		   {
			System.err.print("\nError: IOException");
		   }
		}//while
	}//void
}//class



Hi! This is, i think, the code. The range of the int data type is from -2,147,483,648 to 2,147,483,647. So, i just joined another condition which is Score < 2147483648 so that there would be no error.
And also, in using while loop, i think it is not good to use string conditions. So, i just initialized a boolean variable for the first while loop and for the last while loop.

I hope i can help.

This post has been edited by krizjaz: 15 April 2009 - 02:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1