473,386 Members | 1,798 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,386 software developers and data experts.

String Arrays and user input

Hey all, Im trying to write to the values of the string array studentNames using a for loop; however, when using the for loop it seems to skip the first iteration of the loop and makes studentNames[0] = "". why is it doing this? and what can I do to fix it? Any help would be appreciated thanks!
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args)
  2.              {
  3.                  Scanner in = new Scanner(System.in);
  4.  
  5.  
  6.                //finding the length of the Array studentNames
  7.                System.out.print("how many students?:");
  8.                int totalStudents = in.nextInt();
  9.  
  10.      String[] studentNames = new String [totalStudents];
  11.  
  12.         //allows user to input student names
  13.  
  14.     for(int j = 0; j < studentNames.length;j++) 
  15.     {
  16.         System.out.println(j);
  17.         System.out.print("enter student names:");
  18.         studentNames[j] = in.nextLine();
  19.     }
  20.  
  21.         for (String element:studentNames)
  22.         {   
  23.        System.out.println(element)
  24.         }
  25. }
Apr 9 '11 #1

✓ answered by Dheeraj Joshi

Change nextInt() to nextLine(). That will solve your problem.

Expand|Select|Wrap|Line Numbers
  1. int totalStudents = Integer.parseInt(in.nextLine());
Refer this link for info about nextLine().
Refer this link for info about nextInt().

Regards
Dheeraj Joshi

2 77644
Dheeraj Joshi
1,123 Expert 1GB
Change nextInt() to nextLine(). That will solve your problem.

Expand|Select|Wrap|Line Numbers
  1. int totalStudents = Integer.parseInt(in.nextLine());
Refer this link for info about nextLine().
Refer this link for info about nextInt().

Regards
Dheeraj Joshi
Apr 11 '11 #2
Make different object of Scanner and use it to take string

Scanner in2 = new Scanner(System.in);
//
//
//
studentNames[j] = in2.nextLine();
May 12 '15 #3

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

Similar topics

2
by: Kyle E | last post by:
Ok, I have a little tiny problem, a beginner problem, that I am overlooking. I am writing a Information Gathering Program, that takes user input from scripted questions and prints them in a handy...
6
by: ASPfool | last post by:
Hello everyone, Please help me before I throw my computer out of the window: I'm working on a web application in classic ASP (vbscript), which is making calls to some webservices. The calls...
1
by: Kelly Mandrake | last post by:
I've learned that some compilers include a string class, and I have been reading about it. I can use it in the normal way but I have a question. If I want to have user input stored into my...
14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
0
by: Paulers | last post by:
hello VB masters I have an issue that I am hoping you can help me with. I am creating an application that accepts user input via a richtextbox. the user can enter multiple entries and I need to...
13
by: td0g03 | last post by:
Hello again guys. I have a question. I'm working on a program for my class and I'm stuck on how to find the lowest and highest number of a user input. We aren't at arrays yet so that's out of the...
4
by: slacker7272 | last post by:
hey, i have only been working with c for a very short time and i have a queston that seems to be poorly covered in all areas i have looked. i am attempting to create an array of strings so that 9...
6
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of...
1
by: Chris Carlen | last post by:
Hi: I'm writing a Python program, a hex line editor, which takes in a line of input from the user such as: -e 01 02 "abc def" 03 04 Trouble is, I don't want to split the quoted part where...
14
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.