713 Posted Topics
Re: Gosh this sounds a lot like a homework question. I'm going to assume it is one, and I'm going to be pretty vague. If I fly into San Francisco and I need a vehicle for the week, I go to the car rental stand. I don't ask the person at … | |
Re: [CODE]for(count = 0; count <= values.length; count++)[/CODE] If you use count as an index into values in this loop, it will take you out of bounds, since the last index in values is (values-1). Using (count+1) as the index is just icing on the cake - it puts you out … | |
Re: Can't diagnose it visually - ztini may be right, a problem with your input (is there a leading line without semicolons? That would do it) - but here's two suggestions for diagnosis. 1) you're doing this in a loop - two loops, actually. To pinpoint the error more precisely, put … | |
Re: [CODE] if (sel[i] > max) max = i; [/CODE] This may be your trouble. Is max meant to be a value, or an index? | |
Re: James - that's the answer that came to me, but why doesn't it then perform the increment? Clearly the original writer of the code was confused, there's no need to have the assignment there. However, it should work all the same. Suppose it were [CODE]b = a++;[/CODE] You'd expect the … | |
Re: Scope problem. Declare failurePoint at a higher context: [CODE] boolean FailurePoint = false; for (int i = 1; i < cnodes.size(); i++) { //code ... } [/CODE] | |
Re: To convert from base N to base ten: start your accumulator variable ("decimal" in your program) at zero start at the least significant (rightmost) digit For each digit, the decimal value is (digit * (N^offset)) where offset is the number of places to the left of the decimal point. (starting … | |
Re: Looks to me like the problem is the open curly at line 24: [CODE] if (!cnodes.contains(node)) { cnodes.add(node); [/CODE] or rather the lack of a close brace. This means that if the condition is true, you skip all the way down to the end, past your return statements. | |
Re: You wouldn't be running this as an applet, would you? | |
Re: Or maybe you meant [URL="http://www.libertyorchards.com/category/Aplets_and_Cotlets"]these[/URL]? Tasty! | |
Re: I smell homework here... :) Okay, so start us off. You have a text file. Can you open it and read lines from it, in a loop? Go ahead and do that, just dumping the lines straight to the screen. That'll make a good first step. | |
Re: How do you get an indexoutofbounds exception for an index of zero? I suspect that count was inadvertantly initialized to zero, which means there isn't any array there. The main problem visible in the original posting - which jkp corrected, but failed to call attention to, is the for loop … | |
Re: No, that's your assignment. What's your question? | |
Re: Get a piece of paper and walk through it. [ICODE]fun(5): print 5 decrement 5 if 4>0, call fun(4) >fun(4) >print 4 >decrement 4 >if 3>0, call fun(3) >>fun(3) >>... >>>fun(2) >>>>fun(1) >>>>print 1 >>>>decrement 1 >>>>0 is not > 0, so don't fun(0) >>>>then what? >>>then what? >>then what? >then … | |
Re: And what's the problem you're having? Does the code compile? If the code compiles, what happens when you run it, and how is that different from what you wanted? Please be specific when you ask a question, so you can get the answer you're looking for. | |
Re: You have an algorithm that's pretty clearly stated, all you need to do is code it up. You're right to think that a loop is a good approach: any time you see the same computation repeated n number of times, a loop is likely to come into the picture. Now … | |
Re: Java is case-sensitive. There's a difference between "string" and String, and between "system" and "System". Capitalize those two words and you should be okay. Java style suggests that class names be in the form MyClassName (first letter of each word in caps) and variable/method names in the form myMethodName (initial … | |
Re: For number 6, that seems like a sound start. For #7, a for loop seems like it'll be useful. For # 8, I'm thinking a while loop. I hope that helps a little. Generally, it's useful to think out a problem like this on paper. Suppose for #7 I give … | |
Re: You've sort of petered out in the middle of a loop. I think you've got the right idea, but you're stuck on the execution. Some tips: -if you have the array declaration in the middle of the loop, it'll overwrite itself each time, and you won't be able to address … | |
Re: You can see from the factorial program that your recursive process works by calling itself on a simpler case, moving towards a trivial base case. For factorial, that base case is: when n = 0, factorial(n) = 1. For any positive integer, you reduce towards that case by decrementing. For … | |
Re: One exit is not something that I would hold up as an ideal - it's one way to get a thing done. The important thing is that a method's logic should be clear, and every path through it should be obvious. Often that means skipping out of the method when … | |
Re: Help us out a little. What do you expect to see, and what are you seeing instead? Stating your problem clearly won't just help me and others to solve it, it might actually lead you to a solution. | |
Re: What this is telling you is that the InputStreamReader does not define an "indexOf" method. This makes sense, since you're talking about a stream, not a persisent entity. It looks like your indexOf is trying to find the position of the input character in the target String - this would … | |
Re: - StringBuilder is definitely the way to go if you want to assemble a String. Doesn't make a lot of difference in this sort of thing, but you want to get in the habit now, so later on when it matters, you aren't wasting cycles. My rule is, any time … | |
Re: Looks like it's meant to be a constructor, isn't it? Whatever it is, if you take out that line and one of your close curlies, you might have a legal program that does some calculation and then displays a JFrame with nothing in it. At least, the rest of it … | |
Re: You want to put your prompt ("Enter the number of stars in row N") in a for loop, with rows as the upper bound. | |
Re: Oh, dear. No, I'm afraid this is a Java forum. You know, Java, the programming language? The one that isn't HTML, the markup language? Oh, never mind... | |
Re: Do you mean something like putting a getTextFromTextField1() method in class1, which class2 would call? That's probably what I would do. | |
Re: varshakite - Doing the guy's homework for him isn't much of a help. Much better to lead him to the answer, so he has to make the fixes himself. That way he learns something from the exercise, and you improve your skills as a writer in the process - you … | |
Re: One piece of confusion that needs to be cleared up here is variable scope. Briefly: Variables exist in the context in which they are declared, and any context contained within that context. Context is defined by curly braces. (more precisely, they exist from the moment they are declared until the … | |
Re: You want to look into OO design a little more. If you're putting these things into an array, there must be a reason they're ending up there - that reason should give you a supertype, an abstract class that you can extend to the classes you need. If you find … | |
Re: For your stated purpose, you just need an Ubuntu install disk (or bootable flash drive) and a machine with Windows installed. As for learning to write an OS, I'd say you probably want to start smaller than that. IIRC, Torvalds was trying to write a terminal emulator when he found … | |
| |
Re: You want to sort Strings? Sure, there's library code for that. Strings are Comparable, so if you put them in an array, you can call the sort() method - see the sun reference for [URL="http://download.oracle.com/javase/1.4.2/docs/api/java/util/Arrays.html"]Arrays[/URL]. | |
Re: This is in a GUI, I take it? Do you want to validate as they type, or after they trigger a "submit" (via enter key or pushing a button or whatever)? If it's the latter, just get the text as a String, see if it can be parsed as an … | |
Re: I was confused by James' post at first, and then I realized that what he calls "brackets" are what I call "parentheses". Brackets, to me, are [] and {}. The trouble here is with (). [QUOTE]Next time post your code in code tags and post the complete compiler error message … | |
![]() | Re: - I agree that Bird is a class, not an interface. For the most part, interfaces define capabilities, classes define things and types of things. "Aviator" might be an interface, it would tell you that implementing classes have a method "fly()". -Since you don't want to have a generic Bird … ![]() |
Re: [QUOTE]im thinking i didnt do the "cases" right [/QUOTE] You're right - pcpick is not initialized in all of the possible branches of your switch statement - if you get something other than 1, 2, or 3, it's never given a value, and that's not allowed. Easiest solution is to … | |
Re: ... in the setValue(String) method, that is. [CODE] public void setValue(String value){ double s = Double.parseDouble(value); }[/CODE] This sets a local variable, which is then lost when the method ends. Add Sanjay's line (above) and you're set. | |
| |
Re: You'll need at least one class if you're going to make this in java, but we'll let that be. You're going to have to figure out what the flow of this program will be, and write methods to do the work that you need done. Take a sheet of paper … | |
Re: I'm still not seeing a "getString()" method for TextField, or setString(), either. getText() and setText(), yes, but not getString or setString. This shouldn't be your problem - calling non-existent methods doesn't throw an NPE. To get this exception, your textBox3 must be null. Put in this line before the call … | |
Re: As a matter of coding style, you might start by making your boundaries into constants. In this simple little example, it doesn't look like it saves you anything, but suppose you were writing something where these values were used across multiple class files. You don't want to change three class … | |
Re: Entering in a JTextField? I'd probably try addint a KeyListener. When it fires a keyTyped KeyEvent, get the current text and off you go. | |
Re: So you're using this pattern to split on - doesn't that eliminate anything that matches the pattern? If I split "a+b+c" on "+", I get ["a", "b", "c"] - what happens when you run this on Anyone Someone Something Somebody Science Some other word I would expect ["Anyone","Something","Somebody","Some other word"] … | |
Re: max is a shorthand for "a function returning the maximum of two numbers". You'll find an implementation of such a function in java.Math. | |
Re: [CODE]for (int i = 1; i <= (11 - 2 * line); i++) { System.out.print("*"); [/CODE] This will print "*", ( 11- (2*line) ) times. So if [ICODE]line == 5[/ICODE], it'll print "*", if [ICODE]line == 1[/ICODE] it'll print "*********" So given that your outside loop descends from 5 to … | |
Re: Try this as your regex: "^[S2]" "^" is the start-of-line anchor, and [S2] matches either of those characters, so this should match what you're looking for. (But I could be wrong, regex isn't really my forte) | |
Re: So what's stopping you? (you haven't asked a question here) | |
Re: Forget about the timer for the moment. What does the guess() method look like? That'll depend on whether this is running in a command-line or a GUI situation, but they're pretty much the same at bottom: you want to enter a state in which the user enters words and they … |
The End.