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

Thread: help w/ storing/scanning numbers in two dimensional arrays

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default help w/ storing/scanning numbers in two dimensional arrays

    have a assignment that we have to accomplish the same work as in a previous assignment except use two dimensional arrays... this is my previous assignment

    A Computer Technology Instructor has a small class of 10 students. The instructor evaluates the performance of students in the class by administering 2 midterm tests and a Final Exam.

    Write a program that prompts the instructor to enter the 10 grades of Midterm 1 and store these numbers in an array. Next prompt for the 10 grades of Midterm2 and store these numbers in a different array. Next prompt for the 10 grades of the Final Exam and store these in a different array. Next add Midterm1 to Midterm2 to Final and store the total of grades in a different array. Next, scan the array that has the totals and identify the minimum grade and maximum grade. Inform the instructor of the minimum grade and maximum grade.

    Note : do not assume that the grades are in the range 0 to 100. Your program should function properly whether the grades are in the range 0 to 100 or any other range.

    *****and this is my next assignments instructions


    Use one two-dimensional array to accomplish the same work you did in the last Assignment. Think of the students as being the columns of the two-dimensional array. Think of the scores of Midterm 1 as occupying the first row, scores of Midterm 2 occupying the next row, scores of the Final Exam occupying the next row. The total of the 3 exams occupying the next row. Inform the instructor of the minimum total grade and the maximum total grade.

    ***and here is my code, got errors all over the place and not sure where to begin or iif im even in the ballpark

     
    import java.util.Scanner;
    import java.io.*;
     
     
    public class Assign10_Roberts{
    	public static void main(String[] args){
     
    //input Scanner
    Scanner input=new Scanner(System.in);
     
    int midTerm1=0;
     
    int midTerm2=0;
     
    int finalExam=0;
     
     
    int[][]grades= new int[10][10];
     
     
    System.out.println("Enter the 10 Midterm 1 grades: ");
    for (int i = 0; i < grades.length; i++){
     
    for (int j = 0; j < grades.length ; j++){
     
    System.out.println("MidTerm1 Grades "+(i+1)+": ");
     
    grades[i][j]=input.nextInt();
    }
     
    System.out.print("Enter the 10 Midterm 2 grades: ");
     
    for (int i = 0; i < grades.length; i++){
     
    for (int j = 0; j < grades.length; j++)
     
    System.out.print("Midterm2 Grades "+(i+1)+": ");
    grades[i][j]=input.nextInt();
    }
     
    System.out.print("Enter the 10 Final Exam grades: ");
     
    for (int i = 0; i < grades.length; i++){
     
    for (int j = 0; j < grades.length; j++)
     
    System.out.print("Final Exam Grade "+(i+1)+": ");
     
    grades[i][j]=input.nextInt();
    }
     
    for (int i=0;i<10;i++)
     
    grades[i]=midterm1[i]+midterm2[i]+finalExam
     
    int minGrade=grades[0];
     
    int maxGrade=grades[0];
     
    for (int i=1;i<10;i++)
     
    {
    if (minGrade>grades[i])
     
    minGrade=grades[i];
     
    if (maxGrade<grades[i])
     
    maxGrade=grades[i];
     
    }
     
    System.out.println("The minimum grade is "+minGrade);
     
    System.out.println("The maximum grade is "+maxGrade);
     
    }
    }
    }

    also this is cross posted, thanks
    help w/ storing/scanning numbers in two dimensional arrays - Java Forums


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    Define "get errors all over the place"...compile time errors? Runtime errors? Post them all in full, which will help the diagnosis.

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    heres the errors im getting

     

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Duplicate local variable i
    j cannot be resolved to a variable
    Duplicate local variable i
    j cannot be resolved to a variable
    Duplicate local variable i
    midterm1 cannot be resolved to a variable
    midterm2 cannot be resolved to a variable
    Syntax error, insert ";" to complete Statement
    Type mismatch: cannot convert from int[] to int
    Type mismatch: cannot convert from int[] to int
    Duplicate local variable i
    The operator > is undefined for the argument type(s) int, int[]
    Type mismatch: cannot convert from int[] to int
    The operator < is undefined for the argument type(s) int, int[]
    Type mismatch: cannot convert from int[] to int

    at Assign10_Roberts.main(Assign10_Roberts.java:39)



  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    There really are all sorts of errors!

    For a start, there are multiple for loops inside each other which is causing issues with duplicate variables.

    I would fix these first.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    ok here is my updated code and errors

     
    import java.util.Scanner;
    import java.io.*;
     
     
    public class Assign10_Roberts{
    	public static void main(String[] args){
     
    //input Scanner
    Scanner input=new Scanner(System.in);
     
    int midTerm1=0;
     
    int midTerm2=0;
     
    int finalExam=0;
     
     
    int[][]grades= new int[10][10];
     
    for(int i=0; i<10; i++) {
    // Midterm 1
    grades[i][0]=input.nextInt();
    }
    for(int i=0; i<10; i++) {
    // Midterm 2
    grades[i][1]=input.nextInt();
    }
    for(int i=0; i<10; i++) {
    // Final Exam
    grades[i][2]=input.nextInt();
     
    System.out.println("Enter the 10 Midterm 1 grades: ");
    grades[i][j]=input.nextInt();
     
    System.out.print("Enter the 10 Midterm 2 grades: ");
    grades[i][j]=input.nextInt();
     
    System.out.print("Enter the 10 Final Exam grades: ");
    grades[i][j]=input.nextInt();
    }
     
    {
    	if (minGrade>grades[i])
     
    minGrade=grades[i];
     
    if (maxGrade<grades[i])
     
    maxGrade=grades[i];
     
    }
     
    System.out.println("The minimum grade is "+minGrade);
     
    System.out.println("The maximum grade is "+maxGrade);
     
    }
     
    }
    minGrade cannot be resolved to a variable
    maxGrade cannot be resolved to a variable

    at Assign10_Roberts.main(Assign10_Roberts.java:39)

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    Nowhere is min/maxgrade defined. They need to be defined to be used. For example
    int mingrade = 0;
    int maxgrade = 0;
    //now you can use the variables.
    Typically, since you are looking at min/max values, you want to initialize these values to their opposite extremes, for example initialize mingrade to Integer.MAX_VALUE and vice-versa for maxgrade.

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 6 Times in 3 Posts

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    It also seems that you are trying to access grades[i] outside the scope of the for loop. So I will be undefined as well. Since it is a 2 dimensional array and you only need 3 grades and a total, for 10 students. The array should not be [10][10] but [4][10] which means you have 4 rows and 10 columns.

    If you are still having trouble, let us know.

    Dejan

  8. #8
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    here is my updated code and errors

    public class Assign10_Roberts{
    	public static void main(String[] args){
     
    //input Scanner
    Scanner input=new Scanner(System.in);
     
    int midTerm1=0;
    int midTerm2=0;
    int finalExam=0;
    int minGrade = 0;
    int maxGrade = 0;
     
    int[][]grades= new int[9][9];
     
    for(int i=0, j=0; i<10; i++) {
    // Midterm 1
    grades[i][0]=input.nextInt();
    }
    for(int i=0; i<10; i++) {
    // Midterm 2
    grades[i][1]=input.nextInt();
    }
    for(int i=0, j=0; i<10; i++) {
    // Final Exam
    grades[i][2]=input.nextInt();
     
    System.out.println("Enter the 10 Midterm 1 grades: ");
    grades[i][j]=input.nextInt();
     
    System.out.print("Enter the 10 Midterm 2 grades: ");
    grades[i][j]=input.nextInt();
     
    System.out.print("Enter the 10 Final Exam grades: ");
    grades[i][j]=input.nextInt();
     
     
     
     
    	if (minGrade>grades[i])
     
    minGrade=grades[i];
     
    if (maxGrade<grades[i])
     
    maxGrade=grades[i];
     
    }
     
    System.out.println("The minimum grade is " + minGrade);
     
    System.out.println("The maximum grade is "+ maxGrade);
     
    }
     
    }

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The operator > is undefined for the argument type(s) int, int[]
    Type mismatch: cannot convert from int[] to int
    The operator < is undefined for the argument type(s) int, int[]
    Type mismatch: cannot convert from int[] to int

    at Assign10_Roberts.main(Assign10_Roberts.java:49)

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    You cannot compare an int with an array...since your grades is a 2d array, you need to specify another index
    mingrade < grades[i][0] or something to that effect. Also, re-read my post above, because this code will most likely always produce a mingrade of 0.

  10. #10
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    not sure is this is any better here is what i changed

    import java.util.Scanner;
    import java.io.*;
     
     
    public class Assign10_Roberts{
    	public static void main(String[] args){
     
    //input Scanner
    Scanner input=new Scanner(System.in);
     
    int midTerm1=0;
    int midTerm2=0;
    int finalExam=0;
     
     
    int[][]grades= new int[10][10];
     
    for(int i=0, j=0; i<10; i++) {
    // Midterm 1
    grades[i][0]=input.nextInt();
    }
    for(int i=0; i<10; i++) {
    // Midterm 2
    grades[i][1]=input.nextInt();
    }
    for(int i=0, j=0; i<10; i++) {
    // Final Exam
    grades[i][2]=input.nextInt();
     
    System.out.println("Enter the 10 Midterm 1 grades: ");
    grades[i][j]=input.nextInt();
     
    System.out.print("Enter the 10 Midterm 2 grades: ");
    grades[i][j]=input.nextInt();
     
    System.out.print("Enter the 10 Final Exam grades: ");
    grades[i][j]=input.nextInt();
    {
     
    	public static double getCurrentMin (double [][] input) {    
    		double minGrade = input[0][0];
    	    double maxGrade = input[0][0];
            for (int row = 0; row < input.length; row++){
                for (int column = 0; column < input [row].length; column++){
                    if (minGrade = > input[row][column]){
                        minGrade = input[row][column];
                    }
                }
     
     
    public static double getCurrentMax (double [][] input){    
        double minGrade = input[0][0];
        double maxGrade = input[0][0];
        for (int row = 0; row < input.length; row++){
            for (int column = 0; column < input [row].length; column++){
                if (maxGrade < input[row][column]){
                    maxGrade = input[row][column];        
     
    System.out.println("The minimum grade is " + minGrade);
     
    System.out.println("The maximum grade is "+ maxGrade);
     
    }
     
    }
     
    }
     
    }
     
    }

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error, insert "}" to complete Block
    Syntax error, insert "}" to complete Block
    Syntax error, insert "}" to complete MethodBody

    at Assign10_Roberts.main(Assign10_Roberts.java:44)

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help w/ storing/scanning numbers in two dimensional arrays

    Format your code using tabs. This will make it a lot easier for you to tell where you have missing (or misplaced) closing braces.

    The general convention is for each new block (anything surrounded by curly braces) should be indented, and the closing brace should be un-indented on a newline.

    For example, here's your code with the tabs inserted (I've also put comments in on where the problems with braces are)

    import java.util.Scanner;
    import java.io.*;
     
    public class Assign10_Roberts{
        public static void main(String[] args){
            //input Scanner
            Scanner input=new Scanner(System.in);
     
            int midTerm1=0;
            int midTerm2=0;
            int finalExam=0;
     
            int[][]grades= new int[10][10];
     
            for(int i=0, j=0; i<10; i++) {
                // Midterm 1
                grades[i][0]=input.nextInt();
            }
            for(int i=0; i<10; i++) {
                // Midterm 2
                grades[i][1]=input.nextInt();
            }
            for(int i=0, j=0; i<10; i++) {
                // Final Exam
                grades[i][2]=input.nextInt();
     
                System.out.println("Enter the 10 Midterm 1 grades: ");
                grades[i][j]=input.nextInt();
     
                System.out.print("Enter the 10 Midterm 2 grades: ");
                grades[i][j]=input.nextInt();
     
                System.out.print("Enter the 10 Final Exam grades: ");
                grades[i][j]=input.nextInt();
                {
        // missing curly braces here
        // did you forget to finish your main method?
     
        public static double getCurrentMin (double [][] input) {    
            double minGrade = input[0][0];
            double maxGrade = input[0][0];
            for (int row = 0; row < input.length; row++){
                for (int column = 0; column < input [row].length; column++){
                    if (minGrade = > input[row][column]){
                        minGrade = input[row][column];
                    }
                }
        // missing curly braces here
     
        public static double getCurrentMax (double [][] input){    
            double minGrade = input[0][0];
            double maxGrade = input[0][0];
            for (int row = 0; row < input.length; row++){
                for (int column = 0; column < input [row].length; column++){
                    if (maxGrade < input[row][column]){
                        maxGrade = input[row][column];        
                    // missing curly braces here
            System.out.println("The minimum grade is " + minGrade);
            System.out.println("The maximum grade is "+ maxGrade);
        }
    }
    // these are extra curly braces and should be removed
    }
    }
    }

Similar Threads

  1. help w/ storing/scanning numbers in arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 17th, 2010, 10:55 PM
  2. Two dimensional arrays - HELP!
    By ecco in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 19th, 2010, 12:32 PM
  3. Conversions of Numbers in Arrays
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 1st, 2010, 07:59 PM
  4. Scanning Document Issue
    By redvenice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2010, 08:18 AM
  5. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM