This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

why is the code not running in Eclipse when there is a main method?

 
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi experts,

I am running yet into another road block when I have so little time in preparing for coding test which I have failed umpteen times...

So, I am learning the tutorial before I attempt Hackerrank 30 days of code and this is the code that is in the tutorial that I tried to run in Eclipse but nothing shows up ...as in there is no asking of input etc.



Can someone tell me how I can make Eclipse run the above code?

Tks.
 
Saloon Keeper
Posts: 24330
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first, you might want to clean up your indentations. It makes it confusing to read code that isn't consistently indented. Do that by clicking in the source code, then Ctrl-A to select All code in the pane and Ctrl-I to tell it to indent it properly.

A Java project can actually contain more than one main() method, although only one per class. So Eclipse needs to be told which one you want to run or debug. The Eclipse Run menu is the control point for that.

Normally when you select "Run/Run" (Ctrel+F11) for a new project, Eclipse should automatically create the run profile you need. But if there's a problem, you can do it manually.

To do that, select menu Run/Run Configurations... That will bring up the Run/Debug profile editor. In its left-hand pane are different types of run environments and run configurations defined for those environments. For a stand-alone Java app, you want the "Java Application" environment. Click the "New Launch Configuration" button in the upper left (its Icon looks like a sheet of dog-eared paper). That will bring up a dialog on the right-hand side. Fill in the Name with something that names your project so that you can easily find in in the browser pane on the left. Select your Project using the Project sub-dialog Browse button, then select your Main class using the "Main Class" browse button. A multi-main project should show a list here, but if you've only got one main class, then you should see the fully-qualifed class name of your Main class filled in on the dialog. If you don't there's something wrong with your code or project.

After that, it's simple. You can click the "Run" button on the dialog if you like. Thereafter selecting the project and using the Run/Run menu should do it, I think.
 
Marshal
Posts: 74059
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most people don't like // end something comments.
Don't close a Scanner reading System.in; that can cause no end of problems. If you get a warning about Scanners from Eclipse, every Scanner must be closed, except those reading System.in. For that, the correct response to the warning message and yellow triangle is probably the option with @SuppressWarnings("resource").
 
Tim Holloway
Saloon Keeper
Posts: 24330
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That program wasn't designed to ask for input. It simply reads input from the stdin input stream (System.in). It looks like it probably takes a single line of numbers, although I'm not sure about that. Although Campbell tends to get rather tetchy ablout closing Scanners attached to stdin, if a program does nothing from stdin BUT Scan, closing the Scanner (and thus stdin) makes little difference.

It's not a very user-friendly program, in fact, and the problem is made worse because it's not really designed to run in Eclipse, but to run as a stand-alone Java Application.
 
Campbell Ritchie
Marshal
Posts: 74059
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does take input; as you said, the input can be condensed into a single line. But the program doesn't tell its users what input is wanted.
Yes, in this case the Scanner is closed just before the program terminates, so in this instance nobody will notice the problem. So it will come as a nasty surprise when the close operation does make a difference and it comes back to bite you.
 
tangara goh
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

My console is still not giving me any "output" or asking me to input the number...
If it is meant to be a stand-alone Java app, will the code run in Eclipse ?  I have right click on the configuration to include this program but still the console just gives me a blank and "everything" just stalled...and it is not this one...another one - the solution that I submitted in HackerRank which has only 1 test passed cannot be run in Eclipse also...  Please let me know how to make it run in the Eclipse...cos I have a upcoming coding test(which I have a feeling I am not going to passed....)
 
Campbell Ritchie
Marshal
Posts: 74059
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:. . . My console is still not giving me any "output" or asking me to input the number...

As I said on Wednesday, there is no code prompting for any input.

If it is meant to be a stand-alone Java app, will the code run in Eclipse ? . . . the console just gives me a blank and "everything" just stalled... . . .

Because your program requires input and you aren't giving it any; the program is running and will wait until it gets the appropriate input.
 
tangara goh
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought so too but there was someone in a Discord group that teaches me how to do the bubble sorting algorithm told me that it can actually works in Eclipse but after 3 hours of guiding me I have no time to ask his help further on this IDE thing.

Is there a way to make things run in the IDE by using any api that ask for input ? But, I see there is Scanner in already...
 
Master Rancher
Posts: 4465
38
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To allow the program to run without it requiring the user to enter any numbers, preload the numbers by changing the Scanner class's constructor from this:

to this:
 
Bartender
Posts: 4633
182
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or create an int[] directly, to avoid the use of a scanner:

 
Sheriff
Posts: 26793
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:Is there a way to make things run in the IDE by using any api that ask for input ? But, I see there is Scanner in already...



Normally when you run a Java program in Eclipse, there's a tab in the panel at the bottom right-hand part of the screen which is named "Console". That's the console for the program. System.out and System.err will write data there and System.in will read data from there. So that's where you would type your input. That's assuming you're using the Java perspective, of course, but why wouldn't you be doing that?

Edit: I guess I should mention, to type in the Eclipse console you need to click in the blank area with your mouse, then you'll have a cursor where you can start typing.
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic