Results 1 to 7 of 7
 11092009, 03:07 AM #1Member
 Join Date
 Nov 2009
 Posts
 7
 Rep Power
 0
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 raxis.
12.3+0.0i
There are complex numbers in quadrant number iaxis.
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 raxis.
tabbed over> 12.3 + 0i
There is 1 complex number on the iaxis.
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
Java 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 linebyline. // 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; int quadrantNumber = 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("raxis."); } else if(realValue == 0 && imaginaryValue != 0) { System.out.println("iaxis."); } { System.out.println(complexNumber); }{ System.out.println(); } } } } }
Last edited by Fubarable; 11092009 at 03:11 AM.

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.
 11092009, 04:15 AM #3Member
 Join Date
 Nov 2009
 Posts
 7
 Rep Power
 0
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 semipseudo code. Please ask if you don't understand anything.
Java Code:// string[0] and int[0] will hold quadrant 1, [1] will hold quad 2, etc... String[] quadComplexNumberStrings = new String[4]; int[] quadComplexCount = new int[4]; for each complex number in file read in complex number find quadrant number, subtract 1 /* see comment above */ and place in quadIndex quadComplexNumberStrings[quadIndex] += tab + string representation of complex number + new line; quadComplexCount[quadIndex] += 1; // or quadComplexCount[quadIndex]++; 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" descriptionString += (i + 1); // add the quadrant number println descriptionString println quadComplexNumberStrings[i] end for
 11092009, 04:47 AM #5Member
 Join Date
 Nov 2009
 Posts
 7
 Rep Power
 0
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

This is not code but pseudocode, 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.
 11092009, 05:58 AM #7Member
 Join Date
 Nov 2009
 Posts
 7
 Rep Power
 0
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 raxis.
12.3+0.0i
There are 1 complex numbers on the iaxis.
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
Similar Threads

formatting..
By sireesha in forum New To JavaReplies: 16Last Post: 06262009, 08:11 PM 
Formatting a toString
By MooNinja in forum New To JavaReplies: 8Last Post: 03312009, 08:32 PM 
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02262009, 12:44 AM 
formatting String
By bugger in forum New To JavaReplies: 1Last Post: 11162007, 08:27 PM 
Formatting the date
By yuchuang in forum New To JavaReplies: 5Last Post: 05072007, 07:08 PM
Bookmarks