14 Replies - 396 Views - Last Post: 02 March 2013 - 03:11 PM Rate Topic: -----

#1 Eliash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-September 12

Help on extracting last part of an array

Posted 02 March 2013 - 08:20 AM

Hello all, I have a quick question I would like to ask.
I have been given an exercice where I have addresses, and I have to extract the ZIP code on it. Here it is
class Assignement6
{
public static void main (String []args)
{

String Address[] = {"17, 3FR, Rue Bonaparte, 75001", "9, 4FL, Rue Joseph Bara, 75006", "85, 2FL, Rue Rivoli, 75004", "75, 4FR, Rue De L'Opera, 75012", "29, 9FR, Rue Port-Royal, 75018", "59, 1FL, Rue Pierre Nicole, 75005", "58, 7FR, Rue Monge, 75002", "15, 6FR, Rue Voltaire, 75006", "111, 13FL, Rue Auguste, 75004", "53, 4FR, Rue Des Saints-Pere, 75008"};

String ZIPStr; //variable to hold zip as a string

int ZIPInt; //variable to hold zip as int


for(int i=0; i<Address.length; i++)
{
ZIPStr= Address[i].length-6;
}


System.out.println(ZIPStr);






}
}



So I basically need to get the 750.. into a variable and then print it. How may I do that?
Thanks a lot

This post has been edited by Eliash: 02 March 2013 - 08:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help on extracting last part of an array

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Help on extracting last part of an array

Posted 02 March 2013 - 08:29 AM

Each string in the Address[] array (should have a lower-case name) has the form:

"17, 3FR, Rue Bonaparte, 75001"

For example,

Address[0] = "17, 3FR, Rue Bonaparte, 75001";

Address[0] could be split into its 4 parts using the split() method:

String[] subAddress0 = Address[0].split( "," );

Then,

subAddress0[3] = "75001";
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Help on extracting last part of an array

Posted 02 March 2013 - 08:29 AM

Its the last 5 characters.
The right most 5 characters.
A substring of the entire string starting at the total length minus 5.
Its the item after the last occurrence of a comma.

Surely all of these different ways of looking at the problem must jog some idea making use of either .split or .substring.

This post has been edited by tlhIn`toq: 02 March 2013 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 Eliash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-September 12

Re: Help on extracting last part of an array

Posted 02 March 2013 - 08:34 AM

Thank you for answering. I believe I was asked to use the substring function. Could you explain in a little more details and how it could be used in this particular case.
Thanks

EDIT:
I got this so far
class Assignement6
{
public static void main (String[] args)
{

String Address[] = {"17, 3FR, Rue Bonaparte, 75001", "9, 4FL, Rue Joseph Bara, 75006", "85, 2FL, Rue Rivoli, 75004", "75, 4FR, Rue De L'Opera, 75012", "29, 9FR, Rue Port-Royal, 75018", "59, 1FL, Rue Pierre Nicole, 75005", "58, 7FR, Rue Monge, 75002", "15, 6FR, Rue Voltaire, 75006", "111, 13FL, Rue Auguste, 75004", "53, 4FR, Rue Des Saints-Pere, 75008"};


String[] ZIPStr = new String [10]; //variable to hold zip as a string

int ZIPInt; //variable to hold zip as int


for(int i=0; i<Address.length; i++)
{
ZIPStr[i] = Address[i].substring(Address.length-6, Address.length);
}

for (int i=0; i<10; i++)
{
System.out.println(ZIPStr[i]);
}



}
}



But it prints this out

3FR, R
FL, Ru
2FL, R
4FR, R
9FR, R
1FL, R
7FR, R
6FR, R
 13FL,
4FR, R



Any ideas?
Thank you

This post has been edited by Eliash: 02 March 2013 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Help on extracting last part of an array

Posted 02 March 2013 - 08:46 AM

View PosttlhIn`toq, on 02 March 2013 - 09:29 AM, said:



Click the link - its a 'how to' page.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,217
  • Joined: 20-September 08

Re: Help on extracting last part of an array

Posted 02 March 2013 - 08:49 AM

Clue:
        for (String address : Address) {
            System.out.println(address.split("\\s*,\\s*")[3]);
        }

(if my notion of French 'zip codes' is correct)
Was This Post Helpful? 0
  • +
  • -

#7 Eliash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-September 12

Re: Help on extracting last part of an array

Posted 02 March 2013 - 08:52 AM

Thank you, but there is not much about the .length combined with the subString.
The part I posted works, but for some reason does not print out the last numbers
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Help on extracting last part of an array

Posted 02 March 2013 - 09:03 AM

Let's see - A string is an array of char.
Array's are zero indexed, so the 1st char is element 0.
The zip code is 5 characters and in line 16 you go back six from the end.
You aren't taking into account the zero indexing.

Adjust your starting point accordingly. If you see you are getting answers that start too far back, then make adjustments. We can't do your homework for you. You need to play with it a bit and learn as you go. Some active work and participation on your part is needed.

Play with that -6 part. Make it -2. Make it -10. See what happens to your output. Gain some comprehension on the how and why of it all.

This post has been edited by tlhIn`toq: 02 March 2013 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#9 Eliash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-September 12

Re: Help on extracting last part of an array

Posted 02 March 2013 - 09:10 AM

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

String Address[] = {"17, 3FR, Rue Bonaparte, 75001", "9, 4FL, Rue Joseph Bara, 75006", "85, 2FL, Rue Rivoli, 75004", "75, 4FR, Rue De L'Opera, 75012", "29, 9FR, Rue Port-Royal, 75018", "59, 1FL, Rue Pierre Nicole, 75005", "58, 7FR, Rue Monge, 75002", "15, 6FR, Rue Voltaire, 75006", "111, 13FL, Rue Auguste, 75004", "53, 4FR, Rue Des Saints-Pere, 75008"};


String[] ZIPStr = new String [10]; //variable to hold zip as a string

int ZIPInt; //variable to hold zip as int


for(int i=0; i<Address.length; i++)
{
ZIPStr[i] = Address[i].substring(Address.length-6, Address.length);
}

for (int i=0; i<10; i++)
{
System.out.println(ZIPStr[i]);
}



}
}


Even with this, I still dont get the right references. I have tried messing with it a bit, but it seems that it doesn't start from the end, it seems it starts from the middle.

See, in my logic, if I go from Address.length-6, it should start from the last 5 characters, because length is the end of the string (right?), and -6 refers to the last 5. Then it should end at Address.length, the end.
Where is the probleme please?
Thanks

This post has been edited by Eliash: 02 March 2013 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Help on extracting last part of an array

Posted 02 March 2013 - 09:36 AM

substring(StartAtThisIndex, ForThisManyCharacters)

All this help, links to tutorials, links to methods that get you just the right side...
Any more and I'm doing the homework for you.
Work with it for more than 5 minutes and it will start sinking in.
Was This Post Helpful? -1
  • +
  • -

#11 Eliash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-September 12

Re: Help on extracting last part of an array

Posted 02 March 2013 - 09:43 AM

I have been working on these 20 lines since 3:30. I must be retard. I can't figure it out.
Non the less many thanks for the help.

This post has been edited by Eliash: 02 March 2013 - 09:46 AM

Was This Post Helpful? 0
  • +
  • -

#12 Eliash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-September 12

Re: Help on extracting last part of an array

Posted 02 March 2013 - 10:04 AM

View PostEliash, on 02 March 2013 - 09:43 AM, said:

I have been working on these 20 lines since 3:30. I must be retard. I can't figure it out.
Non the less many thanks for the help.


EDIT: Im so sorry I pressed - on the did that help you, I wanted to click +
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Help on extracting last part of an array

Posted 02 March 2013 - 02:22 PM

View PostEliash, on 02 March 2013 - 11:04 AM, said:

View PostEliash, on 02 March 2013 - 09:43 AM, said:

I have been working on these 20 lines since 3:30. I must be retard. I can't figure it out.
Non the less many thanks for the help.


EDIT: Im so sorry I pressed - on the did that help you, I wanted to click +


No worries mate. We all goof now and then. Just hit + on one of my other posts to negate the -
Was This Post Helpful? 1
  • +
  • -

#14 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Help on extracting last part of an array

Posted 02 March 2013 - 02:49 PM

He probably won't be back any time soon. I did if for you.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help on extracting last part of an array

Posted 02 March 2013 - 03:11 PM

ZIPStr[i] = Address[i].substring(Address.length-6, Address.length);

Address.length returns the length of the array: 10
Address[i].length() returns the length of the String in slot [i]

So you are doing substring(10-6, 10); but you want

ZIPStr[i] = Address[i].substring(Address[i].length()-6);

P.S.
no need to specify the second parameter if youfo to the end of the String and it should have been array[i].length() - 1 any how

This post has been edited by pbl: 02 March 2013 - 03:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1