This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Why the error?

 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so on my code its giving an error and im not sure whats wrong with it. The error is showing on line 28.
 
Miroslav Lehotsky
Ranch Foreman
Posts: 82
1
VI Editor Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you check if there is method print() in PrintStream, without accepting argument, and also check return type of your method you're passing there (strPattern).
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The strPattern() method is declared as void. The System.out.print() method takes a String as a parameter, it won't accept void.  Why would you even use System.out.print() on line 28? The strPattern() method already prints out something.
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the main method was prewritten and im supposed to create a working method strPattern
 
Miroslav Lehotsky
Ranch Foreman
Posts: 82
1
VI Editor Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:the main method was prewritten and im supposed to create a working method strPattern



Then focus on how can you change strPattern() method, to make line 28 compile.
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats what I've been trying to do.
 
Miroslav Lehotsky
Ranch Foreman
Posts: 82
1
VI Editor Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:Thats what I've been trying to do.



As already mentioned, print() method of PrintStream used on line 28 has to accept some argument/value. You're passing there return value of your method strPattern(), which is none, sine return type of method strPattern() is void. You need to change the type and return some value from that method.
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I'll try that, thanks.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Miroslav Lehotsky wrote:You need to change the type and return some value from that method.


I wouldn't say you need to do that though. If anything, you should forget about System.out.print() on line 28 and just call strPattern() method because it's already displaying something.
 
Miroslav Lehotsky
Ranch Foreman
Posts: 82
1
VI Editor Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I wouldn't say you need to do that though. If anything, you should forget about System.out.print() on line 28 and just call strPattern() method because it's already displaying something.



I recommended that mostly because Hannah mentioned, that main() method was given and she is supposed to provide implementation of strPattern() method. But sure, it's also an option to print in strPattern() and return void as it is, if possible in her situation to change code in main() method.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. If main() is already as it should, then you're right, she does need to change strPattern so it returns a value that will be accepted by System.out.print().
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I figured that out but now im having trouble with the spacing.


I've tried all the ways I can move things around but still not matching with the solution. Do someone know how I should fix it?
I attached a picture of the comparison.
( the highlight just represents an extra space)
Screen-Shot-2020-03-23-at-3.27.46-PM.png
[Thumbnail for Screen-Shot-2020-03-23-at-3.27.46-PM.png]
 
Campbell Ritchie
Marshal
Posts: 68109
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't print the individual numbers. Create a StringBuilder object and append the numbers and the spaces to it. End each line by appending what you get from this method or similar.
Campbell's Naughty Cheat: Truncate the length of the StringBuilder by the length of the line end sequence.
Then print out the StringBuilder object. You may get slightly faster execution because printing to screen is much slower than other computations; reduce the printing to one call.

Simpler solution. Don't write System.out.println(""); in line 18. Write System.out.println(); Don't write System.out.println(""); in line 20. Write nothing.
Why does your method return a String? Why doesn't it have void instead of a return type?
 
Knute Snortum
Marshal
Posts: 6868
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you format your code correctly you can see that you have an extra set of braces in your outer for loop.  Learn HowToFormatCode (that's a link).
 
Knute Snortum
Marshal
Posts: 6868
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:I've tried all the ways I can move things around but still not matching with the solution.


This sounds like you're guessing.  Don't do that; it's not going to give you the correct answer in most cases.  Instead, think about the problem and how to solve it.  Even write it done.  Some experimentation to figure out how certain methods work is fine, but for the most part, just use logic.

Start with answering Campbell's questions, even just to yourself.  That will get you going in the right direction.
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . .



Sorry I don't understand what you are recommending me do. And isn't a string what the others suggested for me to change it to tho?
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also when I try to make the print statement empty it gives me an error
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The current solution (almost) produces the expected output. Closer inspection, however, reveals that the implementation is a mish-mash that could use some serious cleanup.

The reason the strPattern() method was made to return a String and not void is that the code in the main() method was provided. That implies that it was not meant to be changed. The "mish-mashy" part of the solution is that the implementation returns a string just to satisfy the needs of the System.out.println() call in main(). But rather than build up the String value returned, the method just returns a " " and then prints the rest of the output itself.

A proper solution is that the strPattern() method creates the String that's supposed to be printed out and returns it. There should be NO System.out.print() or System.out.println() statements in the strPattern() method at all.
 
Campbell Ritchie
Marshal
Posts: 68109
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:. . . isn't a string what the others suggested for me to change it to tho?

Didn't notice that, sorry. It isn't normal for a method to print something also to return it. What does the requirement for the exercise say?
 
Dave Tolls
Rancher
Posts: 4493
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the OP earlier (their second post):

the main method was prewritten and im supposed to create a working method strPattern



So the method is supposed to return a String which is then printed out.
I would guess it's not supposed to be printing anything itself, just building the String.
 
Campbell Ritchie
Marshal
Posts: 68109
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case, I think I shall go back to my suggestion about a StringBuilder.You could use a StringJoiner with space as the middle text for each line and a further StrngJoiner with line end as the middles text to join all the lines into one. You don't need any start or end text.
 
Campbell Ritchie
Marshal
Posts: 68109
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you do that with nested Streams? Yes of course you ccan.

java TriangleDemo.java 2 5
2
2 3
2 3 4
2 3 4 5
[critchie@computer

That obviously needs a bit of tweaking.
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:In which case, I think I shall go back to my suggestion about a StringBuilder.You could use a StringJoiner with space as the middle text for each line and a further StrngJoiner with line end as the middles text to join all the lines into one. You don't need any start or end text.




I don't think im supposed to do that because its supposed to all be in that one method I was writing
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also I don't know what a nested stream is
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:There should be NO System.out.print() or System.out.println() statements in the strPattern() method at all.



I think there is supposed to be because all the examples the professor gave me to help figure this code out used them.
 
Campbell Ritchie
Marshal
Posts: 68109
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One Stream inside another. I tried it yesterday and it worked, but it isn't pretty.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:I think there is supposed to be because all the examples the professor gave me to help figure this code out used them.


I honestly don't know how to convince you otherwise because I have solved this in two minutes with a couple of tweaks to your original code. The solution that works does NOT contain any System.out.println() or System.out.print() statements. It's been said multiple times in this thread already, you need to build up a String using either a StringBuilder or plain old String concatenation, then return that value so that it can be printed out by the System.out.print() statement in the main() method. It's really as simple as that.
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get how I would do it without it. Im not understand how the StringBuilder would work
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so is this what y'all meant?

 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks about right. Does it pass the test?
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:That looks about right. Does it pass the test?



Yes it works. Someone helped me with it and said it was that but I don't understand the logic behind it. Do you think you could try and explain how its working ?
 
Campbell Ritchie
Marshal
Posts: 68109
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to go through that code with your pencil and write down at each stage what the values of i, j, and a are. You will remember it a lot better if you work it our for yourself.
 
Mark Ii
Ranch Hand
Posts: 81
Eclipse IDE Debian Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this instead. I saw your effort in figuring out the code. So thought why not just help you. I am more into helping a person in need. Extremely sorry for my attitude. That is why I am posting the code. Let me know if you wanted this code this way. Or otherwise, you could use the StringBuilder class from Java built-in library and print the string like the other kind and generous forum member showed in the posting.




Input:



Output:

 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Ii wrote:. . .



Thank you, This code almost worked as well. The last two in my book fails though I'm not sure why. Thanks for your help though I appreciate it.
 
Mark Ii
Ranch Hand
Posts: 81
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:

Mark Ii wrote:. . .



Thank you, This code almost worked as well. The last two in my book fails though I'm not sure why. Thanks for your help though I appreciate it.




You are welcome. You said my code almost works. Can you kindly please give situations where it does not work so that I can write you a better version of the code?
 
Campbell Ritchie
Marshal
Posts: 68109
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is line 8 different from what others have written?
Don't write System.out.println(""); Write System.out.println(); instead.
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Ii wrote:

Hanna Roberts wrote:

Mark Ii wrote:. . .






You are welcome. You said my code almost works. Can you kindly please give situations where it does not work so that I can write you a better version of the code?



so the failed tests are shown in the picture I attached, I guess it has to do with the use of void instead of strings in your code?
Screen-Shot-2020-03-26-at-6.28.53-PM.png
[Thumbnail for Screen-Shot-2020-03-26-at-6.28.53-PM.png]
 
Hanna Roberts
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why is line 8 different from what others have written?
Don't write System.out.println(""); Write System.out.println(); instead.



As I wrote before, it gave an error when I ran it when I changed it to that
 
Campbell Ritchie
Marshal
Posts: 68109
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error? System.out.println(); is correct code. Please show us exactly what you wrote.
 
I can't take it! You are too smart for me! Here is the tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!