• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays in java

 
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :

 
Greenhorn
Posts: 14
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic