darkagn 315 Veteran Poster Featured Poster

Hi freddiecool and welcome to DaniWeb,

Not sure if this is your problem, but your variables should be initialised as private. ie:

private pryDialog pd = new pryDialog();
private int option = 0;
darkagn 315 Veteran Poster Featured Poster

Is guessI the String or the JTextField in the GUI? If it is the text field then rajatC is correct :$

darkagn 315 Veteran Poster Featured Poster

if guessI is a String and word is a String then you can use the equals or equalsIgnoreCase methods to compare them.

if (guessI.equals(word)) {
// true if guessI and word are exactly the same
}
if (guessI.equalsIgnoreCase(word)) {
// true if they are the same regardless of case
}

There is no need to use a char array.

darkagn 315 Veteran Poster Featured Poster

change from show() to setVisible(true) .... and find a better tutorial, that one's outdated.

Masijade's right here - find a tutorial on swing rather than awt as the swing packages have mostly overtaken the functionality of the awt packages.

darkagn 315 Veteran Poster Featured Poster

Take a look at the Java API in the Integer class, especially the parseInt and intValue methods. The Java API is located at http://java.sum.com/javase/6/docs/api - you should probably bookmark this page if you want to learn Java.

Sorry, there is a spelling mistake in that URL. It should be http://java.sun.com/javase/6/docs/api - sorry about that! :$

darkagn 315 Veteran Poster Featured Poster

I don't understand your comment...can you please just show it to me cause I am new in java so I am still unfamiliar of the things that you are saying...

Take a look at the Java API in the Integer class, especially the parseInt and intValue methods. The Java API is located at http://java.sum.com/javase/6/docs/api - you should probably bookmark this page if you want to learn Java.

I am having errors from:

out.println(average);  //it does not recognize average 
  StringTokenizer st=new StringTokenizer(number);  //it does not recognize StringTokenizer
  for (int i=0; i<number.length; i++){   //it does not recognize number.length

This is the error that shows...
root cause

java.lang.Error: Unresolved compilation problem:
average cannot be resolved

AverageNumber.doGet(AverageNumber.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

average has not been initialised before this line, so it doesn't make sense to print it out. The StringTokenizer error is caused because you need to import it by adding the following code at the very begining of your code:

import java.util.StringTokenizer;

number.length doesn't make sense because number is initialised as an int. I'm not sure what you are trying to do here, but either number needs to be an int array or the for statement should be < number as opposed to number.length.

darkagn 315 Veteran Poster Featured Poster

Instead of

System.out.println(st.nextToken());

you should assign the nextToken to a String then get the intValue from it using the Integer.parseInt method. Then you will be able to sum your numbers.
To get the average you will need to cast either the sum or the number to a double or float because the result of int / int is always an int. This will not always be accurate.

darkagn 315 Veteran Poster Featured Poster

When the user inputs the numbers, you will have a String. Take a look at the API for the StringTokenizer class - I think you will see that this will be a useful class to separate the numbers (called tokens) by the commas (separators).

Hope this has given a hint in the right direction,
darkagn

darkagn 315 Veteran Poster Featured Poster
for (i = intArray.length-1; i>=0 ; i--) { // sort at end of list

            for (j = 0 ; j < i ;j++) { // "bubble" the largest value to the end

                if (intArray[j] > intArray[j+1]) { // compare each element to the next one

                    temp = intArray[j]; // swap the elements by first setting a temp variable

                    intArray[j] = intArray[j+1]; // then copying one to the other

                    intArray[j+1] = temp; // and recopying the temp
               }
        }
                   // print out the results
                   System.out.println(" Array content after sorting");

                   for(i=0 ; i < intArray.length;i++) { // for each element

                             System.out.print(intArray[i] +""); // print it!

                 }
       }
darkagn 315 Veteran Poster Featured Poster

Looks like darkagn and I replied at about the same time. I hadn't refreshed my page while typing my reply.

I think you explained it much better than I did though :)

darkagn 315 Veteran Poster Featured Poster

It looks to me like it is a flag to tell whether something has been inputted or not. If it hasn't, isFixReg is set to true, and pressing a number (for example) starts a new String. Otherwise, isFixReg is false and entering a number will add to the existing text.

It might pay to check this with T.Yamazaki via the email address that is provided, but since the code was written over 11 years ago you may not get a response from that address.

As an aside, this is why it is important to provide meaningful names and/or comments for methods, variables, classes etc. Just because the author of the code knows what it means doesn't mean that they will always be responsible for maintenance of the code.

Jishnu commented: Nice post :) +2
darkagn 315 Veteran Poster Featured Poster

I think masijade was talking about the method

Integer.parseInt(String s, int radix)

Check out the API for this static method which parses a String s to an Integer value in the base radix.

darkagn 315 Veteran Poster Featured Poster

Maybe you could create a fairly simple Score class that records the player's name and score, and use an ArrayList of Score objects to record the order of high scores. Then to display them, possibly use a JDialog or JOptionPane? Check out sun's tutorial on using dialogs for more info on how to implement this.

Good luck,
darkagn :)

darkagn 315 Veteran Poster Featured Poster

No problem :)

darkagn 315 Veteran Poster Featured Poster

Actually it wasn't that I didn't understand the question, I just wanted cbalu to think about it before I suggested a reason why interfaces can't have static methods. It was my way of getting cbalu to put in some effort to the question.

darkagn 315 Veteran Poster Featured Poster

Hi cbalu,

Do you know what a the difference between a static and non-static method is? Do you know what an interface is? Let me know your thoughts and I will try to help from there. :)

darkagn 315 Veteran Poster Featured Poster

Also, in Employee's constructor you set the variable emp to the String paramater, but Doctor gets a name varaiable set. Is this correct?

darkagn 315 Veteran Poster Featured Poster

Yes, instead of Object job; put Employee job;

This will allow you to create any Employee or child-class. This is called polymorphism and is a fundamental concept of Object Oriented programming. Google 'polymorphism in java' for more info.

darkagn 315 Veteran Poster Featured Poster

Ah I just realised that you are not actually calling the eatDot() method anywhere in your code. I think it should probably be called immediately before the call to repaint()...

darkagn 315 Veteran Poster Featured Poster

Ah yes Ezzaral is correct - sorry for that :$

darkagn 315 Veteran Poster Featured Poster

Set the text for a JLabel at the bottom of the GUI to be the following String:

java.util.Date currentDate = new java.util.Date();
String date = currentDate.toString();
darkagn 315 Veteran Poster Featured Poster

Hi piers,

The problem is that you posted a huge chunk of code that any of your fellow students could copy easily by searching for "scanner" in google. I think that is what the person is implying (possibly one of your professors?) Next time maybe just post the relevant code?

darkagn 315 Veteran Poster Featured Poster

I'm pretty sure this should work:

char c;
cin >> c;

I haven't tried it, but it only allows for a one character input I think...

darkagn 315 Veteran Poster Featured Poster

Hi alexasmith,

The problem you were having has to do with scope. Basically you declare your variable myInts1 in your main method and pass it as a parameter to your readArray method (this is all correct). However, inside your readArray method, the name myInts1 is not defined - it is outside the scope of this method. Inside your readArray method it is called array (which is the name it is given as the parameter). In order to perform calculations on myInts2, you need a second call to your readArray method passing myInts2 to it. This is also called array inside the readArray method - basically the readArray method can perform calculations on one array at a time (the array that gets passed in when the method is called).

I hope this has explained your problem further.

Enjoy!

darkagn

darkagn 315 Veteran Poster Featured Poster

Also, can I suggest putting a few extra println statements in while you are trying to debug your program. Try something like

num = scan.nextInt();
System.out.println("### num = "+num+(" ###");

This will confirm whether you are reading the correct value. Put this sort of statement after every input and don't forget to display your output so you can see the result. This will help to decifer what is happening in your program. Just don't forget to comment them all out before handing in your final assignment!

Cheers,

darkagn

darkagn 315 Veteran Poster Featured Poster

What error are you getting? Or is it just a logic error somewhere? Nothing really stands out, but if you can describe how it isn't working I might be able to find something...

darkagn 315 Veteran Poster Featured Poster

A couple of comments for you to consider:

  • You will need a variable of type float or double to store and print the average
  • You can use modulo arithmetic to get the digits of a number (%)

These two hints should point you in the right direction. Let me know if I am being too vague but I am trying to give you a hint without telling you exactly what to do... ;)

darkagn 315 Veteran Poster Featured Poster

What will happen if devices = 70?

darkagn 315 Veteran Poster Featured Poster

Before you start thinking in terms of code, maybe try understanding the steps involved in english. Write out some dot points on a piece of paper to get a better idea of what is going on. I'll give you step one:

Display text: "Please enter employee's name." Get input and store it somewhere.

Don't worry about how you are going to do this at this stage, just work out what needs to be done. This is an important stage of the problem solving process called design and it is a stage that requires practice, but I can't stress how important design is in the real world with complex problems to solve.

So give that a go, repost what you think the steps are and let us know what you can / can't code.

darkagn 315 Veteran Poster Featured Poster

When debugging I often find it helpful to add some println statements to see exactly what is happening. In your case I would insert a println statement immediately after

transval=Console.in.readDouble();

Something like

System.out.println("### transaval = "+transval+"###");

will help find out what is happening to your variable. Obviously this is the only thing that can exit your while loop both in your condition and your break statement. I personally have never used Console.in but prefer to use an InputStreamReader class (often in conjunction with a BufferedReader) and then parse the input received. But I hope I have given you a pointer in finding your error.

darkagn 315 Veteran Poster Featured Poster

What is the exact error that you are getting? Do you get it when you try to compile or when you run it?