Results 1 to 5 of 5
  1. #1
    tidus1103 is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default need help with several problems

    Hi, i have been writing code for a grade book program. I have a few problems writing a file that is to read and edit a previously made file. When i open the file it says Error: null. im not quite sure what causing it, also i cant figure out how to split the string of rows/ edit them from the text file, and being able to add new "grades" to the file.

    here is the code for the main, just ignore the newUser()
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class DesktopApplicationRunner
    {
    	public static void  main(String[] args)
    	{
    		String newOrOld = "";
    		int tof = 1;
    		int studentValue = 0;
    		int gradeValue = 0;
    		String fileName = "";
    		
    		System.out.println("Hello! Welcome to Grade Check!");
    		System.out.println("Please answer the following questions to get started.");
    
    		Scanner console = new Scanner(System.in);
    		
    		System.out.println("Have you used this program before? (yes or no)");
    		newOrOld = console.nextLine();
    		
    		if(newOrOld.equals("yes"))
    			{
    				oldUser f = new oldUser();
    			}else if(newOrOld.equals("no")){
    				newUser g = new newUser();
    			}else {
    		
    			System.out.println("Invalid entry please try again");
    			System.exit(1);
    			}
    		}
    	}
    this is the code for oldUser()
    import java.util.*;

    Java Code:
    public class oldUser
    {
    	int studentValue = 0;
       int gradeValue = 0;
          
       
       public oldUser()
       {
          Scanner console = new Scanner(System.in);
          
          System.out.println("How many students do you want to add to your class? (0-15)");
          studentValue = console.nextInt();
          
          if(studentValue >15 || studentValue <0)
    		  {
              System.out.println("Invalid entry");
              System.exit(1);
            }
          
          System.out.println("How many grades do you want to add? (0-6)");
          gradeValue = console.nextInt();
          
          if(gradeValue >6 || gradeValue <0)
           	{
                System.out.println("Invalid entry");
                System.exit(1);
             }
          
          System.out.println("What is the file name?");
          
          String fileName = console.next();
          fileName = fileName + ".dat";
    		
    		try
    		{
    		ReadFile j = new ReadFile(studentValue, gradeValue, fileName);
         	}catch(Exception e){
    			System.err.println("Error: " + e.getMessage());
    		}
    
          }
       }
    this is the reader code so far
    Java Code:
    import java.util.*;
    import java.io.*;
    
    
    public class ReadFile
    {
    	static int studentValue;
    	static int gradeValue;
    	static int sEditValue;
    	static int gEditValue;
    	static String fileStudentValue;
    	static String fileGradeValue;
    	static String filePercent;
    	
    	
    	
    	static int count = 3;
    	
    	String fileName;
    	BufferedReader codeFile;
    	Scanner console = new Scanner(System.in);
    	
    	
    	
    	public ReadFile(int students, int grades,String files) throws Exception
    	{
    	
    		sEditValue = students;
    		gEditValue = grades;
    		fileName = files;
    		String[] fileStudent = new String[25];
    		String[] studentName = new String[25];
    		
    
    		
    		codeFile = new BufferedReader(new FileReader(fileName));
    
          PrintWriter outFile = new PrintWriter(fileName);
    	  
    	  
    	  	for(int k =0; k<3; k++)
    		{
    			if(k == 0)
    			{				
    				fileStudentValue = codeFile.readLine();
    				studentValue = Integer.parseInt(fileStudentValue);
    			}else if(k == 1)
    			{
    				fileGradeValue = codeFile.readLine();
    				gradeValue = Integer.parseInt(fileGradeValue);
    				int[][] fileGrades = new int[studentValue][gradeValue];
    				
    			}else if(k == 2)
    			{
    				filePercent = codeFile.readLine();
    				for(int l = 0; l<studentValue; l++)
    				{
    					fileStudent[l] = codeFile.readLine();
    				}
    			}
    		}
    		filePercent = codeFile.readLine();
    		System.out.println(studentValue);
    		System.out.println(gradeValue);
    		System.out.println(filePercent);
    		compareCode(outFile);
    	    
    	   codeFile.close();
    	   outFile.close();
    	}
    	
    	public static void compareCode( PrintWriter outP)
       {	
    		
    		
    		
    	}
    }
    and this is the file that im trying to use
    Java Code:
    4
    4
    60739594
    asdf                032065098045000000000000000000
    asdf                032065098098000000000000000000
    adsf                098096095094000000000000000000
    asdf                095093096094000000000000000000

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    fileName = files;
    File f = new File(fileName);
    System.out.println(f.exists());
    Add those two lines and see what the output is.

  3. #3
    tidus1103 is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    i found out why it was returning the Error:Null. The file was empty, and nothing to set value.
    However after adding these lines :
    Java Code:
    while(count < gradeValue)
    		{
    			if(count == 0)
    			{				
    				fileStudentValue = codeFile.readLine();
    				studentValue = Integer.parseInt(fileStudentValue);
    			}else if(count == 1)
    			{
    				fileGradeValue = codeFile.readLine();
    				gradeValue = Integer.parseInt(fileGradeValue);
    			}else if(count == 2)
    			{
    				filePercent = codeFile.readLine();
    			}else if(count ==3)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==4)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==5)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==6)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==7)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==8)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==9)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==10)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==11)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==12)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==13)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==14)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==15)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==16)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==17)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==18)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==19)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==20)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==21)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==22)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==23)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==24)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}else if(count ==25)
    			{
    				fileStudent[count] = codeFile.readLine();
    			}
    			count++;
    		}
    i now get the following error:
    Error: For input string: "asdf 045078096078000000000000000000"

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    for count == 3 all the way to count == 25 do the exact same thing. Don't hardcode that much stuff, why not just do
    Java Code:
    if(count >= 3){
      fileStudent[count] = codeFile.readLine();
    }

  5. #5
    tidus1103 is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. Gui problems
    By bulldog in forum Advanced Java
    Replies: 1
    Last Post: 12-11-2009, 12:35 PM
  3. having problems
    By pinkdiamondgail in forum New To Java
    Replies: 6
    Last Post: 02-06-2009, 11:35 PM
  4. many to many problems
    By cecily in forum JDBC
    Replies: 1
    Last Post: 08-02-2007, 05:51 PM
  5. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 04:57 PM

Posting Permissions

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