Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-25-2008, 06:11 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-25-2008, 06:14 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-25-2008, 06:17 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
From what I get from your question is yes. Maybe this will help.

Code:
//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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-25-2008, 06:20 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
I would use a vector here but you should just do my
String[] myString = new String[What Ever Size];
myString[i] = input.getNextLine();
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-25-2008, 06:23 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-25-2008, 06:27 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-25-2008, 06:28 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
I'm pretty familiar with making an array with hardcoded information. But what's confusing me is the array with user input.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-25-2008, 06:30 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-25-2008, 06:36 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
Would that work using my trim variable? I'm kind of confused by it.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-25-2008, 06:42 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-25-2008, 06:52 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
How would I go about using ArrayList?

would it be like
Code:
ArrayList[] myString = new ArrayList[]; System.out.println("Enter sentence: "); myString[] = input.getNextLine;
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-25-2008, 07:00 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Defining array list is wrong there.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-25-2008, 07:07 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
I'm guessing add adds the string and index to the array correct?
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-25-2008, 07:09 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
--------------------Configuration: <Default>--------------------
Note: G:\AP Comp. Sci\Labs\p499proj12l2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.


eh...?
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-25-2008, 07:10 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
If you want to do it with an array, do it as follows.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-25-2008, 07:15 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by apfroggy0408 View Post
--------------------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.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 04-25-2008, 07:15 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 04-25-2008, 07:19 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by apfroggy0408 View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 04-28-2008, 07:14 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
So, haven't been able to get back to this program 'til now, and I'm still lost.

I have the code

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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 04-28-2008, 08:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,967
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes