Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Georgino

Thread: Strings and Array!!!

  1. #1
    Georgino is offline Member
    Join Date
    Jan 2013
    Location
    London
    Posts
    7
    Rep Power
    0

    Angry Strings and Array!!!

    I have a task to complete:

    Write a program that accepts ID, Name, E-mail address and Contact No. of all the students in a class and save them in an array. Ask the user for the size of the class and the student details.

    So far what I did:

    package tries;

    import java.util.Scanner;

    public class Tries { public static void main(String[] args) {


    Scanner s = new Scanner(System.in);

    System.out.print("Please enter the number of students: ");
    String[] text = new String[s.nextInt()];
    System.out.println();

    for(int i=0; i<text.length; i++) {
    System.out.print("Please enter student ID of the " +(i+1)+ ". student: ");
    text[i] = s.nextLine();
    }

    for(int i=0 ; i<text.length; i++) System.out.print(text[i]);
    }}

    Somehow, when I run it it does not ask for the first member of the array. Why?

    I also tried with a simple texts, i.e. not asking user for data, it works. Why does it not work with user input? I just cannot understand.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,383
    Blog Entries
    7
    Rep Power
    20

    Default Re: Strings and Array!!!

    The nextInt() method reads an int from its stream or it fails; after having read an int it stops reading; if you think you type an int you have also typed a new-line character and it isn't read by the nextInt() method. Check the Scanner class how to read the rest of the line so you get rid of that new-line character.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Georgino is offline Member
    Join Date
    Jan 2013
    Location
    London
    Posts
    7
    Rep Power
    0

    Default Re: Strings and Array!!!

    Thanks,

    I found this solution:

    package tries;

    import java.util.Scanner;

    public class Tries { public static void main(String[] args) {


    int x;
    String text[];
    Scanner s = new Scanner(System.in);

    System.out.print("Please enter the number of students: ");
    x = s.nextInt();


    text = new String[x];
    s.nextLine();


    for(int i=0; i<text.length; i++) {
    System.out.print("Please enter student ID of the " +(i+1)+ ". student: ");
    text[i] = s.nextLine();
    }

    for(int i=0 ; i<text.length; i++) System.out.print(text[i]);
    }}

    It worked, and then i developed the program introducing a two-dimensional array. Problems came up again:

    package tries;

    import java.util.Scanner;

    public class Tries { public static void main(String[] args) {


    int x;
    String text[][];
    Scanner s = new Scanner(System.in);

    System.out.print("Please enter the number of students: ");
    x = s.nextInt();


    text = new String[x][4];
    s.nextLine();


    for(int i=0; i<text.length; i++) {
    System.out.println("--------------------");
    System.out.println((i+1)+". Student");
    System.out.println();
    System.out.print("Student ID: ");
    text[i][1] = s.nextLine();
    System.out.print("Full name: ");
    text[i][2] = s.nextLine();
    System.out.print("E-mail address: ");
    text[i][3] = s.nextLine();
    System.out.print("Phone number: ");
    text[i][4] = s.nextLine();
    }
    }}

    I got the result below:

    run:
    Please enter the number of students: 2
    --------------------
    1. Student

    Student ID: 254
    Full name: Gregor Smith
    E-mail address: xxxxxx
    Phone number: 0740254801542
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at tries.Tries.main(Tries.java:31)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 31 seconds)

    What is the problem?
    Last edited by JosAH; 01-08-2013 at 12:37 PM. Reason: Email address removed to protect the innocent ...

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Strings and Array!!!

    I just hope this gregor is not a real person or he will receive a lot of mail the next days... ^^

    The problem is that you do not use code tags for posting your code and that indices start at ZERO... 0, 1,2,3 not 1,2,3,4 :)
    I like likes!

  5. #5
    Georgino is offline Member
    Join Date
    Jan 2013
    Location
    London
    Posts
    7
    Rep Power
    0

    Default Re: Strings and Array!!!

    No, Gregor does not exist. I ma not sure about the e-mail address, never tried!!!!!
    Sierra likes this.

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  2. array containing int strings
    By ggalan in forum New To Java
    Replies: 19
    Last Post: 04-22-2011, 04:40 PM
  3. Strings as array help please..
    By crazygurl in forum New To Java
    Replies: 1
    Last Post: 12-01-2009, 08:23 PM
  4. 2 dimensional array with strings.
    By dbashby in forum New To Java
    Replies: 12
    Last Post: 10-13-2009, 10:52 PM
  5. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 07:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •