Results 1 to 7 of 7
  1. #1
    A5i19 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default 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

    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 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;
               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("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.
    Last edited by Fubarable; 11-09-2009 at 03:11 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    A5i19 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by A5i19 View Post
    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.

    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

  5. #5
    A5i19 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    A5i19 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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

Similar Threads

  1. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 08:11 PM
  2. Formatting a toString
    By MooNinja in forum New To Java
    Replies: 8
    Last Post: 03-31-2009, 08:32 PM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  4. formatting String
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 08:27 PM
  5. Formatting the date
    By yuchuang in forum New To Java
    Replies: 5
    Last Post: 05-07-2007, 07:08 PM

Posting Permissions

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