• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting confused on my Stack ADT  RSS feed

 
Taylor Lynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to have the program read 20 integers from the User.

Here is my current code:



When I run the program it continually ask for the integers. You can just continue to enter as many as you want and it never stops you and reads them back.
There are also two classes associated with this class ill post them here:



Here is the second class:




Thank you for any help.
 
Taylor Lynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you put in the 20 integers it also throws an exception on the command line.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taylor Lynn wrote:When you put in the 20 integers it also throws an exception on the command line.


Copy/paste the exact, complete error message, and indicate clearly which line is causing it.
 
Taylor Lynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you copy and paste from the command line? It's line 20 by the way.
 
Taylor Lynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello? I don't want to seem impatient just wondering if anyone has figured out anything.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taylor Lynn wrote:How do you copy and paste from the command line?


If it's Windows, then either just select it with the mouse and hit enter to copy, although you may have to first right-click in the command window and select "Mark". Then paste it into your post here just like you'd paste anything else.

It's line 20 by the way.


Line numbers in what you post here don't necessarily match line numbers in your real code.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taylor Lynn wrote:Hello? I don't want to seem impatient just wondering if anyone has figured out anything.


When you only wait a half hour between posts, it does seem very impatient. If and when anybody has a response, they'll post it. I'd say it's rude to wait less than a full day before bumping your thread. Keep in mind that everybody here is a volunteer, doing this on our own time for our own reasons.
 
Taylor Lynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enter 20 Integers:


5,13,25,16,2,4,5,17,457,88,1,9,26,55,100,75,36,8,10,35
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at PEX3.main(PEX3.java:20)
Press any key to continue . . .

Ok here is the error. This is due tonight. When I read the assignment it seemed as though we could input the numbers into an array or make it read from the user's input so when I turned in the assignment tonight he gave me a 7/10 because I had it read from the array. He told me that I also had to get the program to read the user's input. On the assignment page it does say that we do one OR the other but now hes telling me that we have to do both. So I'm really sorry if I am impatient. I really appreciate your help and I know it's an inconvenience to you.
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think line 20 in the first code is causing your problem.

Reading in the user input shouldn't be too big of a problem, you could just use the Scanner class.
 
Taylor Lynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought i was using the scanner class when i implemented
Scanner scanner = new Scanner(System.in)
I'm also using the nextInt method from the scanner class on line 20.
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taylor Lynn wrote:I thought i was using the scanner class when i implemented
Scanner scanner = new Scanner(System.in)


You created a scanner object, but you're not actually reading in anything from the user.

Taylor Lynn wrote:I'm also using the nextInt method from the scanner class on line 20.


They way you have it is incorrect. You're setting a value equal to it rather than a variable.

The nextInt() method takes in an int from the user and sets it to a variable.

 
Taylor Lynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up fixing this by adding some semicolons to separate the loops.
Thank you everyone!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!