[JAVA] help with array of strings

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
User avatar
silver calc
New Member
Posts: 73
Joined: Tue 28 Mar, 2006 10:50 pm
Location: Wouldn't you like to know?

[JAVA] help with array of strings

Post by silver calc »

I've been trying to create an array of strings in Java. Here's my code (only the parts giving me problems):

In Class A:

Code: Select all

public class A{
 private String a[10];

 public void changeElement(String e, int num){
  a[num]=e;
 }

}
Here's my application code:

Code: Select all

import Java.util.Scanner;
public class App{
 Scanner reader=new Scanner(System.in);
 System.out.print("Elements(less than 10):");
 int j=reader.nextInt();
 A test=new A();
 for(int i=0; i<j; i++){
  While(reader.nextLine()=="\n");
  System.out.print("\nInput element "+i+":");
  A.changeElement(reader.nextLine(),i);
 }
}
What's wrong with this? It compiles fine, but throws a runtime error pointing to changeElement when I when I try to run this code and it reaches that point.
Please "encourage" me to work more on Image any way you deem necessary
threefingeredguy
Calc King
Posts: 2195
Joined: Sun 27 Mar, 2005 4:06 am
Location: sleeping
Contact:

Post by threefingeredguy »

What's the error?
I don't think System.in is enough for that to read a string. I think it's got to be

Code: Select all

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
but I don't know what the Scanner class is, so I can't say that's the problem for sure.
Image
User avatar
crzyrbl
Calc Wizard
Posts: 518
Joined: Wed 06 Jul, 2005 4:56 pm
Location: 3rd rock....

Post by crzyrbl »

May, or may not help:

Code: Select all

import java.io.*;
public class UserInput
{
  private BufferedReader input = null;

  public UserInput()
  {
       input = new BufferedReader(new InputStreamReader(System.in));

  }

  public String getValue(String question) throws IOException
  {
       System.out.print(question);
       String answerStr = input.readLine();
	   return answerStr;
  }

}
(\__/)
(='.'=)This is Bunny. Copy and paste bunny into your
(")_(")signature to help him gain world domination.

Image
User avatar
silver calc
New Member
Posts: 73
Joined: Tue 28 Mar, 2006 10:50 pm
Location: Wouldn't you like to know?

Post by silver calc »

Huh? What version of java are you using?

In the newer versions there is a scanner class that deals with input. I just want to know how to take user input and put it into a String array.
Please "encourage" me to work more on Image any way you deem necessary
User avatar
anykey
Extreme Poster
Posts: 420
Joined: Mon 31 Jan, 2005 3:36 am
Location: In the matrix
Contact:

Post by anykey »

It looks fine to me.
Perhaps you should use an arraylist? I find them quite enjoyable.
oh, and changeElement should probably be renamed addElement, or maybe just add.
I'm glad I could not help. :)
I think, therefore iMac
Image
User avatar
silver calc
New Member
Posts: 73
Joined: Tue 28 Mar, 2006 10:50 pm
Location: Wouldn't you like to know?

Post by silver calc »

nevermind, i figured it out. turns out that initializing arrays in java are different:
C++ way:

Code: Select all

char array[maxsize][5];    //max size is a large number so that the string doesn't run out of space
Java way:

Code: Select all

String[] array = new String[5];
Please "encourage" me to work more on Image any way you deem necessary
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

What compiler are you using? You should get a compiler error because that syntax is simply not valid in Java.
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Yes, I thought it odd that Java used the strange and rather non-intuitive array declaration syntax from C. I'm glad it doesn't. :)

That top example is not the C++ way. If you think it is, take a look at std::string instead of char arrays (which are, unfortunately, the C way).
Spencer
Extreme Poster
Posts: 346
Joined: Mon 17 Jan, 2005 8:56 am
Location: Indiana

Post by Spencer »

Code: Select all

char array[5][maxsize];
User avatar
crzyrbl
Calc Wizard
Posts: 518
Joined: Wed 06 Jul, 2005 4:56 pm
Location: 3rd rock....

Post by crzyrbl »

oh ya, I forgot about that. I haven't used Java in a while.
(\__/)
(='.'=)This is Bunny. Copy and paste bunny into your
(")_(")signature to help him gain world domination.

Image
Post Reply