|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-25-2008, 06:11 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
|
Making arrays by reading user input
Arrays are definitely one of my weak points, and I'm confused as crap as to how to make an array of a string input.
|
|

04-25-2008, 06:14 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
|
|
|
What do you mean? every input shall be stored in and array?
Correct me if im too far from your point
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 06:17 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
From what I get from your question is yes. Maybe this will help.
//call for input
Scanner input = new Scanner(System.in);
System.out.println("Enter sentence: ");
String sent = input.nextLine;
// How do I get sent into an array?
|
|

04-25-2008, 06:20 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
I would use a vector here but you should just do my
String[] myString = new String[What Ever Size];
myString[i] = input.getNextLine();
|
|

04-25-2008, 06:23 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
|
|
|
But here defining the array size can be make an error. ArrayList may be a good choice.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 06:27 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
|
|
|
String array[] = {"Me","You","Them","Us","Other","They"};
Can you access every element of an array?
If not, try to have an experiment....
String s = array[3];
int len = array.length;
String s1 = array[7];
String s2 = array[-1];
Try to observe the output....
By storing an input to an array,
array[0] = "Chupacabras";
array[1] = "Rasta";
array[2] = true;
array[3] = false;
Try to observe what happens now when you seek again the value of every element of the array.... ( showing it again ).....
But it is important to read a tutorials about arrays....
There are more and understandable explanations there....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 06:28 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
I'm pretty familiar with making an array with hardcoded information. But what's confusing me is the array with user input.
Scanner input = new Scanner(System.in);
System.out.println("Enter String to use: ");
String list = input.nextLine();
String trimmedword = list.trim();
System.out.println("Enter Index: ");
int index = input.nextInt();
Is what I have to start with.
I'm using the trim because I need to able to manipulate this array with the index choice. So I need to turn that trimmed string into an array.
Last edited by apfroggy0408 : 04-25-2008 at 06:32 AM.
|
|

04-25-2008, 06:30 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
|
|
|
Now you got the answer right. Is that not clear what Zosden says?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 06:36 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
|
Would that work using my trim variable? I'm kind of confused by it.
|
|

04-25-2008, 06:42 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
|
|
|
Not to worry with trim(), if a white space is there or no, in an Array it not a case.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 06:52 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
How would I go about using ArrayList?
would it be like
ArrayList[] myString = new ArrayList[];
System.out.println("Enter sentence: ");
myString[] = input.getNextLine;
|
|

04-25-2008, 07:00 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
|
|
Defining array list is wrong there.
ArrayList al = new ArrayList();
Scanner input = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = input.nextLine().trim();
System.out.println("Enter an index: ");
int index = input.nextInt();
al.add(index, str);
Loop this to handle for any number of inputs.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 07:07 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
|
I'm guessing add adds the string and index to the array correct?
|
|

04-25-2008, 07:09 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
|
--------------------Configuration: <Default>--------------------
Note: G:\AP Comp. Sci\Labs\p499proj12l2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
eh...?
|
|

04-25-2008, 07:10 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
|
|
If you want to do it with an array, do it as follows.
String[] mm = new String[10];
Scanner input = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = input.nextLine().trim();
System.out.println("Enter an index: ");
int index = input.nextInt();
mm[index] = str;
Issue is there, defining the size of the array. According to my code, you can't exceed 10 for index.
ArrayList is something different. Size is grow depending on your insertion into the list.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 07:15 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
|
|
Originally Posted by apfroggy0408
--------------------Configuration: <Default>--------------------
Note: G:\AP Comp. Sci\Labs\p499proj12l2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
eh...?
Ah, it's the warning that ArrayList is not defined as an expandable array. It's not a big case for the simple processing, like we do here.
If you want to avoid it, change the ArrayList definition as follows.
ArrayList<String> al = new ArrayList<String>();
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 07:15 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
|
Oh, alright. Now the question I have now is why do you have index placed in []? I'm going to be using the inputted index to find a certain spot in the array. As in index 2 in shoe would be h.
|
|

04-25-2008, 07:19 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
|
|
Originally Posted by apfroggy0408
Oh, alright. Now the question I have now is why do you have index placed in []?
That's the inbuilt pattern pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-28-2008, 07:14 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 26
|
|
So, haven't been able to get back to this program 'til now, and I'm still lost.
I have the code
ArrayList<String> mm = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = input.nextLine().trim();
System.out.println("Enter an index: ");
int index = input.nextInt();
mm[index] = str;
System.out.println(mm);
System.out.println(index);
But I can't get the array to show.
|
|

04-28-2008, 08:05 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
|
|
|
You want to display all array elements? Loop it.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
| |