error

Page 1 of 1

5 Replies - 3484 Views - Last Post: 08 January 2012 - 06:01 PM Rate Topic: -----

#1 darayus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-January 12

error

Posted 08 January 2012 - 11:02 AM

package labs;
     
     
    import java.io.*;
import java.net.IDN;
import java.util.*;
     
    public class CSVRead3 {
    	private static Scanner in;

		public static void main(String[] args) throws FileNotFoundException, FileNotFoundException {
    		stdinStats();	
			testArrayStatistics(); 
    		String regNumber, first, last;
    		regNumber=first=last="";
    		String[] enrolModules = new String[8];
    		String[] enrolModuleTitles = new String[8];
    		String[] enrolModuleMarks = new String[8];
    		double average = 0;
            Scanner input = new Scanner(new FileInputStream ("students.csv"));
            System.out.println("Please enter a Student Registration number");
            Scanner userinput = new Scanner(System.in);
            String x = userinput.next();
            input.nextLine();
            while(input.hasNext()){
            	String num = input.nextLine();
            	String delimiter=(",");
            	String[] reg=num.split(delimiter);
            	if(reg[0].equals(x)) {
            		regNumber = x;
                    first = reg[1];
                    last = reg[2];
                    break;  
            	}
            }
            input.close();
            input = new Scanner(new FileInputStream ("enrolment.csv"));
            while(input.hasNext()){
            	String num = input.nextLine();
                String delimiter=(",");
                String[] reg=num.split(delimiter);
                if(reg[0].equals(x)){
                	for(int i=1; i<reg.length; i++){ 
                		enrolModules[i-1]=reg[i];
                	}	
                    break;
                }    
            }
           
            input = new Scanner(new FileInputStream ("modules.csv"));
            input.nextLine();
            while(input.hasNext()) {
            	String num = input.nextLine();
            	String delimiter=(",");
            	String[] reg=num.split(delimiter);
            	for(int i=0; i<enrolModules.length; i++)
            		if(reg[0].equals(enrolModules[i])){
            			enrolModuleTitles[i]=reg[1];
            			break;
            		}
            			
            }
    
          
    	
    
    		input = new Scanner(new FileInputStream ("marks.csv"));
    		input.nextLine();
    		while(input.hasNext()) {
    			String num = input.nextLine();
            	String delimiter=(",");
            	String[] reg=num.split(delimiter);
            	if(reg[0].equals(x)){
            		int k=0;
            		
            		for(int i=1; i<reg.length; i++){
            		
            			if(!reg[i].isEmpty())
            			 enrolModuleMarks[k++]=reg[i];
            		}
            		break;
            	}
          	}
    		
    		for(int i=0; i<enrolModuleMarks.length; i++)   			
    			if(!enrolModuleMarks[i].equals("X"))
    				average+=Double.parseDouble(enrolModuleMarks[i]);
    		
    		average/=enrolModuleMarks.length;
    		
    			
    		 System.out.println(regNumber+"\t"+first+"\t"+last);    
             for(int i=0; i<enrolModules.length; i++)
             System.out.println(enrolModules[i]+"\t"+enrolModuleMarks[i]);
             System.out.println("Average = "+average);
    	}
    	
    			
    		
		
		//part2 starts from here(having problems here)


					
    			
    			public static void testArrayStatistics() {
    				String test = "marks.csv";
    				System.out.println("Sum = " + arraySum(test)); 
    				System.out.println("Min = " + arrayMin(test)); 
    				System.out.println("Max = " + arrayMax(test)); 
    			}
    			
    			
    			private static String arraySum(String test) {
					// TODO Auto-generated method stub
					return null;
				}









				public static void stdinStats() {
    				System.out.println("Please enter number of doubles"); 
    				int size = in.nextInt(); 
    				System.out.println("Please enter doubles"); 
    				double sum = 0; 
    				double min = Double.MAX_VALUE; 
    				double max = Double.MIN_VALUE; 
    				for (int i = 0; i < size; i++){
    					double x = in.nextDouble();
    					if (x < min) min = x; 
    					if (x > max) max = x; 
    					sum += x; 
    				}
    				System.out.println("Sum: "  + sum); 
    				System.out.println("Average: "  + sum/size); 
    				System.out.println("Min: "  + min); 
    				System.out.println("Max: "  + max); 
    			}
    			
    			public static double arraySum(double [] x) {
    				double result = 0.0; 
    				for (double d : x) result += d; 
    				return result; 
    			}
    			
    			public static double arrayAverage(double [] x) {
    				return arraySum(x)/x.length;  
    			}
    			
    			public static double arrayMin(String string) {
    				double min = Double.MAX_VALUE; 
    				for (double d :reg[i] )  // ERROR
    					if (d < min) min = d; 
    				return min; 
    			}
    			
    			public static double arrayMax(String string) {
    				double max = Double.MIN_VALUE; 
    				for (double d : reg[i])  // ERROR
    					if (max < d) max = d; 
    				return max; 
    			}
    			
    	}
    


Is This A Good Question/Topic? 0
  • +

Replies To: error

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: error

Posted 08 January 2012 - 11:06 AM

Look again at the proper syntax for the enhanced for loop -

for (Type value : myArray) 
{
    System.out.println(myValue);
}


Also consider properly formatting your code as at the moment its a horror to read. I think the array 'reg' is a local variable in the main method and so you cannot access it in those two methods anyways (unless you pass it in).

This post has been edited by Ryano121: 08 January 2012 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: error

Posted 08 January 2012 - 11:07 AM

This is a prime example of being lazy and expecting the volunteers here to do everything for you.

You couldn't even be bothered to write a good topic title that summarizes the problem.

You couldn't take 30 seconds to describe the problem, quote the error, describe what your program is trying to do...

You gave this exactly ZERO effort and that is what you can expect of others.
Was This Post Helpful? 2
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: error

Posted 08 January 2012 - 11:09 AM

Please describe you error, or even better post it
Specify if it it a compilation error or a run time error
and give a meaninfull title to your topic
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,213
  • Joined: 20-September 08

Re: error

Posted 08 January 2012 - 11:18 AM

http://technojeeves....free/118-errors
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: error

Posted 08 January 2012 - 06:01 PM

Also, please avoid using parallel arrays. It is not a good OO practice. Design a Module class instead to encapsulate your fields, and store a single Module[]. See my tutorial Moving Away From Parallel Arrays for more information.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1