• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read Integer and then String from java  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am solving a problem in which first my program will ask a number N and then N numbers form the user

suppose:
5
4 3 4 5 6

another
6
3 2 7 8 9 3

and I am using this code


but its not working as i want

Where is fault?

Happy Coding
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faults are pretty much at every line (while we don't know what are you trying to do).

At this stage the best solution would be to stop writing the code, instead, get the piece of paper and pencil.
Write down in general steps, what do you want to do, step by step.
Later, write down in more specific steps, what each step should do.

Don't use any computing terms, just simple language. Usually it helps to solve many of problems.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you program reads in an int then a string then you put the inputted string into an array.

So after all that, what do you need to do? What does maxIndexProduct() method do?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did something like this it works for me



@K.Tsang:read my problem statement i have written all.
about maxIndexProduct() its a class method for some calculations
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't close a Scanner on System.in.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Darryl:Why not closing gives me an resource leak exception
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably using Eclipse. That IDE warns you to close Scanner(System.in) but if you do, you'll never get it open again. Eclipse is smart enough to know you should close a Scanner but not smart enough to know that System.in shouldn't be closed.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing: there is a serious potential error if you use nextLine() after nextAnythingElse(). Look here for more details.

By the way: what does your book say about Scanner#nextLine?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell:

I read your post great explanation.But why we need to call s.getNext() immediately after s.getInt().?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because after the next number, you push the enter key. Then the nextLine method will read an empty String.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats exactlty what I was getting

" "EmptyString exception.

Thanks for pointing in right direction


 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what does it say in your book about nextLine?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!