# Problem with calculating Student Quizscores/ Averages

Page 1 of 1

## 14 Replies - 638 Views - Last Post: 19 July 2012 - 11:24 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=286316&amp;s=e6645586ee2d2d47b903ab7686aede9b&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JoePatriot

Reputation: -20
• Posts: 78
• Joined: 12-July 12

# Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 09:55 AM

Anyone have any ideas on how to repair this?

Here is the code:
```import java.io.IOException;
import java.math.*;
import javax.swing.JOptionPane;

public class Student
{
public static void main(String[] args)
{
//double(quiz1, quiz2, quiz3, quiz4, average, TotalScore);
String name = JOptionPane.showInputDialog(null, "What is your name?");
JOptionPane.showMessageDialog(null, "Hello, " + name + ".");
String quiz1 = JOptionPane.showInputDialog(null, "Enter Quiz 1 score");
JOptionPane.showMessageDialog(null,name + ", your Quiz 1 score is " + quiz1 + " points.");
String quiz2 = JOptionPane.showInputDialog(null, "Enter Quiz 2 score");
JOptionPane.showMessageDialog(null,name + ", your Quiz 2 score is " + quiz2 + " points.");
String quiz3 = JOptionPane.showInputDialog(null, "Enter Quiz 3 score");
JOptionPane.showMessageDialog(null,name + ", your Quiz 3 score is " + quiz3 + " points.");
String quiz4 = JOptionPane.showInputDialog(null, "Enter Quiz 4 score");
JOptionPane.showMessageDialog(null,name + ", your Quiz 4 score is " + quiz4 + " points.");

String TotalScore = quiz1 + quiz2 + quiz3 + quiz4;
JOptionPane.showMessageDialog(null, name + ", your totalscore is " + TotalScore);

String average = quiz1 + quiz2 + quiz3 + quiz4 / 4;
JOptionPane.showMessageDialog(null, name + ", your current Average is: " + average + "%");
}
}

```

and the output:

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '/'
first type: java.lang.String
second type: int
at Student.main(Student.java:28)
Java Result: 1
BUILD SUCCESSFUL (total time: 14 seconds)

Is This A Good Question/Topic? 0

## Replies To: Problem with calculating Student Quizscores/ Averages

### #2 macosxnerd101

• Self-Trained Economist

Reputation: 9158
• Posts: 33,979
• Joined: 27-December 08

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 09:59 AM

You can't perform math on Strings. Also, you could simplify this a lot with loops.
```int total = 0;
for(int i = 1; i <= 4; i++){
String input = JOptionPane.showInputDialog("Enter quiz " + i);

//notice how I use Integer.parseInt() to convert a String to an int
total += Integer.parseInt(input);
}

//now divide total/4 to get the average

```

### #3 CasiOo

• D.I.C Lover

Reputation: 1008
• Posts: 2,250
• Joined: 05-April 11

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:05 AM

On top of what has already been said, your calculation is wrong
```quiz1 + quiz2 + quiz3 + quiz4 / 4;

```

Should be
```(quiz1 + quiz2 + quiz3 + quiz4) / 4;

```

### #4 JoePatriot

Reputation: -20
• Posts: 78
• Joined: 12-July 12

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:09 AM

How would you recommend That I include this in my current code?

### #5 x68zeppelin80x

• D.I.C Regular

Reputation: 50
• Posts: 279
• Joined: 07-March 09

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:18 AM

JoePatriot, on 19 July 2012 - 01:09 PM, said:

How would you recommend That I include this in my current code?

```int total = 0;
int i = 1;

while(i <= 4){
String input = JOptionPane.showInputDialog("Enter quiz " + i++);

//notice how I use Integer.parseInt() to convert a String to an int
total += Integer.parseInt(input);
}

//now divide total/4 to get the average
int avg = total / i;

```

### #6 macosxnerd101

• Self-Trained Economist

Reputation: 9158
• Posts: 33,979
• Joined: 27-December 08

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:21 AM

Let's not do all his work for him, x68zeppelin80x.

Quote

How would you recommend That I include this in my current code?

Look at the logic. What does each step do? Make an attempt at reworking the logic based on what you are trying to accomplish.

### #7 JoePatriot

Reputation: -20
• Posts: 78
• Joined: 12-July 12

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:25 AM

I like your idea of using loops, though i don't believe i should do that yet.

### #8 macosxnerd101

• Self-Trained Economist

Reputation: 9158
• Posts: 33,979
• Joined: 27-December 08

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:25 AM

Don't be afraid to use it, just because your teacher hasn't covered it yet. Most things you will learn about programming you will end up teaching to yourself.

### #9 x68zeppelin80x

• D.I.C Regular

Reputation: 50
• Posts: 279
• Joined: 07-March 09

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:28 AM

macosxnerd101, on 19 July 2012 - 01:21 PM, said:

Let's not do all his work for him,

All I did was take the code you provided him, and make 'i' global. If anyone helped him it was you.

### #10 JoePatriot

Reputation: -20
• Posts: 78
• Joined: 12-July 12

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:28 AM

I know, I just don't want to become too involved before I'm ready to.

### #11 macosxnerd101

• Self-Trained Economist

Reputation: 9158
• Posts: 33,979
• Joined: 27-December 08

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:31 AM

Quote

All I did was take the code you provided him, and make 'i' global.

You basically finished the assignment. Also, 'i' is not a global. A global is a static variable.

Quote

I know, I just don't want to become too involved before I'm ready to.

You're ready to take the leap. Loops aren't hard. They make your life easier and your code cleaner. Don't hold yourself back.

### #12 JoePatriot

Reputation: -20
• Posts: 78
• Joined: 12-July 12

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:56 AM

Hey, thanks guys, I finally got it going.

here is the new code.
```import java.io.IOException;
import java.math.*;
import javax.swing.JOptionPane;

public class Student
{
public static void main(String[] args)
{
//double(quiz1, quiz2, quiz3, quiz4, average, TotalScore);
String name = JOptionPane.showInputDialog(null, "What is your name?");
JOptionPane.showMessageDialog(null, "Hello, " + name + ".");

int TotalScore = 0;
for(int i = 1; i <= 4; i++)
{
String input = JOptionPane.showInputDialog("Enter quiz " + i);
TotalScore += Integer.parseInt(input);
}

JOptionPane.showMessageDialog(null, name + ", your totalscore is " + TotalScore);

int average = TotalScore / 4;
JOptionPane.showMessageDialog(null, name + ", your current Average is: " + average + "%");
}
}

```

### #13 macosxnerd101

• Self-Trained Economist

Reputation: 9158
• Posts: 33,979
• Joined: 27-December 08

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:59 AM

### #14 jon.kiparsky

• Pancakes!

Reputation: 5603
• Posts: 9,044
• Joined: 19-March 11

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 11:00 AM

macosxnerd101, on 19 July 2012 - 12:31 PM, said:

Also, 'i' is not a global. A global is a static variable.

Interesting statement. I think I can see how that would make sense, but could you unpack it a little?

### #15 macosxnerd101

• Self-Trained Economist

Reputation: 9158
• Posts: 33,979
• Joined: 27-December 08

## Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 11:24 AM

Variable scope is defined in three ways: globals, instance fields, and local variables. Globals are static variables in a class. Static variables are associated with the class rather than the individual objects, and are invoked using ClassName.globalName. Globals are often times constants.

Instance variables are declared in the class as well, but not in any methods, and without the static keyword. Instance variables are associated with the individual Objects. For example, if you have a Person class, name, age, and gender would be instance variables. Since each Person can have a unique name, age, and gender.

Local variables are declared inside methods (or blocks of code in the class). Their scope is limited to said method or block of code. For example, given the code below, a is local to foo(). However, x is local to the if statement within foo(). A local variable cannot be accessed beyond its context. So a cannot be accessed outside of foo(), and x cannot be accessed outside of the if statement.
```public void foo(){
int a = 3;

if(a == 5){
int x = 3;
}
}

```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }