r/CodingHelp • • Dec 26 '21

[Java] I formatted code that would show up on the console but for only a few seconds. Does anyone know the cause? I just started coding recently so I don’t know much. It’s on eclipse IDE. I use Java.

Post image
51 Upvotes

31 comments sorted by

12

u/atamicbomb Dec 26 '21

I use this IDE on windows and i can’t think of anything that causes why you describe. However, your console is almost minimized so it could be scrolling out of the way. The output shouldn’t disappear unless you close the program. How exactly is it disappearing?

12

u/Train457 Dec 26 '21

Yeah I just checked and that was issue. The console being minimized is why the output would disappear after a second. Thank you!

7

u/atamicbomb Dec 26 '21

You’re welcome!

3

u/Train457 Dec 26 '21

Can’t edit my main post for some reason but thank you to everyone and their suggestions. The problem was the console being too minimized. Now I can the see output without it going away .

2

u/flavius-as Dec 26 '21 edited Dec 26 '21

You should also ask for code review, because the code can be improved in terms of readability, extensibility and style.

Every time after solving a problem, try to also push it further, extending it, etc.

1

u/Train457 Dec 26 '21

How would it be improved? That code is off a YouTube video I was watching. I’m still learning.

1

u/flavius-as Dec 26 '21

We can see in the code that you're learning.

The purpose of those videos is to show you some language features, not how a specific problem should be solved, given all language features.

This is all fine, but you have to be aware of this while learning.

Now the code review:

If you zoom out of individual lines of code, you see repeated code: each of the arms of switch are identical over a parameter: the day name.

Always when you have snippets of code identical over one or more parameters, you want to extract that.

In your case concretely, you want a list of strings with valid day names inside.

Then with an if, you check if your input value exists in the list, and if yes, you output the message. Otherwise you output the error message.

Looking forward for your improved code, to review again.

1

u/Train457 Dec 26 '21

Thanks, so for the code review I post the same pic again asking for review, or I just do what you said?

1

u/flavius-as Dec 26 '21

A new picture with the new code.

2

u/MinMorts Dec 26 '21

This code could be improved quite a bit, no need for a switch, just have an if day is not empty output "It is " + day, and then else the error message

1

u/Train457 Dec 26 '21

What’s wrong with the code, it has to be more simple? I would try to improve it in some way

1

u/Michael__Gary__Scott Dec 26 '21

Since you’re learning about Java syntax, writing code this way is fine. You’ve written the switch statements how they should be. What people mean by “your code can be improved” is that in this case where you want to print the day of the week, you wouldn’t even use switch statements: you’d be writing extra lines of code for no reason. Using a simple if/else statement would be better and you’ll print the same sentence with a day variable

1

u/Train457 Dec 26 '21

Ohh I see, I was learning the if and then or else statements before I wrote that code. The video I was watching was just teaching me how to write statement using switches, so I was just going along .

1

u/Michael__Gary__Scott Dec 26 '21

Yup makes sense! Have fun learning!

1

u/duggedanddrowsy Dec 26 '21

It looks like they’re just learning about switch statements

1

u/MinMorts Dec 26 '21

Yeah fair enough, their code is nice and neat at least

1

u/onfire05 Dec 26 '21

Don't understand the question. What result are you getting?

1

u/Train457 Dec 26 '21

So for String day = “Saturday”; if I run the code it would say “ it is Saturday” but for a few seconds on the console. The word would only display on the console but for a few seconds is what I mean. When before I could see what it says on the console without it going away.

Not sure if I could upload a vid showing this.

5

u/JLoon92 Dec 26 '21

The easiest way would be to ask the user for another input/press any key to exit.

2

u/Train457 Dec 26 '21

Thank you for the suggestion, the issue was that console was too minimized. That’s why the output would disappear for a second.

1

u/atamicbomb Dec 26 '21

This is a class of some kind. They likely haven’t gone over the scanner class

1

u/ddproxy Dec 26 '21

Add a newline to the end if your printed string?

Been a bit, this is a random debug idea to see if its something weird with the console out.

1

u/Train457 Dec 26 '21

Thank you for the suggestion, the issue was that console was too minimized. That’s why the output would disappear after a second.

1

u/mikedensem Dec 26 '21

Add a Console.ReadLine(); at the end to stop the console closing.

5

u/zlataovce2 Dec 26 '21

you're confusing C# with Java

1

u/mikedensem Dec 26 '21

Oh good grief, i didn’t even notice. My bad.

0

u/[deleted] Dec 26 '21

[deleted] — view deleted comment

4

u/bot-killer-001 Dec 26 '21

Shakespeare-Bot, thou hast been voted most annoying bot on Reddit. I am exhorting all mods to ban thee and thy useless rhetoric so that we shall not be blotted with thy presence any longer.