Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 03-27-2008, 06:27 PM
Member
 
Join Date: Mar 2008
Location: Greenville, SC
Posts: 2
hayjk is on a distinguished road
[SOLVED] Problems with arrays
I am new to Java and I am having an issue with an array problem. I need to check the first digit in a String array and if it has a value of 0 or 9, I need to change it to a 1. Below is my code that is trying to do this. What I am having trouble with is populating the new array. Any help would be greatly appreciated.
Code:
for (int i = 0; i < nums.length; i++) { prinum[i] = nums[i].substring(5); char[] temp = prinum[i].toCharArray(); for (int x = 0; x < prinum[i].length(); x++) { if (prinum[i].charAt(x) == '9') { temp[x] = 1; } else if (prinum[i].charAt(x) == '0') { temp[x] = 1; } } String newprinum[i] = new String(temp);

Last edited by DonCash : 04-07-2008 at 07:49 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-27-2008, 07:03 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
for (int i = 0; i < nums.length; i++) { prinum[i] = nums[i].substring(5); // if the first character is a // digit [0 - 9] change it to a "1". // This loop will examine every letter // in the string "prinum[i]" char[] temp = prinum[i].toCharArray(); for (int x = 0; x < temp.length; x++) { char c = temp[x]; if (c == '9' || c == '8' || c == '7' ...) { temp[x] = 1; } } // Assign the corrected value of temp[x] // to "newprinum[i]" newprinum[i] = temp[x];
Code:
// Another possible way to look at it: String digits = "0123456789"; ... prinum[i] = nums[i].substring(5); String first = prinum[i].substring(0, 1); // Is the first letter a digit? if(digits.indexOf(first) != -1) { // change it to a "1" prinum[i] = "1" + prinum[1].substring(1); }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-27-2008, 10:05 PM
Member
 
Join Date: Mar 2008
Location: Greenville, SC
Posts: 2
hayjk is on a distinguished road
Thanks hardwired. I found an easier way to do it. Here is the code snippet i used.
Code:
for (int i = 0; i < nums.length; i++) { prinum[i] = nums[i].substring(6); firstnum[i] = nums[i].substring(5, 6); firstnum[i] = firstnum[i].replaceAll("9", "1"); firstnum[i] = firstnum[i].replaceAll("0", "1");
I appreciate your help.

Last edited by DonCash : 04-07-2008 at 07:51 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-07-2008, 07:52 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
I edited to put the [code] [/ code] tags round the code to make it easier to read.
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Some problems with arrays and loops BHCluster New To Java 3 04-16-2008 02:24 PM
new to arrays jimJohnson New To Java 1 04-08-2008 04:45 PM
2D-Arrays kbyrne New To Java 1 02-08-2008 12:08 AM
arrays help Warren New To Java 6 11-23-2007 09:23 PM
Problems with arrays Marcus New To Java 2 07-04-2007 10:10 AM


All times are GMT +3. The time now is 12:30 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org