Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-09-2009, 03:07 AM
Member
 
Join Date: Nov 2009
Posts: 7
Rep Power: 0
A5i19 is on a distinguished road
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

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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-09-2009, 03:31 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,326
Rep Power: 8
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-09-2009, 04:15 AM
Member
 
Join Date: Nov 2009
Posts: 7
Rep Power: 0
A5i19 is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-09-2009, 04:27 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,326
Rep Power: 8
Fubarable is on a distinguished road
Default
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.

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
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-09-2009, 04:47 AM
Member
 
Join Date: Nov 2009
Posts: 7
Rep Power: 0
A5i19 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-09-2009, 04:51 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,326
Rep Power: 8
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-09-2009, 05:58 AM
Member
 
Join Date: Nov 2009
Posts: 7
Rep Power: 0
A5i19 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 05:28 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org