Win a copy of Java Challengers this week in the Java in General 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Linked List,i have to press 1 to get the main menu to work.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ive deleted some codes so it would fit,i checked every scanner..

 
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd start by removing all but one Scanner object that reads from System.in.  There should only be one Scanner for System.in
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:I'd start by removing all but one Scanner object that reads from System.in.  There should only be one Scanner for System.in


i did tht as one scanner for every code but that didnt work too.
 
Norm Radder
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

that didnt work too.


Can you post the new code?
A problem with Scanner class's methods is mixing use of the next...  methods with the nextLine method.
This discusses the problem: https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/
 
Saloon Keeper
Posts: 8093
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you trying to do with lines 5 & 6? Line 5 gets keyboard input but you are not prompting for any input. Line 6 creates a new Node that appears for the only purpose of having some thing for head to point to. Your code should be able to deal with an empty list without having to create a dummy node.

As Norm states only ONE Scanner for System.in for your **ENTIRE** program, however many classes that is. In general, pick your primary class to put it in or make a class specifically for that purpose. Example:
Also, never ever call KEYBOARD.close(). That will permanently close the input stream and it can't be re-opened until the program is re-run.
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

that didnt work too.


Can you post the new code?
A problem with Scanner class's methods is mixing use of the next...  methods with the nextLine method.
This discusses the problem: https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/



i used InputForNode at the top of my code for every scanner. it wont fit in if i post the new code :c
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:What are you trying to do with lines 5 & 6? Line 5 gets keyboard input but you are not prompting for any input. Line 6 creates a new Node that appears for the only purpose of having some thing for head to point to. Your code should be able to deal with an empty list without having to create a dummy node.

As Norm states only ONE Scanner for System.in for your **ENTIRE** program, however many classes that is. In general, pick your primary class to put it in or make a class specifically for that purpose. Example:
Also, never ever call KEYBOARD.close(). That will permanently close the input stream and it can't be re-opened until the program is re-run.



that is what i did as norm stated,i made one scanner for all methods.
 
Carey Brown
Saloon Keeper
Posts: 8093
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bishara Babish wrote:

Norm Radder wrote:

that didnt work too.


Can you post the new code?
A problem with Scanner class's methods is mixing use of the next...  methods with the nextLine method.
This discusses the problem: https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/



i used InputForNode at the top of my code for every scanner. it wont fit in if i post the new code :c


import java.util.Scanner;


 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Re-post your modified code.


do you mean a new topic?
 
Norm Radder
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when line 3 is executed?  What does the user do?
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:What happens when line 3 is executed?  What does the user do?


its for     static Node head = new Node(r);
 
Norm Radder
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but what happens when line 3 is executed?  What does the user have to do?

Can you copy the contents of the command prompt window and paste it here so we can see what happens when the code is executed?
Add some comments to the output line(s) where the operation of the program is not what was expected.
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Yes, but what happens when line 3 is executed?  What does the user have to do?

Can you copy the contents of the command prompt window and paste it here so we can see what happens when the code is executed?
Add some comments to the output line(s) where the operation of the program is not what was expected.



I don’t actually get it, the scanner is just for all methods and stuff,line 3 is “static Node head = new head(r) which takes the scanner. It’s for the head,if you are intending to delete the line the heads in every method won’t work because it depends on it.
 
Norm Radder
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You missed this part of my post:


Can you copy the contents of the command prompt window and paste it here so we can see what happens when the code is executed?
Add some comments to the output line(s) where the operation of the program is not what was expected.


That will help us see what the problem is.

,line 3 is “static Node head = new head(r)


You must have a different version of the code.  What I see at line 3:
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:You missed this part of my post:


Can you copy the contents of the command prompt window and paste it here so we can see what happens when the code is executed?
Add some comments to the output line(s) where the operation of the program is not what was expected.


That will help us see what the problem is.

,line 3 is “static Node head = new head(r)


You must have a different version of the code.  What I see at line 3:



Oh my bad sir,that’s the only scanner that has been declared for every method.
 
Norm Radder
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You missed this part of my post again:


Can you copy the contents of the command prompt window and paste it here so we can see what happens when the code is executed?
Add some comments to the output line(s) where the operation of the program is not what was expected.


That will help us see what the problem is.
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:You missed this part of my post again:


Can you copy the contents of the command prompt window and paste it here so we can see what happens when the code is executed?
Add some comments to the output line(s) where the operation of the program is not what was expected.


That will help us see what the problem is.


i dont follow,im sorry about this,see this pic if im right or not or give me an example sorry again for wasting your time on me.
this.png
[Thumbnail for this.png]
 
Norm Radder
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that is a start.  But you left off the description of what was wrong with the way the program executed.
Now can you copy and paste it here as text (not an image)   Add comments to it  where the output is not what was desired or expected.
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Ok, that is a start.  But you left off the description of what was wrong with the way the program executed.
Now can you copy and paste it here as text (not an image)   Add comments to it  where the output is not what was desired or expected.




there.
 
Norm Radder
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Review all the past posts where both Carey and I asked about the code on line 3.
What does the statement on line 3 require the user to do?
Is that related to what your said: // not expected

What happens if you enter 999 instead of 1?
 
Carey Brown
Saloon Keeper
Posts: 8093
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InsertE() does not do anything with newTail.
 
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP,

you do not have to type a 1, before going on to the main menu. Any number will do. See line 3:

Your code is waiting here for the user to type in some number, However, there is no indication that a user has to type in anything, so the code seems to be stuck here.
It has been asked repeatedly: what good line 3 is for? Anyway, give a prompt here, so that it is clear that something has to be typed.

Another question: why is 'head' a static variable?
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:@OP,

you do not have to type a 1, before going on to the main menu. Any number will do. See line 3:

Your code is waiting here for the user to type in some number, However, there is no indication that a user has to type in anything, so the code seems to be stuck here.
It has been asked repeatedly: what good line 3 is for? Anyway, give a prompt here, so that it is clear that something has to be typed.

Another question: why is 'head' a static variable?


I listened to me IDE and changed it to static since all my things are static,and yes something has to be typed.
I mentioned before it’s for the Nodes.
 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bishara Babish wrote:. . . I listened to me IDE . . . .

You have to learn which are the 99% of occasions when the IDE's suggestions are good and the few occasions where the IDE's suggestions simply add errors. The correct solution to code not compiling because it is in a static context is usually to make more code nonstatic.
 
Norm Radder
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yes something has to be typed.
I mentioned before it’s for the Nodes.


Is your problem solved now since you know that something has to be typed?

Why can't the value be hardcoded in the constructor?  Why does the user have to enter a number?
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

yes something has to be typed.
I mentioned before it’s for the Nodes.


Is your problem solved now since you know that something has to be typed?

Why can't the value be hardcoded in the constructor?  Why does the user have to enter a number?


Bro that’s what I’m trying to fix all this time 😭😭
I don’t the user to type anything or any number to let the main work,I just need the main menu to show up.
 
Carey Brown
Saloon Keeper
Posts: 8093
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As  I said, your code should be able to tolerate an Empty list, that is a list with NO Nodes and a head reference that is null. Of course your various methods would need to handle that as well.

An alternative approaches is to have head BE a Node which we don't care what the values is. Again, your methods would need to understand that condition.

In either case, no value should need to be entered.
 
Piet Souris
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As your class is supposed to represent a SinglyLinkedList, you should allow for many instances, each instance with its own head. So, head should be non-static, or else you can have only ONE SLL.

Now, all your methods are static, meaning that if the head is non-static, you should indicate on which SLL the method should operate (for instance by making head a parameter of these methods).

Therefore, as Campbell wrote: make these methods non-static as well, so they will all have access to the relevant head.

And you should have at least one constructor, for instance:

or
 
Marshal
Posts: 26531
81
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

Bishara Babish wrote:Bro that’s what I’m trying to fix all this time 😭😭
I don’t the user to type anything or any number to let the main work,I just need the main menu to show up.



If that's a problem, then you really ought to put that code aside and work on some simpler code which just gets the menu and the related user input working. All that business about nodes is just distracting, you can put it back in to the working main-menu code when that's ready to go.
 
Bishara Babish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Bishara Babish wrote:Bro that’s what I’m trying to fix all this time 😭😭
I don’t the user to type anything or any number to let the main work,I just need the main menu to show up.



If that's a problem, then you really ought to put that code aside and work on some simpler code which just gets the menu and the related user input working. All that business about nodes is just distracting, you can put it back in to the working main-menu code when that's ready to go.


I already fixed it by myself,thanks anyways,idk how to close this topic.
 
Paul Clapham
Marshal
Posts: 26531
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bishara Babish wrote:I already fixed it by myself,thanks anyways,idk how to close this topic.



Excellent! I marked the thread as "resolved".
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic