• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays in java  RSS feed

 
Vihanga Raj
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I want to input and store some values in arrays but when I compile and execute it, the command line shows :


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is my code :

 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means while you are running your program, java is not able to find class file in classpath. Make sure you are properly setting the classpath while running like :

java -cp [compiler outpur dir] <Class Name>
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vihanga Raj, please show us which directory that class is in, and also the instructions you are using to compile and run the file.
 
Vihanga Raj
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, I found the solution to my problem

But I have another problem which comes when I run the code. I want to inout the values t the array when the code comes but this is the output that comes:





This is my modified program:

 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Check properly while inserting data, i think you missed to enter data for 'Enter Name 2' and directly trying to enter for 'Weight'
 
Vihanga Raj
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jain Pratik wrote:Hi,

Check properly while inserting data, i think you missed to enter data for 'Enter Name 2' and directly trying to enter for 'Weight'


I did enter it correctly but it didn't wait till I input the name to ask for weight.
 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Issue is with scanner.nextInt(); , if you want scanner to wait for the line always used scanner.nextLine().

so replace line 12 with
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call scanner.nextInt(), it doesn't remove the line break from the Scanner. That's why you aren't asked to fill in anything for "Enter name 2" and the "Enter weight 2" prompt immediately shows. The line break you entered for the 1st weight is what is returned by the call to nextLine() on line 10. If you put a scanner.nextLine(); after line 12 you will consume that line break. You'd have to check what else it consumes though. If I enter "15 abc", the "abc" part is ignored now.
 
Vihanga Raj
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jain Pratik wrote:Issue is with scanner.nextInt(); , if you want scanner to wait for the line always used scanner.nextLine().

so replace line 12 with


Rob Spoor wrote:When you call scanner.nextInt(), it doesn't remove the line break from the Scanner. That's why you aren't asked to fill in anything for "Enter name 2" and the "Enter weight 2" prompt immediately shows. The line break you entered for the 1st weight is what is returned by the call to nextLine() on line 10. If you put a scanner.nextLine(); after line 12 you will consume that line break. You'd have to check what else it consumes though. If I enter "15 abc", the "abc" part is ignored now.



Thanks a lot guys, I got the whole program correct. It works perfectly now.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!