# Need help formatting output and some code

• 11-09-2009, 03:07 AM
A5i19
Need help formatting output and some code
Alright, what I need help with is converting some code. The basics are I am reading in a file and need to output that file to the terminal window formatted as described below.

First my terminal window output equals:

There are complex numbers in quadrant number 1.
1.0+2.0i

There are complex numbers in quadrant number 4.
4.0+-3.2i

There are complex numbers in quadrant number r-axis.
12.3+0.0i

There are complex numbers in quadrant number i-axis.
0.0+-1.9i

There are complex numbers in quadrant number 3.
-1.0+-1.0i

There are complex numbers in quadrant number 2.
-1.0+2.3i

There are complex numbers in quadrant number 1.
4.0+4.0i

WHILE MY OUTPUT REALLY NEEDS TO LOOK LIKE THIS:

There are 2 complex numbers in quadrant 1.
tabbed over--> 1 + 2i
tabbed over--> 4 + 4i
There is 1 complex number in quadrant 2.
tabbed over--> -1 + 2.3i
There is 1 complex numbers in quadrant 3.
tabbed over--> 1 + -1i
There is 1 complex numbers in quadrant 4.
tabbed over--> 4 + -3.2i
There is 1 complex number on the r-axis.
tabbed over--> 12.3 + 0i
There is 1 complex number on the i-axis.
tabbed over--> 0 + -1.9i

So basically what I need is help formatting the output so that mine looks exactly like the above output while using only one static void main and keeping the code simple.
So what I need help with is
1. setting up a function that counts the # of complex numbers on a particular quadrant
2. getting the output to be the in the correct order...example quadrant 1, quadrant 2, etc
3. getting the output sentences to have the correct grammar...example more than 1 complex number means I need "There are 2 complex numbers on the .... while when theres only 1 number I need "There is 1 complex number...

Here's my source code: ANY HELP WOULD BE GREATLY APPRECIATED

Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.lang.*;
import java.util.*;
import java.text.NumberFormat;

public class ComplexCatalog {

//data is read in from file...data contained in file is as follows:
// 7
// 1 + 2 i
// 4 + -3.2 i
// 12.3 + 0 i
// 0 + -1.9 i
// -1 + -1 i
// -1 + 2.3 i
//4 + 4 i

public static void main(String[] args) throws FileNotFoundException {

{
//
// Here Scanner is used to read the file content line-by-line.
//

Scanner s = new Scanner(new File("complex.txt"));

int complexCount;
complexCount=s.nextInt();

for (int index = 0; index < complexCount; index++)
{
double realValue = s.nextDouble();
String plusSign = s.next();
double imaginaryValue = s.nextDouble();
String imaginarySign = s.next();

String complexNumber = (realValue+plusSign+imaginaryValue+imaginarySign);

int count = 0;
String axis = null;

System.out.print("There are " + ""+  "complex numbers in quadrant number " );
if (realValue > 0 && imaginaryValue > 0)
{
System.out.println(1+".");
}
else if (realValue < 0 && imaginaryValue > 0)
{
System.out.println(2+".");
}
else if (realValue < 0 && imaginaryValue < 0)
{
System.out.println(3+".");
}
else if (realValue > 0 && imaginaryValue < 0)
{
System.out.println(4+".");
}
else if (imaginaryValue == 0 && realValue != 0)
{
System.out.println("r-axis.");
}
else if(realValue == 0 && imaginaryValue != 0)
{
System.out.println("i-axis.");
}
{
System.out.println(complexNumber);

}{

System.out.println();

}
}
}
}
}

Edited by Fubarable: code tags added.
• 11-09-2009, 03:31 AM
Fubarable
The best solution would be to create and use a ComplexNumber class, and create four ArrayLists to hold objects of this class, one for each quadrant. But since this is basic intro to Java (my guess), you may have to do a work around.

One way is to have four Strings (or an array of String[4]) and four int variables, the Strings to hold the complex number Strings that you output, one for each quadrant, and the ints to hold the counts of complex numbers found in each quadrant. When a complex number is found, you could find out the quadrant it belonged to, append to the proper quadrant String a tab "\t", the complex number String representation, and a newline char "\n" (actually StringBuilder object would be theoretically better than a String, but in this small program, it doesn't really matter), and then increment that quadrant's int counter var.

Once you've done this, it should be relatively easy to figure out how to output it.

Much luck and HTH.
• 11-09-2009, 04:15 AM
A5i19
any chance you might be able to show me a small example I have been doing this for far too long and cannot seem to get anything to work...
• 11-09-2009, 04:27 AM
Fubarable
Quote:

Originally Posted by A5i19
any chance you might be able to show me a small example I have been doing this for far too long and cannot seem to get anything to work...

Creating the code I feel is your responsibility, but I will be happy to nudge you a bit. Here is some semi-pseudo code. Please ask if you don't understand anything.

Code:

// string[0] and int[0] will hold quadrant 1, [1] will hold quad 2, etc...

for each complex number in file
find quadrant number, subtract 1 /* see comment above */ and place in quadIndex
quadComplexNumberStrings[quadIndex] += tab + string representation of complex number + new line;
end for

for int i = 0 to i < 4 // for each quadrant
String descriptionString = "";
if quadComplexCount[i] is 0 then descriptionString = "there are no complex numbers in quadrant ";
else if ... is 1 then descriptionString = "there is 1 complex.... in quadrant ";
else descriptionString = "There are " + quadComplexCount[i] + " complex.... in quadrant"
println descriptionString

end for

• 11-09-2009, 04:47 AM
A5i19
does this code remove my if else statements or do I leave the if else statements and put this new code under a new class under the if else statements
• 11-09-2009, 04:51 AM
Fubarable
This is not code but pseudo-code, and as such it replaces nothing but instead tries to explain a possible algorithm or logic. You should be able to read it and follow the logic, do a little thinking on your own, and see if it will be useful or not and if so, how to use it.
• 11-09-2009, 05:58 AM
A5i19
hey if you see this...i know its not what you put up but would this work?

int count1 = 0;

if (realValue > 0 && imaginaryValue > 0)
{
count1++;

System.out.println("There are " + count1 + " complex numbers in quadrant " + quadrant 1.);
System.out.println(complexNumber);

etc.....

My output is now:

There are 1 complex numbers in quadrant 1.
1.0+2.0i
There are 1 complex numbers in quadrant 4.
4.0+-3.2i
There are 1 complex numbers on the r-axis.
12.3+0.0i
There are 1 complex numbers on the i-axis.
0.0+-1.9i
There are 1 complex numbers in quadrant 3.
-1.0+-1.0i
There are 1 complex numbers in quadrant 2
-1.0+2.3i
There are 1 complex numbers in quadrant 1.
4.0+4.0i