Got questions? Got answers? Go here for both.
Moderator: MaxCoderz Staff
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 » Mon 22 Jan, 2007 10:33 pm
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
any way you deem necessary
threefingeredguy
Calc King
Posts: 2195 Joined: Sun 27 Mar, 2005 4:06 am
Location: sleeping
Contact:
Post
by threefingeredguy » Mon 22 Jan, 2007 11:11 pm
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.
crzyrbl
Calc Wizard
Posts: 518 Joined: Wed 06 Jul, 2005 4:56 pm
Location: 3rd rock....
Post
by crzyrbl » Mon 22 Jan, 2007 11:19 pm
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.
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 » Tue 23 Jan, 2007 12:23 am
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
any way you deem necessary
anykey
Extreme Poster
Posts: 420 Joined: Mon 31 Jan, 2005 3:36 am
Location: In the matrix
Contact:
Post
by anykey » Tue 23 Jan, 2007 3:19 am
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
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 » Tue 23 Jan, 2007 5:23 am
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:
Please "encourage" me to work more on
any way you deem necessary
coelurus
Calc Wizard
Posts: 585 Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:
Post
by coelurus » Tue 23 Jan, 2007 7:26 am
What compiler are you using? You should get a compiler error because that syntax is simply not valid in Java.
benryves
Maxcoderz Staff
Posts: 3089 Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:
Post
by benryves » Tue 23 Jan, 2007 11:12 am
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 » Tue 23 Jan, 2007 1:59 pm
crzyrbl
Calc Wizard
Posts: 518 Joined: Wed 06 Jul, 2005 4:56 pm
Location: 3rd rock....
Post
by crzyrbl » Fri 26 Jan, 2007 8:16 am
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.