This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!

Bill Platt

Ranch Hand
+ Follow
since Mar 23, 2019
Old dude who returned to college last year. I recently took an intro to Python class, now taking an intro to Java
Bossier City, LA
Cows and Likes
Cows
Total received
1
In last 30 days
1
Total given
0
Likes
Total received
1
Received in last 30 days
1
Total given
0
Given in last 30 days
0
Forums and Threads

Recent posts by Bill Platt

I'm trying to replace a set of asterisks from a line in an array that comes from a read text file. I have tried using replaceAll() but it's not working.

How would I go about this?

What is posted below is for example and illustrative purposes only.

The line that has been read from the text file says:


"******Warning! Problem detected!******"



What I want to do is remove the asterisks so that it reads as:

Warning! Problem detected!




If I use the following code, I can get it to print as original:




I have tried using replace.All() but it did absolutely nothing.



What I think the above statement is saying is to replace one or more of the occurrence of "*" with white space, the escape character being needed as the asterisks is a special character in regex.

my output shows no change, and I am not receiving an error message, the replaceAll() appears to be just be ignored.

1 week ago

Campbell Ritchie wrote:I have heard that one before. We often see people posting poor style code who say they will sort out the style when they have finished coding, but that isn't how it works. You sort out the style and the design as you go. Otherwise you will probably leave the poor quality code uncorrected and lose marks for it.



This may be well and good for someone who is experienced and knows where the pitfalls and tripwires may be located. I'm not there yet; I'm still at the stage of trying to get my code to work, then I can go back and make it pretty and/ or clean it up. I wouldn't even classify myself as a beginner yet, I'm still a very basic student who is only 6-7 weeks into my first Java class. I have taken a similar class that dealt with Python, but that is like comparing apples to oranges, true they're both fruits but are significantly different. I am learning, but it's a slow process.



Campbell Ritchie wrote:
Start with names, which you can read about here. That link might be old, but it is still relevant. A class name like MonitorAnimal tells me that you are looking for monitor lizards, not that you want to do any monitoring of animals. You would want to call the class AnimalMonitorer (only that is unpronounceable); monitorAnimal() might however be good name for a method. The act that you used those names in your code shows how much better they are as names for methods.
LInes 12‑13 show up a problem. You are not calling any methods there, but only creating objects. Yes, you need objects, but making them is only a part of the whole process. You need to know the difference between creating objects and calling methods on them. You would want something like animalWatcher.observeAnimal(a); where a is a variable of type Animal. So you need to decide what such a method is going to do, and then write the method to do that.
Lines 33 and 40 show you writing something you know will cause your code to ail to compile, so what's the point of that?



Again, I can see your point, though I don't fully agree. I felt that the choice of monitorAnimal was both simple and descriptive. I would not be surprised if my choices for method and class names change as I gain more experience. Once again, I'm not there yet.

As for lines 11-12 and 33 and 40.

I inserted them for the purpose of this post for clarity; my intent was to show where I suspected the errors may be. It is not something that I planned to include in my program.

Also, may I draw your attention to the final line in my original post?

" I realize that there are probably better ways to go about this, but I'm trying to just make it work I can work on more advanced techniques as I gain more knowledge."



I do not have years or even months of experience. I have not written multiple programs, nor do I fully understand most of the available techniques associated with writing code in any programming language. I am trying to learn how to do things correctly, and I understand that there are certain best practice techniques, but one must also consider personal style; what works for one may not be acceptable to another, etc. With experience comes the wisdom to do things better, quicker, and with less effort. Someone recently told me that it takes at least several years of intense study to master the art of programming; I believe it, as every time I attempt to do something I seem to be being exposed to something new.
1 week ago
Wow! I wasn't trying to stir something up, I was just looking for clarification.

Thanks to all who have responded. I suspect with many aspects of learning programming, it'll take a while to sink in. It also seems as though there are differences of opinions.


Tim Holloway wrote:I didn't notice, maybe someone already said it, but "this" isn't an implicit parameter. It's an implicit member variable. Or, if you prefer, it's defined as a private member of the ultimate base class, java.lang.Object, albeit one that is not formally documented. And constructed referencing the class instance it's defined in.



My choice of using "implicit parameter is based on what my textbook was calling it (see screenshot below). I am willing to admit that the textbook may not be 100% correct in the terminology that it uses, but I tend to go with what I have been given and expand from there.

thanks again everyone

Bill
1 week ago

Angus Ferguson wrote:Please have a look at this piece of code.



You were trying to call the methods by using the instance of your classes. The instance of the class MonitorAnimal, for example, allows you to call the methods written in that class by adding a dot at the end of the instance. So, in this case, we are supposing that  .method1() has been written within the  MonitorAnimal class.




Thank you Angus!

That did the trick. I got the program working, now I can concentrate on cleaning it up and perhaps looking into the run()

thanks again

Bill
1 week ago

Angus Ferguson wrote:Instead of adding logic to the main, it is recommendable to write a run method. The code becomes much more robust and clean. You can have a look at this Main - Pain article.

https://coderanch.com/wiki/660020/Main-Pain  



I can see your point, and I may go that route once I get everything working.

It doesn't, however, address my original point. What am I missing to successfully call my methods from my other two classes to make everything work?

If if I were to use a run() method, I'd still be in the same boat.

Bill
1 week ago
I apologize for improper terminology, as I'm struggling with getting the "lingo" down.

I have also read more threads than I can count here, as well as searched the net for a solution.

Here is what I have going on. I have two classes (classes are called MonitorAnimals which contains the method monitorAnimals() and MonitorHabitats contains the method monitorHabitats()) that perform a read file (using BufferedReader and FileReader), then I am outputting the data from an array based on user input (using Scanner).

I am attempting to call the data from the two classes back into main(). This, in turn, will be using a switch for user input to go in a particular direction. I can post the code for the two other classes if need be,
but I think they're OK, My "primary" class that includes main() is listed below.



I think once I can get the other methods from the other classes, I should be OK. I realize that there are probably better ways to go about this, but I'm trying to just make it work
I can work on more advanced techniques as I gain more knowledge.

thanks again

Bill
1 week ago

Fred Kleinschmidt wrote:In your first example, you calculate avgVal on line 21 BEFORE you calculate the sumVal. That is, sumVal is still zero.
In the second example, you calculate the average AFTER you have summed up all of the values.



OK, thanks. I see that now.

One of these days I will get where I am thinking in the proper circles.

It didn't even occur to me that the second loop was doing the calculations for sumVal.

That probably should have been obvious.


Bill
1 week ago
I'm taking an intro to Java course (some may have seen previous questions posted).


The code below is for an interactive example from my text. I didn't write the majority of the code, the scenario is:

Modify the program to print the average (mean) as well as the sum. Hint: You don't actually have to change the loop, but rather change what you print.



This is the initial modification that I attempted. It did not provide the proper output.
I added line 21, as well as line 26



Output for the above code was:

Enter 8 integer values...
Value:
Value:
Value:
Value:
Value:
Value:
Value:
Value:
Sum: 920
Average: 0



based on input of:
3 5 234 346 234 73 26 -1.

If I remove the variable initialization at line 21, and then I change line 26 to do the same thing that I was trying to do at line 21,
everything works out fine. I achieve proper output with the Average computed properly at 115.

Code that worked:




What I don't understand is why the second way worked, but the first didn't. Was I not essentially doing the same thing, only with a variable?

Thanks
1 week ago
Can someone clarify what this is used for?

Here is my understanding (flawed as it may be).

If there is a field variable with the same name as a member variable used within a method, using this can distinguish between the two,
and as a result, things are less confusing.

Am I understanding this correctly?

Is there a benefit to using this as opposed to just declaring a different variable name?

Thanks, and I apologize if this has been covered, I didn't see this specific question asked when I attempted a search.

Bill
2 weeks ago

Junilu Lacar wrote:

Bill Platt wrote:My thinking with call it "multiple parameters" was that when the method was defined, char horizChar & char vertChar were both required parameters.
Am I still misunderstanding this?


No, you're not misunderstanding it because that actually is a method that is defined with multiple parameters. However, your subject and the fact that the printTicTacToe() method is called with two parameters has absolutely nothing to do with the question/problem you had, so it's a poor choice of words. A better subject might have been "Why am I getting a number when I print out characters?" or even "Why does this work if I add "" in front?"



Noted, I will try to keep future questions along the lines of what's actually happening, as opposed to what I think it's called.

Bill
2 weeks ago

Carey Brown wrote:I'm a stickler for asthetics. I prefer this spaced output.

Output:



This was an exercise from my textbook. It was all about being able to call methods that included two parameters.
As with most things, there is more than one way to get the job done (some are better than others).

BIll
2 weeks ago
OK, I think I got it now. I'm didn't realize that the "Char" was an integer value. I was thinking that it was its own deal but as a sort of "mini-String" (another poor choice I works I know but it's how I thought of it)



Junilu Lacar wrote:By the way, the subject you used for this thread indicates a little bit of misunderstanding on your part at worst and poor choice of words at best.

In the System.out.println() statement in question, you are only passing in one argument/parameter. You've used a multi-term expression which gets evaluated down to a single value which is then passed to the println() method.



My choice of words was based on the screenshot below from my text.

My thinking with call it "multiple parameters" was that when the method was defined, char horizChar & char vertChar were both required parameters.
Am I still misunderstanding this?


Thanks for the clarification on the return versus Display. I wasn't really considering that there is a difference. I can see where there is (now) though.

My thought process was that if something was displayed and/or output (somewhere) then it was being "returned"

What does it mean then, when something is being returned? Does this simply mean that the current method is complete, and it's going back to where it was/ moving along to the next one?

I apologize for so many silly questions, but it seems that my understanding is not what I thought it was.


Bill
2 weeks ago
Good morning folks.

This sounds like a very basic question, but it's puzzling me.

Why is starting a print statement argument with "" + critical for the proper output?

As an example, the below code returns a numerical value for horizChar:



The return was:

x!x!x
630

x!x!x
630

x!x!x
630

Yet if the code is modified slightly, it returns something completely different.



The return was:

x!x!x
~~~~~
x!x!x
~~~~~
x!x!x


Is this just something that is the nature of the beast with Java?

Thanks
Bill
2 weeks ago

Junilu Lacar wrote:Mostly on  the right track, I think. The trick is to write some code and see it work first. Start with the menu and at least get the flow right.



Thanks Junilu

Once again you're a HUGE help
3 weeks ago

Junilu Lacar wrote:Since you're already familiar with how to use a Scanner, why don't you just use that? See the JavaDocs for Scanner, you can create one that reads from a file. https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html



Thanks, sometimes the simplest answer is, in fact, the right answer.

As usual, I'm over thinking it.
3 weeks ago