Results 1 to 5 of 5
Thread: Help in Arrays
- 02-01-2012, 06:10 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 19
- 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 06:35 AM.
- 02-01-2012, 12:34 PM #2
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
Re: Help in Arrays
What's the expected output in each case ?
- 02-01-2012, 12:51 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 02-01-2012, 12:55 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Re: Help in Arrays
the output is in the above 3 examples.
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 12:59 PM.
- 02-01-2012, 02:06 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays
By PhQ in forum New To JavaReplies: 3Last Post: 04-18-2010, 08:40 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Arrays
By TheRocket in forum New To JavaReplies: 6Last Post: 12-10-2008, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks