The Artima Developer Community
Sponsored Link

Java Answers Forum
Could Someone Help?

6 replies on 1 page. Most recent reply: Nov 7, 2003 6:58 AM by s

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Ernie Douglas

Posts: 18
Nickname: y2j
Registered: Nov, 2002

Could Someone Help? Posted: Nov 5, 2003 10:56 AM
Reply to this message Reply
Advertisement
Hi, i have been having a problem with this program, everytime i keep trying to fix it, another error keeps coming up, so would someone be able to sort the errors out for me please? thanks
/**
* Write a description of class Statistics here.
*
* @author (Ernie Douglus)
* @version (04/10/2003)
*/
import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class Statistics {
public static void main ( String args[] )
{
int classTotal, mark; //hold user entered numbers
int first = 0, upperSecond = 0,
lowerSecond = 0, third = 0,
pass = 0, fail = 0;

// for the count of each category
int count=0, largest=0, smallest=100, total=0;
//displaying results
double average = 0.0; //float to display correct average
double firstPC = 0.0,
upperSecondPC = 0.0,
lowerSecondPC = 0.0,
thirdPC = 0.0,
passPC = 0.0,
failPC = 0.0;
//displaying averages

String inputString;
//get total in class number
inputString = JOptionPane.showInputDialog(
"Enter the Number of Students in the Class (0 - 20): " );
classTotal = Integer.parseInt ( inputString );
//validate this number

while (classTotal < 0 || classTotal > 20) {
JOptionPane.showMessageDialog(
null, "Must be between 0 and 20",
"Input Error", JOptionPane.ERROR_MESSAGE );
inputString = JOptionPane.showInputDialog(
"Incorrect Entry - Please Re-Enter:");
classTotal = Integer.parseInt (inputString);
}
if (classTotal == 0)
{
JOptionPane.showMessegeDialog(
null, "The Program Is Closing Down",
"Exit Messeage", JOptionPane.INFORMATION_MESSAGE );
}
else
{
//get marks
for (int i = 1; i <= classTotal; i++)
{
inputString = JOptionPane.showInputDialog(
"Student Number" + i + " Mark (0 - 100): ");
mark = Integer.parseInt (inputString);
//validate this number
while (mark < 0 || mark > 100) {
JOptionPane.showMessageDialog(
null, "Must Be Between 0 and 100",
"Input Error", JOptionPane.ERROR_MESSAGE);
inputString = JOptionPane.showMessageDialog(
"Incorrect Entry - Please Re-Enter:" );
mark = Integer.parseInt (inputString);
}//end while
total += mark; // running total to calculate average
if (mark > largest) // check for largest number
largest = mark;

if (mark < smallest) // check for smallest number
smallest = mark;
//calculate category and increment category total
if (mark >69)
first++;
else
if (mark >59)
upperSecond++;
else
if (mark >49)
lowerSecond++;
else
if (mark >38)
pass++;
else
fail++;
} //end for

DecimalFormat twoPlaces = new DecimalFormat ("0.00");
//format float to 2 decimal places
average = (double) total / classTotal;
//ensure correct average using
//cast to float
//calculate percentages
firstPC = ((double) first *100)/classTotal;
upperSecondPC = ((double) upperSecond *100)/classTotal;
lowerSecondPC = ((double) lowerSecond *100)/classTotal;
thirdPC = ((double) third *100)/classTotal;
passPC =((double) pass *100)/classTotal;
failPC = ((double) fail *100)/classTotal;

JTextArea outputArea = new JTextArea ( 6, 40 ); //results display
String output = "Firsts - Number: " + first + "Percentage " + twoPlaces.format(firstPC) + "\n";
output += "Upper Seconds - Number: " + upperSecond + "Percentage" + twoPlaces.format(upperSecondPC) + "\n";
output += "Lower Seconds - Number: " + lowerSecond + "Precentage" + twoPlaces.format (lowerSecondPC) + "\n";
output += "Thirds - Number: " + third + "Percentage" + twoPlaces.format (thirdPC) + "\n";
output += "Passes - Number: " + pass + "Percentage" + twoPlaces.format (passPC) + "\n";
output += "Fails - Number: " + fail + "Percentage" + twoPlaces.format (failPC) + "\n";

output += "Highest Mark: " + largest + "\n";
output += "Smallest Mark: " + smallest + "\n";

output += "Average: " + twoPlaces.format (average) + "\n\n\n";

outputArea.setText ( output );
//title
JOptionPane.showMessageDialog(
null, outputArea, "Student Marks - Ernie Douglus",
JOptionPane.INFORMATION_MESSAGE );
}
System.exit( 0 );
}
}


s

Posts: 23
Nickname: codemonkey
Registered: Nov, 2003

Re: Could Someone Help? Posted: Nov 5, 2003 12:39 PM
Reply to this message Reply
How about a clue as to what kind of errors you are experiencing...maybe what you are doing to cause the errors ...etc also that is the most horribly formatted
code ive ever seen ...very hard to read ...

s

Posts: 23
Nickname: codemonkey
Registered: Nov, 2003

Re: Could Someone Help? Posted: Nov 5, 2003 12:53 PM
Reply to this message Reply
you had a typo on line 48 Messege rather than Message
and were trying to assign void to a String object on 65.
It compiles like this ...im not going to try to figure out
what this thing is sposed to do...why do i feel like
im doing someones homework??

/**
* Write a description of class Statistics here.
*
* @author (Ernie Douglus)
* @version (04/10/2003)
*/
import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class Statistics {
public static void main ( String args[] )
{
int classTotal, mark; //hold user entered numbers
int first = 0, upperSecond = 0,
lowerSecond = 0, third = 0,
pass = 0, fail = 0;

// for the count of each category
int count=0, largest=0, smallest=100, total=0;
//displaying results
double average = 0.0; //float to display correct average
double firstPC = 0.0,
upperSecondPC = 0.0,
lowerSecondPC = 0.0,
thirdPC = 0.0,
passPC = 0.0,
failPC = 0.0;
//displaying averages

String inputString;
//get total in class number
inputString = JOptionPane.showInputDialog("Enter the Number of Students in the Class (0 - 20): " );
classTotal = Integer.parseInt ( inputString );
//validate this number

while (classTotal < 0 || classTotal > 20) {
JOptionPane.showMessageDialog(null, "Must be between 0 and 20",
"Input Error", JOptionPane.ERROR_MESSAGE );
inputString = JOptionPane.showInputDialog("Incorrect Entry - Please Re-Enter:");
classTotal = Integer.parseInt (inputString);
}
if (classTotal == 0)
{
JOptionPane.showMessageDialog(null, "The Program Is Closing Down",
"Exit Messeage",
JOptionPane.INFORMATION_MESSAGE );
}
else
{
//get marks
for (int i = 1; i <= classTotal; i++)
{
inputString = JOptionPane.showInputDialog(
"Student Number" + i + " Mark (0 - 100): ");
mark = Integer.parseInt (inputString);
//validate this number
while (mark < 0 || mark > 100) {
JOptionPane.showMessageDialog(null,
"Must Be Between 0 and 100",
"Input Error",
JOptionPane.ERROR_MESSAGE);

JOptionPane.showMessageDialog(null,
"Incorrect Entry - Please Re-Enter:" );
mark = Integer.parseInt (inputString);
}//end while
total += mark; // running total to calculate average
if (mark > largest) // check for largest number
largest = mark;

if (mark < smallest) // check for smallest number
smallest = mark;
//calculate category and increment category total
if (mark >69)
first++;
else
if (mark >59)
upperSecond++;
else
if (mark >49)
lowerSecond++;
else
if (mark >38)
pass++;
else
fail++;
} //end for

DecimalFormat twoPlaces = new DecimalFormat ("0.00");
//format float to 2 decimal places
average = (double) total / classTotal;
//ensure correct average using
//cast to float
//calculate percentages
firstPC = ((double) first *100)/classTotal;
upperSecondPC = ((double) upperSecond *100)/classTotal;
lowerSecondPC = ((double) lowerSecond *100)/classTotal;
thirdPC = ((double) third *100)/classTotal;
passPC =((double) pass *100)/classTotal;
failPC = ((double) fail *100)/classTotal;

JTextArea outputArea = new JTextArea ( 6, 40 ); //results display
String output = "Firsts - Number: " + first +
"Percentage " + twoPlaces.format(firstPC) + "\n";
output += "Upper Seconds - Number: " + upperSecond +
"Percentage" + twoPlaces.format(upperSecondPC) + "\n";
output += "Lower Seconds - Number: " + lowerSecond +
"Precentage" + twoPlaces.format (lowerSecondPC) + "\n";
output += "Thirds - Number: " + third + "Percentage" +
twoPlaces.format (thirdPC) + "\n";
output += "Passes - Number: " + pass +
"Percentage" + twoPlaces.format (passPC) + "\n";
output += "Fails - Number: " + fail +
"Percentage" + twoPlaces.format (failPC) + "\n";

output += "Highest Mark: " + largest + "\n";
output += "Smallest Mark: " + smallest + "\n";

output += "Average: " + twoPlaces.format (average) + "\n\n\n";

outputArea.setText ( output );
//title
JOptionPane.showMessageDialog(null, outputArea,
"Student Marks - Ernie Douglus",
JOptionPane.INFORMATION_MESSAGE );
}
System.exit( 0 );
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Could Someone Help? Posted: Nov 5, 2003 6:42 PM
Reply to this message Reply
> ...etc also that is the most horribly formatted
> code ive ever seen ...very hard to read ...

Yours too. You just need to use the java tags as described in the Formatting Your Post in the gray box to the right, while writing your post.

For example:

Before:
class IncredibleDigitAnalyzerTechnology
{
public static void main(String args[])
{
IncredibleDigitAnalyzerTechnology idat = new IncredibleDigitAnalyzerTechnology();
for( int index = 0; index < args.length; index++ )
idat.update(args[index]);
System.out.println( idat.getZeroCount() + " zero digits." );
System.out.println( idat.getOddCount() + " odd digits." );
System.out.println( idat.getEvenCount() + " even digits." );
System.out.println( idat.getOtherCount() + " other digits." );
}

private int zero = 0;
private int odd = 0;
private int even = 0;
private int other = 0;

public void update( String s )
{
for( int index = 0; index < s.length(); index++ )
switch( s.charAt(index) )
{
case '0': zero++; break;
case '1': case '3': case '5': case '7': case '9': odd++; break;
case '2': case '4': case '6': case '8': even++; break;
default: other++; break;
}
}

public void reset()
{
zero = 0;
odd = 0;
even = 0;
other = 0;
}

public int getZeroCount() { return zero; }
public int getOddCount() { return odd; }
public int getEvenCount() { return even; }
public int getOtherCount() { return other; }
}

After:
class IncredibleDigitAnalyzerTechnology
{
   public static void main(String args[])
   {
      IncredibleDigitAnalyzerTechnology idat = new IncredibleDigitAnalyzerTechnology();
      for( int index = 0; index < args.length; index++ )
         idat.update(args[index]);
      System.out.println( idat.getZeroCount()  + " zero digits." );
      System.out.println( idat.getOddCount()   + " odd digits." );
      System.out.println( idat.getEvenCount()  + " even digits." );
      System.out.println( idat.getOtherCount() + " other digits." );
   }
 
   private int zero = 0;
   private int odd = 0;
   private int even = 0;
   private int other = 0;
 
   public void update( String s )
   {
      for( int index = 0; index < s.length(); index++ )
         switch( s.charAt(index) )
         {
            case '0':                                         zero++; break;
            case '1': case '3': case '5': case '7': case '9':  odd++; break;
            case '2': case '4': case '6': case '8':           even++; break;
            default:                                         other++; break;
         }
   }
 
   public void reset()
   {
      zero = 0; 
      odd = 0;  
      even = 0; 
      other = 0;
   }
 
   public int getZeroCount()  { return zero;  }
   public int getOddCount()   { return odd;   }
   public int getEvenCount()  { return even;  }
   public int getOtherCount() { return other; }
}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Could Someone Help? Posted: Nov 5, 2003 7:00 PM
Reply to this message Reply
Good one Matt :-) I am not sure how many more time you are going to repeat this before everyone post code to this forum get that [ java][ /java] tag into their head.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Could Someone Help? Posted: Nov 5, 2003 8:34 PM
Reply to this message Reply
I need to get Bill to add a signature feature, so I can have it as my sig!

s

Posts: 23
Nickname: codemonkey
Registered: Nov, 2003

Re: Could Someone Help? Posted: Nov 7, 2003 6:58 AM
Reply to this message Reply
Sorry...my second or third time visiting ...
def makes a difference in the formatting.
Thanks for that heads up though

Flat View: This topic has 6 replies on 1 page
Topic: Server Socket IP Previous Topic   Next Topic Topic: Client to Browser Communication

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use