4 Replies - 79 Views - Last Post: 04 February 2012 - 08:15 PM Rate Topic: -----

Topic Sponsor:

#1 Xponent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 12

Integers output problem

Posted 04 February 2012 - 06:20 PM

Hello, I'm having problems to display 2 columns which in 1 there would be all the positive integers values and in the other one all the positive values.

This is my code so far:

public static void main (String[] args)
    {
         System.out.print("\f");  

        int num1 =0;
        int totalp=0;
        int totaln=0;
        int avgp=0;
        int avgn=0;
        int greaterp=0;
        int greatern=0;
       int loop1=1;
       int amountp=0;
       int amountn=0;
        int castsquaredp=0;
        int castcubedp=0;
        int castsquaredn=0;
        int castcubedn=0;
   Scanner scan = new Scanner (System.in);
     

  
   
   System.out.println("\t\t\t"+"Positive"+"\t\t\t"+"Negative");
   

while(loop1!=0){
System.out.println("Enter Number: ");

num1=scan.nextInt();

if (num1>0)
 {
     totalp=totalp+num1;
     amountp=amountp+1;
     System.out.println("\t\t\t"+num1);
    }
 else if(num1<0)
 {
     totaln=totaln+num1;
    amountn=amountn+1;
    }
 else if(num1==0)
 {System.out.println("Cannot accept zeros Re-enter");
         num1=scan.nextInt();
       System.out.println("\t\t\t\t\t"+num1);
        }
         
       if (num1>0&&num1>greaterp)
       { greaterp=num1; }
       
       if (num1<0&&num1<Math.abs(greatern))
       {greatern=num1; }
           
 
     
     
       
        
    System.out.print("Press 0 to exit/Press any integer to continue ");   
        loop1=scan.nextInt();
    }
    double squaredp =(int) Math.pow(greaterp,2);
   double  cubedp =(int) Math.pow(greaterp,3);
   double squaredn= (int)Math.pow(greatern,2);
   double cubedn=(int) Math.pow(greatern,3); 
    castsquaredp=(int)squaredp;
     castcubedp=(int)cubedp;
     castsquaredn=(int)squaredn;
     castcubedn=(int)cubedn;
    
     
    if (totalp>0)
avgp=totalp/amountp;
if (totaln>0)
avgn=Math.abs(totaln)/amountn;
    System.out.println("The total positive integer values are: "+totalp);
   System.out.println("The total negative integer values are: "+totaln);
   System.out.println("The positive values average is : "+avgp);
   System.out.println("The negative values average is : "+avgn);
     
   
   
  
    System.out.println("The largest positive value is: "+greaterp+" This value squared  "+castsquaredp+" This value cubed "+castcubedp);
    
   System.out.println("The largest negative value is: "+greatern+" This value squared  "+castsquaredn+" This value cubed "+castcubedn);

   
    }


My question for you guys: Is it posssible to print two columns that organize the respective values without the use of an Array/ArrayList?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Integers output problem

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 719
  • View blog
  • Posts: 1,692
  • Joined: 05-May 05

Re: Integers output problem

Posted 04 February 2012 - 06:40 PM

Your giving too little information. Organize values how? Where does an array/list come into play here? Please give a high-level description of what your program is supposed to do. Is it possible to print two columns of data? Yes.

System.out.println("\t\t\t"+"Positive"+"\t\t\t"+"Negative");


Looks like your well on your way to accomplishing that. Usually, after that code you'd have a loop that prints your values (in a tabulated format).

Actually using tabs to format your data won't work well. You need to use format specifiers.
Was This Post Helpful? 0
  • +
  • -

#3 Xponent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 12

Re: Integers output problem

Posted 04 February 2012 - 06:52 PM

View Postblackcompe, on 04 February 2012 - 06:40 PM, said:

Your giving too little information. Organize values how? Where does an array/list come into play here? Please give a high-level description of what your program is supposed to do. Is it possible to print two columns of data? Yes.

System.out.println("\t\t\t"+"Positive"+"\t\t\t"+"Negative");


Looks like your well on your way to accomplishing that. Usually, after that code you'd have a loop that prints your values (in a tabulated format).

Actually using tabs to format your data won't work well. You need to use format specifiers.


Thanks for replying. Let me explain the assignment better.

The user needs to input integers, the integers need to be organized into 2 columns positive and negative(can't enter 0). When you input the desired integers you need to press 0 to exit. The program also needs to calculate the sum, average, the greatest positive integer value and the greatest negative value. Also it needs to square and cube of the greatest values.

As you said, I have a problem in organizing data. So I am looking for the best way to organize it.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 719
  • View blog
  • Posts: 1,692
  • Joined: 05-May 05

Re: Integers output problem

Posted 04 February 2012 - 07:29 PM

I see. The problem is that if your printing a new row, and you're looking at a negative number in your data, which belongs in the second column, you have to skip the first column, leaving it blank.

The solution would be to organize your data in such a way that you get alternating positive and negative numbers:

int data[] = {-1, 43, 2, -32, -3, 10};



needs to become

int data[] = {-1, 43, -32, 2, -3, 10};



They then its simply:

System.out.println("Negative\t\tPositive\n\n");
for(int i = 0; i < data.length-1; i++)
	System.out.println(data[i]+"\t\t"+data[i+1]);



You could throw the positives into an array and the negatives into another, sort them, then merge them, picking one from each to fill the next element of the resultant array. You never actually mention that the data needs to be sorted, so you don't have to sort the arrays, just merge them in the manner you need.

Or you could sort the arrays so that all negatives are first, using Arrays.sort, and then access them like this:

int data[] = {-1, 43, 2, -32, -3, 10};
Arrays.sort(data); //data becomes {-32, -3,-1, 2, 10, 43}

int split = findIndexOfFirstPositiveElement(data);

System.out.println("Negative\t\tPositive\n\n");
for(int i = 0; i < split; i++)
	System.out.println(data[i]+"\t\t"+data[split+i]);



With both methods you'll probably run into issues with data sets that have a differing number of positive and negative numbers, but it shouldn't be hard to remedy.
Was This Post Helpful? 1
  • +
  • -

#5 Xponent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 12

Re: Integers output problem

Posted 04 February 2012 - 08:15 PM

Thanks man!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1