473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help with String array program

mfshake
16
I am try to make the user enter 10 names and then display them out in
reverse order. I am having trouble with my for statement. can someone help me. Thanks


Expand|Select|Wrap|Line Numbers
  1.                  Scanner reader = new Scanner (System.in);
  2.         String [] names = new String [10];
  3.         int count = 0;
  4.         while(count < names.length){
  5.             System.out.print("Enter your name: ");
  6.             String name = reader.nextLine();
  7.             names[count] = name;
  8.             count++;
  9.         }
  10.         System.out.print("\nHere are the names:");
  11.         for(int i = 0; i < count; i++){
  12.             System.out.print("\n" + names[i] + "\n");        
  13.                }
Jan 29 '08 #1
9 2359
the problem is with your for loop..instead start from 0, start from 9. the string is stored in array.
Jan 29 '08 #2
nomad
664 Expert 512MB
[quote=mfshake]I am try to make the user enter 10 names and then display them out in
reverse order. I am having trouble with my for statement. can someone help me. Thanks

[code=java] Scanner reader = new Scanner (System.in);
String [] names = new String [10];

Also new String [10]; would give you 11 names not 10


nomad
Jan 29 '08 #3
BigDaddyLH
1,216 Expert 1GB
Also new String [10]; would give you 11 names not 10
That's wrong. maybe you are thinking of VB, but that will create an array of length 10, with indices [0] up to and including [9].
Jan 29 '08 #4
mfshake
16
the problem is with your for loop..instead start from 0, start from 9. the string is stored in array.
it says i have a negative value after my progam finishes
Jan 29 '08 #5
BigDaddyLH
1,216 Expert 1GB
it says i have a negative value after my progam finishes
It's hard to comment on code we can't see (hint).
Jan 29 '08 #6
mfshake
16
It's hard to comment on code we can't see (hint).
Expand|Select|Wrap|Line Numbers
  1.         Scanner reader = new Scanner (System.in);
  2.         String [] names = new String [10];
  3.         int count = 0;
  4.  
  5.         while(count < names.length){
  6.             System.out.print("Enter your name: ");
  7.             String name = reader.nextLine();
  8.             names[count] = name;
  9.             count++;
  10.         }//close while
  11.  
  12.         System.out.print("\nHere are the names:");
  13.         for(int i = 9; i < count; i--){
  14.             System.out.print("\n" + names[i] + "\n");
  15.         }//close for
  16.  
Jan 29 '08 #7
BigDaddyLH
1,216 Expert 1GB
Remember a loop terminates when its test condition is false.
Jan 30 '08 #8
Remember a loop terminates when its test condition is false.
The best solution would be to look at each line of code and check the value of each variable and BigDaddy's hint!
Jan 30 '08 #9
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1.         Scanner reader = new Scanner (System.in);
  2.         String [] names = new String [10];
  3.         int count = 0;
  4.  
  5.         while(count < names.length){
  6.             System.out.print("Enter your name: ");
  7.             String name = reader.nextLine();
  8.             names[count] = name;
  9.             count++;
  10.         }//close while
  11.  
  12.         System.out.print("\nHere are the names:");
  13.         for(int i = 9; i < count; i--){
  14.             System.out.print("\n" + names[i] + "\n");
  15.         }//close for
  16.  
Better go through your loop "manually" writing down the values of i and count at each pass. That way you can be able to see what is going on (or what is not going on).
Jan 30 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
16
by: Simon | last post by:
I am a programming student and have recently missed two weeks of school due to a serious injury in a car accident. I am completing all of my assignments (via Labs) but have come across the...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
3
by: c++dummy | last post by:
I got this project for my class and I'm totally lost as to how to copy the 1d array with the bone name into a 2d array using this supposed strncpy function I'm supposed to create. I believe the...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
2
by: bmbvm5 | last post by:
Hello; I am beginner in java and I should submit one java program in early day. I try to write this program more then ten time but every time I find one broblem. Please help me because if you...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.