Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pushing numbers obtained with Scanner#nextInt() onto a stack  RSS feed

 
Nina Bussey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java and I have to do basically the same thing except I have to capture the 20 integers one at a time in a "for" loop using the nextInt () method of my scanner object. I'm stuck there with the nextInt method. I can't figure out how to do that. The code above doesn't do that anywhere from what I can tell using the nextInt method. Is there anyone that can explain to me how to do that? Any guidance you can give will be appreciated.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may help to look at this thread for background.

Welcome to the Ranch
That looked like a new question which deserves a thread of its own.
I think you are making things difficult by trying to do too many things simultaneously. Can you write a loop which runs 20×? Can you get numbers with nextInt()? Can you write a utility class which uses nextInt()? I won’t tell you how to write a utility class, nor will I tell you there is an example of exactly what you want here.

I suggest you get your loop working. Then read your twenty ints. Are you supposed to write your own Stack class, or use the one in provided by Java ready‑made? And, yes, I have linked to the right place. don’t even think of stacks until you have a loop which prints your twenty numbers back to you. then consider adding the stack.
 
Nina Bussey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Story of my academic life...making things too difficult!
Yes, I have an ArrayStack.java and a StackInterface.java file. Those are both coded correctly.
My output is supposed to look like this:

Run:

Enter 20 integers:

5 13 25 16 2 4 5 17 457 88 1 9 26 55 100 75 36 8 10 35

Even integers in reverse order:

10 8 36 100 26 88 4 2 16

Odd integers in reverse order:

35 75 55 9 1 457 17 5 25 13 5


This is supposed to be a test file to test the other two files.
Here is my code so far and it compiles without any errors but when it runs it just sits there with a blank screen like the loop is just running maybe??? That's a guess!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nina Bussey wrote:Story of my academic life...making things too difficult!
Yes, I have an ArrayStack.java and a StackInterface.java file. Those are both coded correctly.


On what basis are you making that claim? Not being snide or anything, just trying to understand where these classes come from and what kind of credentials they have or testing they've undergone.


Here is my code so far and it compiles without any errors but when it runs it just sits there with a blank screen


What do you think this line does?


And on a related note, why is this way down near the end?

 
Nina Bussey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On what basis are you making that claim? Not being snide or anything, just trying to understand where these classes come from and what kind of credentials they have or testing they've undergone.

<smiling because I understand your concern> My professor provided an example for us to go off of and it compiled correctly.

What do you think this line does?


TheIntegers[someNum] = scanner.nextInt();


Ahem! I was kind of hoping that it captured my 20 integers??? Guess that's not right, huh?

And on a related note, why is this way down near the end?


System.out.println("Enter 20 integers:");

Because I always put all my System.out.println's at the end of my files. It's always worked well for me before. Guess not so much now?
See here's the thing. In my Java I class we only used Scanner for keyboard input. Then we're thrown in this God awful class and I don't understand why we are using Scanner when this is just a test file. Nobody is going to be entering anything!!! I am providing the doggone integers in the file. I don't get it. I feel like we skipped 6 chapters or something.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nina Bussey wrote:
On what basis are you making that claim? Not being snide or anything, just trying to understand where these classes come from and what kind of credentials they have or testing they've undergone.

<smiling because I understand your concern> My professor provided an example for us to go off of and it compiled correctly.


Those are two points in its favor, but not a guarantee. Something compiling correctly says nothing at all about whether it will run correctly. It can compile correctly and still be totally completely wrong. But for now it's probably safe to assume that these classes are fine.

What do you think this line does?


TheIntegers[someNum] = scanner.nextInt();


Ahem! I was kind of hoping that it captured my 20 integers??? Guess that's not right, huh?


Well, it will capture one integer, and in a 20x loop, yes, it will capture 20, but that's only part of the story. Do you know what has to happen for it to "capture" those integers?


And on a related note, why is this way down near the end?


System.out.println("Enter 20 integers:");

Because I always put all my System.out.println's at the end of my files. It's always worked well for me before. Guess not so much now?


The code inside a method is executed in the order in which it appears in the method. It's fine to put the printouts at the end of the method if you don't want them to appear until after everything else is done, and for the final output of your program, that's generally appropriate. For output that prompts the user for input, such as "Enter 20 integers", it would make more sense to put it right before the code that actually reads in those integers, so that the order of execution is 1) ask the user for input, 2) take that input.

See here's the thing. In my Java I class we only used Scanner for keyboard input. Then we're thrown in this God awful class and I don't understand why we are using Scanner when this is just a test file. Nobody is going to be entering anything!!!


But your code says otherwise. It says, "stop here until the user enters something." You told it to stop there. It has no way of knowing that nobody's going to enter anything.
 
Nina Bussey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it will capture one integer, and in a 20x loop, yes, it will capture 20, but that's only part of the story. Do you know what has to happen for it to "capture" those integers?

Unfortunately not. My professor uses that term "capture" in his assignment sheet but the book doesn't use it at all to explain what to do.

The code inside a method is executed in the order in which it appears in the method. It's fine to put the printouts at the end of the method if you don't want them to appear until after everything else is done, and for the final output of your program, that's generally appropriate. For output that prompts the user for input, such as "Enter 20 integers", it would make more sense to put it right before the code that actually reads in those integers, so that the order of execution is 1) ask the user for input, 2) take that input.


That makes sense. nextInt takes that input, correct? Again though, I'm stuck with this whole thing about there being NO input.

But your code says otherwise. It says, "stop here until the user enters something." You told it to stop there. It has no way of knowing that nobody's going to enter anything.


How do I tell it that? I don't know what term or section to look up in my book that can explain that to me. This chapter just tells us about a array stack. I read all about that and I get the concept. push, pop, top....last in, first out. It doesn't tell us how to build a test file. I'm telling you we went from "Hello World. My name is Nina." in Java I in which I got an "A" to this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scanner.nextInt() says "read the next int from the System.in stream" (which in this case means the keyboard. The execution won't get beyond that point until there is some input for it to read--in other words, until you type in an integer (and hit Return).

If you're not going to be reading input from the user typing on the keyboard, don't use a Scanner connected to System.in. And if you're not going to have your user enter the numbers, why are you printing out, "Enter 20 numbers"?

 
Nina Bussey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I asked my professor and he just told me that if I thought it was more interesting to use the keyboard to enter the integers then by all means do that. I don't think it's any more interesting. I just wanted to do the assignment the way he had asked us to but if he doesn't care then I'll just do it the way I know how to. I got it now. Thanks for your help.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether it is interesting or not, if you have been told to use a Scanner, that means from the keyboard. It does nobody any good to change an assessment halfway through.
It is easy to capture individual numbers from the keyboard with a Scanner. I have already given you a link. There is one pitfall with Scanner which is not well documented, however. It’s described here, but you are probably not using nextLine(), so you won’t notice it.
Then it would be easy to capture those numbers twenty times. You should have no difficulty writing a program which prints this sort of thing once:-
Next number please: 123
You entered 123
Now you can change that to take twenty numbers. Now you can put those twenty numbers onto a stack. If you break the problem into small pieces like that, you will find it much easier. If you try to keep the whole thing in your head and try to do it all in one stage, you are liable to get confused.
 
Nina Bussey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I agree that it does no one any good to change the assignment half way through. I just wanted to do it the way he asked us to. I wanted to LEARN which is why I like this site. You don't just give the code. You explain what the code means. The assignment is over now. After sending him (my professor) about 12 emails simply wanting to know if I am supposed to have the user literally enter 20 integers and never getting a straight yes or no answer, I just got frustrated and just sent it in as it was. I don't know what the problem is...if it is a language barrier or not. He's from overseas. Nice guy and all, just can't seem to communicate with him effectively. I work full-time, have a family, and take other classes. I just couldn't spend any more time on it. I'm sure I will be back asking for more help on another assignment!
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
And sorry to hear about those other problems.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!