• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Compile error

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a problem compiling a program. I am trying to create a class to prompts the user for the student�s name and Social Security number, and reprompts for just the Social Security number until it receives a string of 9 Once the name and Social Security number have been entered, examScore should be initialized to zero and score already entered should be initialized to false. When I compile I get an error that state public Stiudent should have class or interface i add class and then i get 20 errors one staing that class has already been defined. here is a copy of my code can you help?

import javax.swing.*;
import java.util.*;
public class Student {
// Instance Variables
String fname ;
String lname ;
String ssn ;
int examscore;
Boolean examentered;




}
public Student {
String fnamString,lnameString,ssnString;
this.fname = fname ;
this.lname = lname ;
this.ssn = ssn ;
this.examscore = examscore ;
this.examentered = examentered ;
do
{

fnameString = JOptionPane.showInputDialog(null,
"Please enter the Students first name:");

fname = fnameString;
}
while (fname <= 0 );
do
{

lnameString = JOptionPane.showInputDialog(null,
"Please enter the Sudents last name:");

width = lnameString;
}
while ( width <= 0 );

do
{

ssnString = JOptionPane.showInputDialog(null,
"Please enter the length of your box with a positive number:");

ssn = ssnString;
}
while (ssn.length() == 9);

examscore = 0;
examentered = false;


}



public void enterscore(studentname) {
scoreString = JOptionPane.showInputDialog(null,
"Please enter the the exam score ranging between 0 - 100:");

examscore = Integer.parseInt(scoreString);
examentered = true;
}

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll make comments on your code below.

First I suggest that you use boolean instead of Boolean. The former is a built-in type, but the later is a class. Using the class could cause you problems because it is a bit more complex. If you use the native boolean type instead, you can just assign true and false to the variable. It isn't quite this easy with the Boolean class.

I think you mean this to be the constructor. A constructor is a function and needs (at least) a set of parentheses after the name. You probably also want to add parameters in between these parentheses. Change it to this:

Of course, you can add more parameters than that if you wish.

Personally, I think that the dialog boxes should not be in the constructor. You should be able to simply create a Student object by passing the necessary information as parameters to the constructor. The Student object shouldn't be worried about where the data came from. It just wants to store and manipulate it.
Another object, say a JFrame can create the dialog boxes and ask for the information then create appropriate Student objects. This design also allows for alternative methods of obtaining the data. For example, another program can create student objects by retrieving the information from a database.
Perhaps this is a bit too much, though. The main thing is that the constructor needs a set of parentheses after the name. That small change will fix the first of the compiler errors.
Please come back with more questions when you need more help.
Keep coding!
Layne
[ February 23, 2003: Message edited by: Marilyn de Queiroz ]
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to do it the way you suggested and still received the same compile error. My code again YOur right I have no idea what your talking about. I was trying to get all the student info in the constructor. Is that impossible? Can my way compile?

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Let's start here.

You have an extra curly brace just above your "public Student" constructor. The compiler thinks that the constructor is outside of your class and is asking for a new class declaration ("an error that state public Stiudent should have class or interface").

Next, you have an extra set of parens in your constructor. Rather than
public Student()(String fname, String lname, String ssn)
you want
public Student(String fname, String lname, String ssn).

There are other problems as well, but hopefully this will get you started.
[ February 23, 2003: Message edited by: Marilyn de Queiroz ]
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I went through my program and started debugging again and like you said I found about 11 errors. I corrected them except I am down to 1 and it say line 73 } expected. I tried closing various parts of the program and cannot figure out what is wrong. Again here is my code.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, it will help TREMENDOUSLY if you clean up the appearance of your code a bit. You should use consistent tabbing within your program. The common practice is to tab in another level after an open curly brace. Maybe a bit of an example will help illustrate:

Notice how there are different levels of tabs in the example above. Also notice that matching curly braces line up neatly. If you change the format of your code so that it is similar to the example above, you should be able to easily see where the missing curly brace should go.
HTH
Layne
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I went through and did what you said I think I corrected the curly brace problem but now I'm back to the same error I wrote you about he first time. Class or interface expected student.java . again a copy of my code
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you have a } after the line boolean examentered; ?
You would do well to be more careful about your indentation (code formatting) as nicely formatted code is a lot easier to read and understand.
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To repeat what the last post said; the curly ends your class. Everything else is outside of the class.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I went through and did what you said I think I corrected the curly brace problem but now I'm back to the same error I wrote you about he first time.

You added a curly brace -- the same one you previously removed -- so you got the previous error. Obviously this was not the place to put the missing curly brace.

If you format your code as advised above, it will be much easier for anyone reading your code (including you) to see where the missing curly brace really belongs.

JavaRanch official Style Guide
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lisa,
I formatted your code to end everybody's suffering. Your code now compiles. However, you still have a number of problems, including the access qualifiers and names of your variables, the logic in your constructor (there should be none or very little), the excessive use of local variables, and readability of your code.

Eugene.
[ February 24, 2003: Message edited by: Eugene Kononov ]
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you elaborate?Remeber I am trying to learn. Are ou saying the code won't do what I am telling? I am planning to write a test program to see if it will do what I am asking.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eugene Kononov:
I formatted your code to end everybody's suffering.


How beautiful it looks. How much easier to read.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just this code should be in the constructor. Take the input variables and store them.

Now the extra code needs to be put into another method, or the method you already have.
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh now I see, something like a a method called student info. If I want to keep track of all the student entered can i put something like Studentname++ in the info method. Also if I want to get the average of all the grades entered.How can start writing the method to average average all the scores. I know the formula but how will it average each score entered? And If I have already enetered a score for a student and I don't want to re-enter the same score ,how can I check it to make sure I haven't entered the score twice?
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to store more than one thing you want some sort of data structure. Array is a good place to start. Make an array called "student_grade", or something like that. Than a methode "enter_grade" which would store the value in the array.
But before you start doing any of that you need to get what you have working, and understand it. Until you do that trying to add functionality will just end up confusing you.
[ February 25, 2003: Message edited by: William Barnes ]
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I di what you said and herre is a copy of my code. I have a succesful compile. I would like to do the dditons without an array. I don't understand the array concept yet. Next week!!!
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added a to string method so i could print out the student information entered to my code. i created the driver to check my output and i received a error in my testbox class. my student class compiled but i have no idea what the problem is. it is only one error in testbox stating cannot resolve symbol. i pasting a copy of my code again


 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what does the error message say?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to give Student a no-argument constructor:

You need it because your test program does:
Student Studentone = new Student();
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternatively, you can pass the correct number of parameters to the constructor. Of course, that means you need to ask the user for input BEFORE you create the Student object.
Regards,
Layne
p.s. Your code STILL isn't formatted as I suggested. If your .java file IS formatted correctly, you should just copy and paste the code here. It should keep the formatting when you use the [ CODE ] [ /CODE ] tags.
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say no agumetns what do you mean?
Are you talking about the consturcuctor or the class? The only thing student are the varibles. in in the constructor I am referencing varibles.
Should I remove this from the constructor.
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yeah, this the error message

C:\javas>c:\j2sdk1.4\bin\javac teststudent.java
teststudent.java:18: cannot resolve symbol
symbol : constructor Student ()
location: class Student
Student Studentone = new Student();
^
1 error
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you say no agumetns what do you mean? Are you talking about the consturcuctor or the class?


He is saying that your class "testStudent" has this line:

Which says give me an instance of the class "Student" using the constructor which doesn't need any input arguments. But you class "Student" only has one constructor, which needs 5 input arguments. So you can either add a no argument constructor, OR pass in the 5 required arguments. Making a no args ctor is a better idea.
Your error message, I think, is telling you the same thing.
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So are you saying I should initialize everythingin my constructor to 0 or just delete the this.fname = fname etc and replace it with new varibles. I am not understandinginput arguments. Are you saying in my constructor I am asking the user to put in fname,lname etc.. I thought I was coding the program to say that in the setname method the values entered are the same in public class public. Should
i just remove the this.fname= fname and add the code to setname to student constructor?
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Layne said:

Alternatively, you can pass the correct number of parameters to the constructor. Of course, that means you need to ask the user for input BEFORE you create the Student object.


If your Student class only has a constructor which accepts 5 arguments, than you will need to know the values for those arguments in order to make the class.
But how can you pass in the students first name (for example), if you haven't asked for it yet?
So in this case it might make more sense to have a constructor which has no arguments. In that constructor you could do nothing.
Than have another method which would run the code which calls asks for first name, last name, ...
In either case your test driver code needs to have the correct number of arguments (even if there are no arguments) for your code to compile.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Sorry if this confuses you more, but it must be said.)
One problem here is that your Student class is doing more than just describing a student. The Student class should only have attributes like: first name, last name, SSN, and methods like: (not sure actually). Instead your Student class is trying to do things like keep the count of the number of students.
It would make more sense to have a Student class with student info only, than another class called something like "TestScores". This new class would contain one or more Student classes. Your test driver would than run the TestScores class.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And these lines of code:

confuse me.
The only way of getting out of this loop is to enter an SSN which doesn't have 9 digits.
Maybe you want to say "while (ssn.length() != 9)"
[ February 26, 2003: Message edited by: William Barnes ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think Willims is right...
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's exactly what I am tring to do. So are you saying to put the student name and ssn in one class and then enter score in another and etc..
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to understand the problem, which actually I don't. What were the original requirements? We can all help you get your code to run, but if it doesn't solve the problem it isn't much help.
This is from your first post:


I am trying to create a class to prompts the user for the student�s name and Social Security number, and reprompts for just the Social Security number until it receives a string of 9 Once the name and Social Security number have been entered, examScore should be initialized to zero and score already entered should be initialized to false.


So working from that, and if you don't want to do everything in one class, we are back to you needing a no-argument constructor. Since the only constructor you have needs to know the first name, last name, ...
We don't seem to be making any progress here.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the general framework I would start with. Which gives an example of a no args ctor.
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well guys the bells finally went off. I got the program to comile and work but during my error checking I realized that I did not set a range for examscore so I put a do while in examscore and a sucessful compile but it isn't functining properly. It still prints the numbers less tha 0 and greater than 100
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This line of code says you will loop while the score is less than or equal to 0 AND greater than or equal to 100.
I would be included to think that you want to use an OR here, instead of the AND.
[ February 26, 2003: Message edited by: William Barnes ]
 
buckaroo
Posts: 401
Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lisa;
Here is my shot at readability of your code:
Student class:

Class Driver:

It all compiles but returns a runtime error. I am not quite clear what it is you want overall and my knowledge of java.swing is limited - but I am learning and will use your example to learn more.
HTH
doco
[ February 26, 2003: Message edited by: doco mastadon ]
 
Donald R. Cossitt
buckaroo
Posts: 401
Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boy! Did that ever scatter out! I used the advice from this site and changed from tabs to spaces x 4. It sure doesn't look this way on my end. What happened?
doco
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Could you elaborate?Remeber I am trying to learn. Are ou saying the code won't do what I am telling?


A programmer writes the code that works. A good programmer writes the code that is easy to read, understand, modify, reuse, and maintain. There are some sophisticated strategies to achive perfection (design patterns and refactoring), but there are also some easy to follow elementary guidelines:
-- use meaningful variables names
-- follow Java Coding Conventions: instead of "numberofstudents" use "numberOfStudents" name
-- give your class level variables and methods the most restrictive access qualifier possible
-- reduce the number of local variables
-- declare the local variables just before they are used (as opposed the beginning of the method)
-- have your methods short and focused
-- have your classes cover a narrow set of responsibilities
Whatever you do, remember that it is very likely that someone else will be modifying/maintaining your code. The tolerance of people in this forum is truly overwhelming, given the fact that you still post the code with inconsistent indentation.
Eugene.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by doco mastadon:
Boy! Did that ever scatter out! I used the advice from this site and changed from tabs to spaces x 4. It sure doesn't look this way on my end. What happened?



Apparently your editor is "helping" you by changing the spaces to tabs. Your code in the above post if chock full of tabs.
 
Donald R. Cossitt
buckaroo
Posts: 401
Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me guess; the answer is to actually use the space bar for indentation? That sucks. :roll:
doco
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doco, What editor are you using?
In some editors you can set tabs to 4 spaces and tell it to expand tabs to spaces.
 
I don't always make ads but when I do they're tiny
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic