0
import java.util.Scanner;
import java.awt.Container;
importjavax.swing.*;
public class AM {
public static void main(String[] args) 
{
String s0 = JOptionPane.showInputDialog( "no. of Students" );
  int array [];
  array = new int[s0];
  Scanner s = new Scanner ( System.in);
  String s1 = JOptionPane.showInputDialog( "Enter the Name of Student" );
  String s2 = JOptionPane.showInputDialog("Enter the Exam Marks" );
  String output = "Name of the Student\tExam Marks\n";
  for ( int counter = 0; counter < array.length; counter++ )
     output += counter + "\t" + array[ counter ] + "\n";
  JTextArea outputArea = new JTextArea();
  outputArea.setText( output);
  JOptionPane.showMessageDialog( null, outputArea,
                 "Analysis of Exam Marks",
     JOptionPane.INFORMATION_MESSAGE );
 }
}

Why the array = new int [ ] <----can not put s0 into here and how to make the array be the no. of the student?

And that WHEN I use array = new int [5]; the result :

no.__Marks

1_____0

2_____0

3_____0

4_____0

5_____0

How to make this "0" be the Exam Marks?

2
  • Sorry but What?! Also you should remove the "Enter code here" line ton have it compilable ;) Commented Mar 23, 2016 at 10:57
  • Well s0 is a String, of course you can't use it as the size of an array. You'd need to cast it to Integer first. @questionare "Enter code here" is not part of his code, it's what happens when you try creating the code block on SO first before you insert the code and then forget to replace it. Commented Mar 23, 2016 at 11:08

3 Answers 3

1

Why the array = new int [ ] <----can not put s0 into here and how to make the array be the no. of the students??

If you read the syntax from the documentation, you should know why. You are supposed to place an integer value in the square brackets to allocate the size for the array. Strings are not allowed in there.

array = new int[size];  //where size is int

You can parse your String into int before, placing it in the brackets:

int numStudents= Integer.parseInt(s0);
array = new int[numStudents];  

HOW to make this "0" be the Exam Marks??

If you want to prompt the user to enter the marks for all students, then you need to enclose your prompting in a loop:

for(int x=0; x<numStudents; x++){
    String name = JOptionPane.showInputDialog( "Enter the Name of Student" );
    String str = JOptionPane.showInputDialog("Enter the Exam Marks" );
    int marks = Integer.parseInt(str);
    arrNames[x] = name;
    arrMarks[x] = marks;  
}

So your complete program will look like:

public static void main(String[] args){
    int numStudents = Integer.parseInt(JOptionPane.showInputDialog("Enter number of students."));

    String[] names = new String[numStudents];
    int[] marks= new int[numStudents];

    //Populate student data
    for(int x=0; x<numStudents; x++){
        String name = JOptionPane.showInputDialog( "Enter the Name of Student" );
        String str = JOptionPane.showInputDialog("Enter the Exam Marks" );
        int marks = Integer.parseInt(str);
        arrNames[x] = name;
        arrMarks[x] = marks;  
    }

    //Display student data    
    StringBuilder sb = new StringBuilder();
    sb.append("Name of the Student\tExam Marks\n");
    for(int x=0; x<numStudents; x++)
        sb.append(names[x] + "\t" + marks[x] + "\n");

    JTextArea outputArea = new JTextArea();
    outputArea.setText(sb.toString());
    JOptionPane.showMessageDialog( null, outputArea, "Analysis of Exam Marks", JOptionPane.INFORMATION_MESSAGE );    
}
Sign up to request clarification or add additional context in comments.

Comments

0

please, update your code's main method accordingly,

String s0 = JOptionPane.showInputDialog( "no. of Students" );
Integer number_of_students = 0;
try {
    number_of_students = Integer.parseInt(s0);


    String output = "Name of the Student\tExam Marks\n";
    for ( int counter = 1; counter <= number_of_students; counter++ ){

        String s1 = JOptionPane.showInputDialog( "Enter the Name of Student - " + counter );

        String s2 = JOptionPane.showInputDialog("Enter the Exam Marks for " + s1 );

        output += counter + "\t" + s1 + "\t" + s2 + "\n";
    }

    JTextArea outputArea = new JTextArea();

    outputArea.setText( output);

    JOptionPane.showMessageDialog( null, outputArea,
            "Analysis of Exam Marks",
            JOptionPane.INFORMATION_MESSAGE );

} catch (Exception e) {
    JOptionPane.showMessageDialog( null, "Please , enter numeric values only",
            "Analysis of Exam Marks",
            JOptionPane.INFORMATION_MESSAGE );
}

** Success Out-Put :**

enter image description here

If user entered non-numeric value then out-put looks likewise,

enter image description here

enter image description here

3 Comments

I want to ask one more question. How to use the variable input (s0) and the data to calculate such as use the data to calculate the mean or check the values are/not over 40marks?? @vishal gajera
See first of S0, S1,S2 are type of String. So if you want to perform mathematic operation then must need to convert into Numeric form likewise, Integer.parseInt(S0 or S1 or S2); which will return Integer type of value. Then use it further for your desire operation perform.
If you are satisfied by my answer then please upvote+accept my answer thanks!
0

Edit code to accept Name and marks N number of times, where n is count of records. Below code should solve your purpose.

 public static void main(String[] args) throws IOException {
        String s0 = JOptionPane.showInputDialog( "no. of Students" );
          int array [];

          array = new int[Integer.parseInt(s0)];
          Scanner s=null;
          String s1=null;
          String s2 =null;

          String[] arrNames= new String[array.length];
          String[] arrMarks= new String[array.length];

          for(int i=0;i<array.length;i++){
          s = new Scanner ( System.in);
          s1 = JOptionPane.showInputDialog( "Enter the Name of Student" );
          arrNames[i]=s1;
          s2 = JOptionPane.showInputDialog("Enter the Exam Marks" );
          arrMarks[i]=s2;
          }

          String output = "Name of the Student\tExam Marks\n";
          for ( int counter = 0; counter < array.length; counter++ )
             output += arrNames[counter] + "\t" + arrMarks[counter] + "\n";

          JTextArea outputArea = new JTextArea();

          outputArea.setText( output);

          JOptionPane.showMessageDialog( null, outputArea,
                         "Analysis of Exam Marks",
             JOptionPane.INFORMATION_MESSAGE );


    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.