1. Member
Join Date
Feb 2012
Posts
23
Rep Power
0

## Help in Arrays

1. Ask of a specific String
2. The array will only contain integral values
3. Display the length of the original String
4. Display the length of the array
5. Display the highest integer of the array
6. Display the lowest integer of the array
7. Display the closest int to zero

ex 1.
String input: a97r2-5x1-1
Array: [9,7,2,-5,1,-1]
Length of String: 11
Length of Array: 6
Highest int = 9
Lowest int = -5
Int Closest to zero= 1(if the there are two int closest to zero like -1 and 1, it should be the negative)

ex 2.
String input: -3-A101B3
Array: [-3,-1,0,1,3]
Length of String: 9
Length of Array: 5
Highest int = 3
Lowest int = -3
Int Closest to zero= 1

ex. 3
String input: ----------5a---------32
array :[-5,-3,2]
Length of original String: 23
Length of array: 3
The highest int: 2
The Lowest int: -5
Closest int to zero: 2

So here is my source codes, I'm quite noob so please help me. tnx. my problem is on example 2.
-A has joined. i think the problem is in the red codes

PHP Code:
```import java.util.*;

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

int highestInt = -99999999, lowestInt =0,i =0, closestToZero, lowestPosi=999,highestNega=-999999;
String numbers = "[", forNumbersArray= "", forNumbersArrayLength ="";

Scanner input = new Scanner(System.in);

System.out.println("String input: ");
String inputString = input.nextLine();

char inputStringArray [] = inputString.toCharArray();

for(int x = 0; x<inputStringArray.length; x++)
{
if(Character.isDigit(inputStringArray[x])){
if(x==0){
numbers += inputStringArray[x];
}else{
numbers += ","+ inputStringArray[x];
}

forNumbersArray += inputStringArray[x];
forNumbersArrayLength +=inputStringArray[x];

}
else if(inputStringArray[x]== '-'){

while(inputStringArray[x+1]== '-'){x++;}//trap for the --------

//if(Character.isDigit(inputStringArray[x])){}
if(x==0){                                                        //<<  I think
numbers += "-"+inputStringArray[x+1];            // << the problem
}else{                                                           // << is in Here
numbers += ",-"+inputStringArray[x+1];          // <<
}                                                                // <<

forNumbersArray +="-"+ inputStringArray[x+1];
forNumbersArrayLength +=inputStringArray[x+1];
x++; //x++;
}
else{

}
}
System.out.println("array :"+numbers+"]");
System.out.println("Length of original String: "+inputStringArray.length);

char numbersArrayLength [] = forNumbersArrayLength.toCharArray();
char numbersArray [] = forNumbersArray.toCharArray();
System.out.println("Length of array: "+numbersArrayLength.length);

//System.out.println("length katong taas: "+numbersArray.length);
//==============================================================================
String theNumbers [] = new String [numbersArray.length];
for(int x = 0; x<numbersArray.length; x++)
{
if(numbersArray[x]== '-')
{

theNumbers [i] = "-"+numbersArray[x+1];
x++; //x++;
}
else
{
theNumbers [i] = numbersArray[x]+"";
}
i++;

}

int theNumbersConverted [] = new int [theNumbers.length];

for(int x=0; x<theNumbersConverted.length;x++)
{
try
{
theNumbersConverted[x] = Integer.parseInt(theNumbers[x]);
}catch(NumberFormatException nfe)
{
theNumbersConverted[x] = Integer.parseInt(theNumbers[0]);
}

}

for(int x = 0; x<theNumbersConverted.length;x++)
{
if(lowestInt>theNumbersConverted[x]){
lowestInt = theNumbersConverted[x];
}
if(highestInt<theNumbersConverted[x]){
highestInt = theNumbersConverted[x];
}else{
highestInt = highestInt;
}

}

for(int x=0; x<theNumbersConverted.length;x++)
{
try
{

}catch(NumberFormatException nfe)
{
theNumbersConverted[x] = lowestInt;
}

}

System.out.println("The highest int: "+highestInt);
System.out.println("The Lowest int: "+lowestInt);

for(int x =0; x<theNumbersConverted.length;x++)
{
//if(lowestPosi>theNumbersConverted[x])
//lowestPosi= theNumbersConverted[x];

if(theNumbersConverted[x]<0)
{
if(highestNega<theNumbersConverted[x])
highestNega = theNumbersConverted[x];
}
else
{
if(theNumbersConverted[x]==0){}
else if(lowestPosi>theNumbersConverted[x])
lowestPosi= theNumbersConverted[x];
}
}
closestToZero=lowestPosi;

if(lowestPosi<Math.abs(highestNega))
closestToZero= lowestPosi;
else if(lowestPosi>=Math.abs(highestNega))
closestToZero = highestNega;

//System.out.println("Lowest Positive: "+lowestPosi);
//System.out.println("Highest Negative: "+highestNega);
System.out.println("Closest int to zero: "+closestToZero);

}
}```
Last edited by jairoh_; 02-01-2012 at 07:35 AM.

2. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## Re: Help in Arrays

What's the expected output in each case ?

3. Moderator
Join Date
Apr 2009
Posts
13,329
Rep Power
23

## Re: Help in Arrays

You're doing waaaay too much in a single method there.
Break the problem down, and do those bits in separate methods.
That way you can test each method and narrow down the location of logic problems.

4. Member
Join Date
Feb 2012
Posts
23
Rep Power
0

## Re: Help in Arrays

Originally Posted by bams
What's the expected output in each case ?
the output is in the above 3 examples.

Originally Posted by Tolls
You're doing waaaay too much in a single method there.
Break the problem down, and do those bits in separate methods.
That way you can test each method and narrow down the location of logic problems.
i already finished it sir but is my source code to long? here it is
import java.util.*;

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

int highestInt = -99999999, lowestInt =0,i =0, closestToZero, lowestPosi=999,highestNega=-999999;
String numbers = "[", forNumbersArray= "", forNumbersArrayLength ="";
int a = 0;
//int CHARACTER_IS_NUMBER = 9;

Scanner input = new Scanner(System.in);

System.out.println("String input: ");
String inputString = input.nextLine();

char inputStringArray [] = inputString.toCharArray();

for(int x = 0; x<inputStringArray.length; x++)
{
if(inputStringArray[x]== '-'){

while(inputStringArray[x+1]=='-'){x++;}
if(x==0){
if(Character.getType(inputStringArray[x+1])==Character.DECIMAL_DIGIT_NUMBER){
numbers += "-"+inputStringArray[x+1];
}
}else{
if(Character.getType(inputStringArray[x+1])==Character.DECIMAL_DIGIT_NUMBER){
numbers += ",-"+inputStringArray[x+1];
}
}
if(Character.getType(inputStringArray[x+1])==Character.DECIMAL_DIGIT_NUMBER){
forNumbersArray +="-"+ inputStringArray[x+1];
}
if(Character.getType(inputStringArray[x+1])==Character.DECIMAL_DIGIT_NUMBER){
forNumbersArrayLength +=inputStringArray[x+1];
}
x++; //x++;
}else if(Character.isDigit(inputStringArray[x])){
if(x==0){
numbers += inputStringArray[x];
}else{
numbers += ","+ inputStringArray[x];
}

forNumbersArray += inputStringArray[x];
forNumbersArrayLength +=inputStringArray[x];

}
else{

}
}

System.out.println("array :"+numbers+"]");
System.out.println("Length of original String: "+inputStringArray.length);

char numbersArrayLength [] = forNumbersArrayLength.toCharArray();
char numbersArray [] = forNumbersArray.toCharArray();
System.out.println("Length of array: "+numbersArrayLength.length);

//System.out.println("length katong taas: "+numbersArray.length);
//================================================== ============================

String theNumbers [] = new String [numbersArray.length];
for(int x = 0; x<numbersArray.length; x++)
{
if(numbersArray[x]== '-')
{

theNumbers [i] = "-"+numbersArray[x+1];
x++; //x++;
}
else
{
theNumbers [i] = numbersArray[x]+"";
}
i++;

}

int theNumbersConverted [] = new int [theNumbers.length];

for(int x=0; x<theNumbersConverted.length;x++)
{
try
{
theNumbersConverted[x] = Integer.parseInt(theNumbers[x]);
}catch(NumberFormatException nfe)
{
theNumbersConverted[x] = Integer.parseInt(theNumbers[0]);
}

}

for(int x = 0; x<theNumbersConverted.length;x++)
{
if(lowestInt>theNumbersConverted[x]){
lowestInt = theNumbersConverted[x];
}
if(highestInt<theNumbersConverted[x]){
highestInt = theNumbersConverted[x];
}else{
highestInt = highestInt;
}

}

/*
for(int x=0; x<theNumbersConverted.length;x++)
{
try
{

}catch(NumberFormatException nfe)
{
theNumbersConverted[x] = lowestInt;
}

}
*
*/

System.out.println("The highest int: "+highestInt);
System.out.println("The Lowest int: "+lowestInt);

for(int x =0; x<theNumbersConverted.length;x++)
{
//if(lowestPosi>theNumbersConverted[x])
//lowestPosi= theNumbersConverted[x];

if(theNumbersConverted[x]<0)
{
if(highestNega<theNumbersConverted[x])
highestNega = theNumbersConverted[x];
}
else
{
if(theNumbersConverted[x]==0){}
else if(lowestPosi>theNumbersConverted[x])
lowestPosi= theNumbersConverted[x];
}
}
closestToZero=lowestPosi;

if(lowestPosi<Math.abs(highestNega))
closestToZero= lowestPosi;
else if(lowestPosi>=Math.abs(highestNega))
closestToZero = highestNega;

//System.out.println("Lowest Positive: "+lowestPosi);
//System.out.println("Highest Negative: "+highestNega);
System.out.println("Closest int to zero: "+closestToZero);

}
}
Last edited by jairoh_; 02-01-2012 at 01:59 PM.

5. Moderator
Join Date
Apr 2009
Posts
13,329
Rep Power
23

## Re: Help in Arrays

Yes.
A method should do one thing, and should be able to fit on a screen (so 40 or 50 lines long at the most).

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•