Supermarket Array program help!

class Supermarket {
    double[] profit;
    String[] city;
    String[] aboveAverage;
    double average,deviation;
    double list[], lenght;
    Supermarket(String[] thisCity, double[] thisProfit)
   city = thisCity;
   profit = thisProfit;
    @Override
public String toString(){
        return "City" + '\t' + "Profit" + '\n' + city + '\t' + profit + 'n';
    public double getSum()
        double sum = 0;
        for(int i = 0; i < profit.length; i++)
            sum = sum + profit;
return sum;
public double average()
return getSum()/profit.length;
public String aboveAverage()
String s = "";
double avg = average();
for(int i = 0; i < profit.length; i++)
if(profit[i] > avg)
s = city[i] + "" + profit[i] + '\n';
return s;
public double getDeviation()
double sumdifference = 0;
double mean = average();
for(int i = 0; i < profit.length; i++)
sumdifference = sumdifference + Math.pow((profit[i]- 1), mean);
return Math.sqrt(sumdifference/profit.length);
public double findhighValue()
double highestValue = profit[0];
for(int i = 1; i < profit.length; i++)
if(profit[i] > highestValue )
highestValue = profit[i];
return highestValue;
public double findlowestValue()
double lowestValue = profit[0];
for(int i = 1; i > profit.length; i++)
if(profit[i] > lowestValue)
lowestValue = profit[i];
return lowestValue;
public String barGraph()
String s = "";
for(int i = 0; i < profit.length; i++)
s = s + city[i] + "";
int x = (int)Math.floor(profit[i]);
for(int j = 1; j <= i; j++)
s = s + "*";
s = s + '\n';
return s;
public int findPosition(int startfrom)
int position = startfrom;
for(int i = startfrom + 1; 1 < profit.length; i++)
if(profit[i] < profit[position])
position = i;
return position;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
class TestSupermarket
public static void main(String arg[])
NumberFormat nf = NumberFormat.getInstance();
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern("0.00");
Locale local = Locale.US;
NumberFormat cf = NumberFormat.getCurrencyInstance(local);
double profit[] = {10200000, 14600000, 17000000, 6700000, 3600000, 9100000};
String city[] = {"Miami", "Sunrise", "Hollywood", "Tallahassee", "Jacksonville", "Orlando"};
System.out.println("Millions in revenue " + profit + city);
Supermarket n = new Supermarket(city, profit);
System.out.println("Average of profits " + cf.format(n.getSum()));
System.out.println("\nThe average " + cf.format(n.average()));
System.out.println("\nthe highest value " + cf.format(n.findhighValue()));
System.out.println("\nthe lowest value " + cf.format(n.findlowestValue()));
System.out.println("\nbargraph\t " + n.barGraph());
System.out.println("\ndeviation " + n.getDeviation());
System.out.println("\nposition " + n.aboveAverage());
I'm still having some issues. 
1st - Deviation calculation seems wrong, as it produces infinite.
2nd -?     A horizontal bar graph showing the performance for each city, to the nearest million dollars.  Not sure how to produce that.
3rd - ?     List in descending order of profit, the cities and their respective profit.
I'm still working on this situation, but help is appreciated if you can set me to the right path.  Producing the array bar is the problem I'm mainly having.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

I offered to help him stated at my last post in his topic. Different people, not sure how to prove we're different people other than IP check. He didn't need to produce "origin"city. Just the two arrays of Profit and city. I went ahead and develop a test class to test out the input.

Similar Messages

  • Java 2D array program help please?

    Hi - I'm teaching computer science to myself (java, using jGRASP) using some websites online, and one of the activities I have to do is to make an "image smoother" program.
    I just started learning about 2D array, and I think I get what it is (based on what I know of 1D arrays), but I have no clue how to do this activity.
    This is what the description says:
    "A grey-level image is sometimes stores as a list of int values. The values represent the intensity of light as discrete positions in the image.
    An image may be smoothed by replacing each element with the average of the element's neighboring elements.
    Say that the original values are in the 2D array "image". Compute the smoothed array by doing this: each value 'smooth[r][c]' is the average of nine values:
    image [r-1][c-1], image [r-1][c ], image [r-1][c+1],
    image [r ][c-1], image [r ][c ], image [r ][c+1],
    image [r+1][c-1], image [r+1][c ], image [r+1][c+1].
    Assume that the image is recangular, that is, all rows have the same number of locations. Use the interger arithmetic for this so that the values in 'smooth' are integers".
    and this is what the website gives me along with the description above:
    import java.io.*;
    class Smooth
    public static void main (String [] args) throws IOException
    int [][] image = {{0,0,0,0,0,0,0,0,0,0,0,0,},
    {0,0,0,0,0,0,0,0,0,0,0,0},
    {0,0,5,5,5,5,5,5,5,5,0,0},
    {0,0,5,5,5,5,5,5,5,5,0,0},
    {0,0,5,5,5,5,5,5,5,5,0,0},
    {0,0,5,5,5,5,5,5,5,5,0,0},
    {0,0,5,5,5,5,5,5,5,5,0,0},
    {0,0,5,5,5,5,5,5,5,5,0,0},
    {0,0,5,5,5,5,5,5,5,5,0,0},
    {0,0,5,5,5,5,5,5,5,5,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0}};
    //assume a rectangular image
    int[][] smooth = new int [image.length][image[0].length];
    //compute the smoothed value for
    //non-edge locations in the image
    for (int row=1; row<image.length-1; row++)
    for (int col=1; col<image[row].length-1; col++)
    smooth[row][col] = sum/9;
    //write out the input
    //write out the result
    "The edges of the image are a problem because only some of the nine values that go into the average exist. There are various ways to deal with this problem:
    1. Easy (shown above): Leave all the edge locations in the smoothed image to zero. Only inside locations get an averaged value from the image.
    2. Harder: Copy values at edge locations directly to the smoothed image without change.
    3. Hard: For each location in the image, average together only those of the nine values that exist. This calls for some fairy tricky if statements, or a tricky set of for statements inside the outer two.
    Here is a sample run of the hard solution:
    c:\>java ImageSmooth
    input:
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 5 5 5 5 5 5 5 5 0 0
    0 0 5 5 5 5 5 5 5 5 0 0
    0 0 5 5 5 5 5 5 5 5 0 0
    0 0 5 5 5 5 5 5 5 5 0 0
    0 0 5 5 5 5 5 5 5 5 0 0
    0 0 5 5 5 5 5 5 5 5 0 0
    0 0 5 5 5 5 5 5 5 5 0 0
    0 0 5 5 5 5 5 5 5 5 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    output:
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 1 1 1 1 1 1 1 1 0 0
    0 1 2 3 3 3 3 3 3 2 1 0
    0 1 3 5 5 5 5 5 5 3 1 0
    0 1 3 5 5 5 5 5 5 3 1 0
    0 1 3 5 5 5 5 5 5 3 1 0
    0 1 3 5 5 5 5 5 5 3 1 0
    0 1 3 5 5 5 5 5 5 3 1 0
    0 1 3 5 5 5 5 5 5 3 1 0
    0 1 2 3 3 3 3 3 3 2 1 0
    0 0 1 1 1 1 1 1 1 1 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    c:\>"
    OKAY that was very long, but I'm a beginner to java, especially to 2D arrays. If anyone could help me with this step-by-step, I would greatly appreciate it! Thank you very much in advance!

    larissa. wrote:
    I did try, but I have no clue where to start from. That's why I asked if someone could help me with this step by step...regardless, our track record for helping folks who state they are "completely lost" or "don't have a clue" is poor to dismal as there is only so much a forum can do in this situation. We can't put knowledge in your head; only you can do that. Often the best we can do is to point you to the most basic tutorials online and suggest that you start reading.
    That being said, perhaps we can help you if you have some basic knowledge here, enough to understand what we are suggesting, and perseverance. The first thing you need to do is to try to create this on your own as best you can, then come back with your code and with specific questions (not the "I'm completely lost" kind) that are answerable in the limited format of a Java forum. By posting your code, we can have a better idea of just where you're making bad assumptions or what Java subjects you need to read up on in the tutorials.

  • Simple array program help

    Hello could I please have some hints as to how to get this program working e.g. would it better to use a while loop for this? and how would I make the program stop when it has read 100 names?
    cheers anyone
    import java.util.*;
    import java.io.*;
    * The problem with files is generally you don`t know how many values
    * are in there. Develop a program to read names from a file - it carries
    * on and reads names until either 100 is reached or there is no more
    * data in the file. Display all the names read in together with the
    * count of how many names have been read.
    class Names100
         public void process (String[] argStrings)throws Exception
              int namesRead = 0;
              Scanner sc = new Scanner(new File("names.txt"));
              String[] namesArray = new String[100];
              for(int i = 0; i <= 100; i++)
                   namesArray[i] = sc.next();
                   System.out.println(namesArray);
                   namesRead++;
              System.out.println();
              System.out.println("Names read: " + namesRead);
              sc.close();

    > Thanks, the way that I have been taught at UNI means
    that I have a separate file called names100App (this
    is used to run the program).
    Ok, I understand.
    > The scanner is to get
    the content of the file although I could have used
    other things I guess.
    Yes, but the Scanner class is mighty handy.
    > Also I will check out what you said.
    >
    cheers
    John
    Good, and while you're at it, take a look at the ArrayList class (resizable-array implementation):
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
    Good luck.

  • When i open itunes its normal and then out of nowhere it says "itunes has stopped working" and i have to click 'close program'. help?

    when i open itunes its normal and then out of nowhere it says "itunes has stopped working" and i have to click 'close program'. help?

    Having the same issue here....a quick fix is to just minimize the window, and immediately maximize it.  It will now fit the screen.  Trouble is, after you close it, you'll have to do it again when you re-start itunes.  The good news is it takes all of about 2 seconds.  Apple should fix this.  Should.

  • Programming help - how to get the read-only state of PDF file is windows explorer preview is ON?

    Programming help - how to get the read-only state of PDF file is windows explorer preview is ON?
    I'm developing an application where a file is need to be saved as pdf. But, if there is already a pdf file with same name in the specified directory, I wish to overwrite it. And in the overwrite case, read-only files should not be overwritten. If the duplicate(old) file is opened in windows (Win7) explorer preview, it is write protected. So, it should not be overwritten. I tried to get the '
    FILE_ATTRIBUTE_READONLY' using MS API 'GetFileAttributes', but it didn't succeed. How Adobe marks the file as read-only for preview? Do I need to check some other attribute of the file?
    Thanks!

    Divya - I have done it in the past following these documents. Please read it and try it it will work.
    Please read it in the following order since both are a continuation documents for the same purpose (it also contains how to change colors of row dynamically but I didnt do that part I just did the read_only part as your requirement) 
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/f0625002-596c-2b10-46af-91cb31b71393
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/d0155eb5-b6ce-2b10-3195-d9704982d69b?quicklink=index&overridelayout=true
    thanks!
    Jason PV

  • Freely Programmed help in Pop Up Window

    Hi,
      Is it possible to display the data of freely programmed help in the pop up window.
    Thanks
    Raghavendra

    Hi Thomas,
       I agree that by using Freely Programmed help the data will be displayed in a seperate window. I want to display the data in a window( of type Pop Up) because i am displaying table control with 3 fields on clicking f4 and it has 2 buttons Ok and Cancel. But if there are 50 entries the user needs to scroll down to click on Ok. If the window is of type pop up Ok button will be embedded in the window instead of on view.
    Thanks
    Raghavendra

  • Freely programmed help in ALV

       HI:ALL
          How to achieve  Freely programmed help in ALV?

    Hi,
    On APPLY buttton action, write the below code
    *Fire the event to indicate that data selection is done
    wd_comp_controller->fire_vh_data_selected_evt( ).
    wd_comp_controller->value_help_listener->close_window( ).
    *Raise Event
       cl_fpm_factory=>get_instance( )->raise_event_by_id( EXPORTING   iv_event_id   = '<custom event name>').
    And in PROCESS_EVENT of your webdynpro component(where Freely Programmed search help is implemented) write the code as per your requirement to set the values back to the table.
    CASE IO_EVENT->MV_EVENT_ID.
              WHEN '<CUSTOM EVENT NAME>'.
                        write code as per your requirement.
    ENDCASE.
    Thanks
    KH

  • N95 Maps Program Help and Suggestions Anybody??

    N95 Maps Program Help and Suggestions Anybody??
    havnt updated to firmware v12 yet
    Why can't I search my Address or Current Location and then save this as HOME or say my Start_Point_01?
    I have managed to save my HOME as a PEOPLE LOCATION and my friends address etc and worked out
    how to plan a route etc, BUT Maps GPS POSITION thinks I live 20 miles away??
    I dont seem to be able to simply set my HOME or Current Location myself,
    I have seen some Satelite activity but insists I live 20 miles away :/ how do I fix this,
    it's realy annoying. any handy tips people??
    cant upgrade firmware as need phone plugged into wall socket
    at webcafe, will have to wait for firmware :/
    N95-1 (8GB-MicroSD),LCG-Audio,Fring,Nimbuzz,Skype,Youtube,iPlayer,Garmin4-GPS.Googlemaps,SkyFire,ZoneTag,Gravity, Sennheiser CX-400-IIs,500-IIs,TR-120 Wireless,HD215'S.AudioTechnica ATX-M50's.BT B-Tube BT Speaker.

    The least problems you'll have with miniDV cameras. About any miniDV camera will connect to a Mac via Firewire. HDD cameras are an open invitation to trouble, especially as you speak of older computers.
    There are well-featured models out there now for 2-300 bucks. But watch out for cameras that connect to the computer via a dock. I had two Sony with these @#$% docking stations .... both docks broke and rendered the cameras useless for capturing. replacing the dock was nearly the price of a new cam.

  • What Is Wrong With My Program Help

    This Is The Assignment I Have To Do:
    Write a Java program that prints a table with a list of at least 5 students together with their grades earned (lab points, bonus points, and the total) in the format below.
    == Student Points ==
    Name Lab Bonus Total
    Joe 43 7 50
    William 50 8 58
    Mary Sue 39 10 49
    The requirements for the program are as follows:
    1.     Name the project StudentGrades.
    2.     Print the border on the top as illustrated (using the slash and backslash characters).
    3.     Use tab characters to get your columns aligned and you must use the + operator both for addition and string concatenation.
    4.     Make up your own student names and points -- the ones shown are just for illustration purposes. You need 5 names.
    This Is What I Have So Far:
    * Darron Jones
    * 4th Period
    * ID 2497430
    *I recieved help from the internet and the book
    *StudentGrades
      public class StudentGrades
         public static void main (String[] args )
    System.out.println("///////////////////\\\\\\\\\\\\\\\\\\");
    System.out.println("==          Student Points          ==");
    System.out.println("\\\\\\\\\\\\\\\\\\\///////////////////");
    System.out.println("                                      ");
    System.out.println("Name            Lab     Bonus   Total");
    System.out.println("----            ---     -----   -----");
    System.out.println("Dill             43      7       50");
    System.out.println("Josh             50      8       58");
    System.out.println("Rebbeca          39      10      49");When I Compile It Keeps Having An Error Thats Saying: "Illegal Escape Character" Whats Wrong With The Program Help Please

    This will work exactly as you want it to be....hope u like it
         public static void main(String[] args)
              // TODO Auto-generated method stub
              System.out.print("///////////////////");
              System.out.print("\\\\\\\\\\\\\\\\\\\\\\\\");
              System.out.println("\\\\\\\\\\\\\\");
              System.out.println("== Student Points ==");
              System.out.print("\\\\\\\\\\\\\\");
              System.out.print("\\\\\\\\\\\\");
              System.out.print("\\\\\\\\\\\\");
              System.out.print("///////////////////");
              System.out.println(" ");
              System.out.println("Name Lab Bonus Total");
              System.out.println("---- --- ----- -----");
              System.out.println("Dill 43 7 50");
              System.out.println("Josh 50 8 58");
              System.out.println("Rebbeca 39 10 49");
         }

  • Freely Programed Help for select-option field

    Hi,
    how can i set freely programmed help for select option field, i mean while adding selection field what are the parameters that are important for freely programmed help.
    i have implemented iwd_value_help in one component comp1 and declared the usage of comp1 in comp2 where i actually defined the usage of select-option component.
    i used parameter   i_value_help_type = if_wd_value_help_handler=>co_prefix_appldev while adding selection field, however when i presss F4 icon, the following message is coming
    View WD_VALUE_HELP does not exist within the component WDR_SELECT_OPTIONS
    Please suggest where i am doing wrong??
    Edited by: kranthi kumar on Dec 29, 2010 6:19 PM

    >
    kranthi kumar wrote:
    > Hi,
    >
    > how can i set freely programmed help for select option field, i mean while adding selection field what are the parameters that are important for freely programmed help.
    >
    > i have implemented iwd_value_help in one component comp1 and declared the usage of comp1 in comp2 where i actually defined the usage of select-option component.
    >
    > i used parameter   i_value_help_type = if_wd_value_help_handler=>co_prefix_appldev while adding selection field, however when i presss F4 icon, the following message is coming
    >
    > View WD_VALUE_HELP does not exist within the component WDR_SELECT_OPTIONS
    >
    > Please suggest where i am doing wrong??
    >
    > Edited by: kranthi kumar on Dec 29, 2010 6:19 PM
    Hi Kranthi,
    Please help me to understand your design.
    Why would you like to create a Freely programmed value help for select-option?. why not use wdr_select_option directly ?

  • Why i cant open the ae after i download ~then pop up something like ae error, crash in program~help~pls

    why i cant open the ae after i download ~then pop up something like ae error, crash in program~help~pls

  • Help with array program!!

    hi friends
    i am a new comer to java and I have been studying Arrays recently. I came across this program on the internet ( thanks to Peter Williams)
    package DataStructures;
    import java.util.NoSuchElementException;
    * An array implementation of a stack
    * @author Peter Williams
    public class StackArray implements Stack {
    private int top; // for storing next item
    public StackArray() {
    stack = new Object[1];
    top = 0;
    public boolean isEmpty() {
    return top == 0;
    public void push(Object item) {
    if (top == stack.length) {
    // expand the stack
    Object[] newStack = new Object[2*stack.length];
    System.arraycopy(stack, 0, newStack, 0, stack.length);
    stack = newStack;
    stack[top++] = item;
    public Object pop() {
    if (top == 0) {
    throw new NoSuchElementException();
    } else {
    return stack[--top];
    interface Stack {
    * Indicates the status of the stack.
    * @return <code>true</code> if the stack is empty.
    public boolean isEmpty();
    * Pushes an item onto the stack.
    * @param <code>item</code> the Object to be pushed.
    public void push(Object item);
    * Pops an item off the stack.
    * @return the item most recently pushed onto the stack
    * @exception NoSuchElementException if the stack is empty.
    public Object pop();
    class StackDemo {
    public static void main(String[] args) {
         Stack s = new StackArray();
         // Stack s = new StackList();
         for (int i = 0; i < 8; i++) {
         s.push(new Integer(i));
         while (!s.isEmpty ()) {
         System.out.println(s.pop());
    what baffles me is as below:
    there is an 'if' construct in the push method, which talks about if(top == stack.length)
    I fail to understand how top can at any time be equal to stack.length.
    Could you help me understand this program?
    a thousand apologies and thanks in advance

    figurativelly speaking:
    if you take an array and put it standing on your tesk, so that start of array points to floor, and end of array points to roof, then you can start filling that array as stack.
    if you put books into stack, then you push (put) them on top of the stack, so the first book ever but into stack is adiacent to array element with index zero (0) and then second book in stack would be adiacent to array element that has index one (1) and so on.
    if you have array of size 5, then fift book in stack would be adiacent to array element with index four (4)
    after pushing that Object to stack, the top variable will get incremented (top++ in code) and be equal to size of array.
    however, if you would like to push another Object to stack, then it would fall over the end of the array, so longer array is needed...

  • Need Help with simple array program!

    Hi, I have just recently started how to use arrays[] in Java and I'm a bit confused and need help designing a program.
    What this program does, it reads in a range of letters specified by the user. The user then enters the letters (a, b or c) and stores these characters into an array, which the array's length is equal to the input range the user would enter at the start of the program. The program is then meant to find how many times (a,b and c) appears in the array and the Index it first appears at, then prints these results.
    Here is my Code for the program, hopefully this would make sense of what my program is suppose to do.
    import B102.*;
    class Letters
         static int GetSize()
              int size = 0;
              boolean err = true;
              while(err == true)
                   Screen.out.println("How Many Letters would you like to read in?");
                   size = Keybd.in.readInt();
                   err = Keybd.in.fail();
                   Keybd.in.clearError();
                   if(size <= 0)
                        err = true;
                        Screen.out.println("Invalid Input");
              return(size);
         static char[] ReadInput(int size)
              char input;
              char[] letter = new char[size];
              for(int start = 1; start <= size; start++)
                   System.out.println("Please enter a letter (a, b or c) ("+size+" max), enter # to stop:");
                   input = Keybd.in.readChar();
                   while((input != 'a') && (input != 'b') && (input != 'c') && (input != '#'))
                        Screen.out.println("Invalid Input");
                        System.out.println("Please enter a letter (a, b or c) ("+size+" max, enter # to stop:");
                        input = Keybd.in.readChar();
                                    while(input == '#')
                                                 start == size;
                                                 break;
                   for(int i = 0; i < letter.length; i++)
                        letter[i] = input;
              return(letter);
         static int CountA(char[] letter)
              int acount = 0;
              for(int i = 0; i < letter.length; i++)
                   if(letter[i] == 'a')
                        acount++;
              return(acount);
         static int CountB(char[] letter)
              int bcount = 0;
              for(int i = 0; i < letter.length; i++)
                   if(letter[i] == 'b')
                        bcount++;
              return(bcount);
         static int CountC(char[] letter)
              int ccount = 0;
              for(int i = 0; i < letter.length; i++)
                   if(letter[i] == 'c')
                        ccount++;
              return(ccount);
         static int SearchA(char[] letter)
              int ia;
              for(ia = 0; ia < letter.length; ia++)
                   if(letter[ia] == 'a')
                        return(ia);
              return(ia);
         static int SearchB(char[] letter)
              int ib;
              for(ib = 0; ib < letter.length; ib++)
                   if(letter[ib] == 'b')
                        return(ib);
              return(ib);
         static int SearchC(char[] letter)
              int ic;
              for(ic = 0; ic < letter.length; ic++)
                   if(letter[ic] == 'c')
                        return(ic);
              return(ic);
         static void PrintResult(char[] letter, int acount, int bcount, int ccount, int ia, int ib, int ic)
              if(ia <= 1)
                   System.out.println("There are "+acount+" a's found, first appearing at index "+ia);
              else
                   System.out.println("There are no a's found");
              if(ib <= 1)
                   System.out.println("There are "+bcount+" b's found, first appearing at index "+ib);
              else
                   System.out.println("There are no b's found");
              if(ic <= 1)
                   System.out.println("There are "+ccount+" c's found, first appearing at index "+ic);
              else
                   System.out.println("There are no c's found");
              return;
         public static void main(String args[])
              int size;
              char[] letter;
              int acount;
              int bcount;
              int ccount;
              int ia;
              int ib;
              int ic;
              size = GetSize();
              letter = ReadInput(size);
              acount = CountA(letter);
              bcount = CountB(letter);
              ccount = CountC(letter);
              ia = SearchA(letter);
              ib = SearchB(letter);
              ic = SearchC(letter);
              PrintResult(letter, acount, bcount, ccount, ia, ib, ic);
              return;
    }     Some errors i get with my program are:
    When reading in the letters to store into the array, I get the last letter I entered placed into the entire array. Also I believe my code to find the Index is incorrect.
    Example Testing: How many letters would you like to read? 3
    Enter letter (a, b or c) (# to quit): a
    Enter letter (a, b or c) (# to quit): b
    Enter letter (a, b or c) (# to quit): c
    It prints "There are no a's'" (there should be 1 a at index 0)
    "There are no b's" (there should be 1 b at index 1)
    and "There are 3 c's, first appearing at index 0" ( there should be 1 c at index 2)
    The last thing is that my code for when the user enters "#" that the input of letters would stop and the program would then continue over to the counting and searching part for the letters, my I believe is correct but I get the same problem as stated above where the program takes the character "#" and stores it into the entire array.
    Example Testing:How many letters would you like to read? 3
    Enter letter (a, b or c) (# to quit): a
    Enter letter (a, b or c) (# to quit): #
    It prints "There are no a's'" (should have been 1 a at index 0)
    "There are no b's"
    and "There are no c's"
    Can someone please help me??? or does anyone have a program simular to this they have done and would'nt mind showing me how it works?
    Thanks
    lou87.

    Without thinking too much...something like this
    for(int start = 0; start < size; start++) {
                System.out.println("Please enter a letter (a, b or c) ("+size+" max), enter # to stop:");
                input = Keybd.in.readChar();
                while((input != 'a') && (input != 'b') && (input != 'c') && (input != '#')) {
                    Screen.out.println("Invalid Input");
                    System.out.println("Please enter a letter (a, b or c) ("+size+" max, enter # to stop:");
                    input = Keybd.in.readChar();
                if(input == '#') {
                        break;
                letter[start] = input;
            }you dont even need to do start = size; coz with the break you go out of the for loop.

  • Help with simple array program

    my program compiles and runs but when after i enter in 0 it says that the total cost is 0.0 any help would be great thanks
    /**Write a program that asks the user for the price and quantity of up to 10 items. The program accepts input until the user enters a price of 0. 
    *The program applies a 7% sales tax and prints the total cost.
    Sample
    Enter price 1:
    1.95
    Enter quantity 1:
    2
    Enter price 2:
    5.00
    Enter quantity 2:
    1
    Enter price 3:
    0
    Your total with tax is 9.52
    import java.util.*;
    public class Cashregister
    static Scanner input = new Scanner(System.in);
    static double userinput = 1;
    public static void main(String[] args)
      int anykey;
      int howmanyproducts;
      System.out.println("This Program will allow you to enter the price and quanity of a set of items until you press zero, and after you press zero it will put the tax on it and give you the subtotal.");
      int whichproduct = 1;//used to say what
      int whichquanity = 1;
      double beforetax = 0;
      double aftertax = 0;
      double [] finalproduct = new double [9];
      double [] finalquant = new double[9];
      double [] finalanswer = new double[9];
      double taxrate = .07;
      int index = 0;
      while(userinput !=0)
       System.out.println("Enter price " + whichproduct + ":");
       userinput = input.nextDouble();
       whichproduct ++;
       if(userinput != 0)
       finalproduct[index] = userinput;
       System.out.println("Enter quanity " + whichquanity + ":");
       userinput = input.nextInt();
       finalquant[index] = userinput;
       whichquanity ++;
       index ++;
      else
      int [] indecies = new int [index];//used for array.length purposes
       for(int j = 0; j<indecies.length; j++)
         finalanswer[index] = finalproduct[index] * finalquant[index];
      for(int k = 0; k < indecies.length; k++)
         finalanswer[k] = finalanswer[k] + finalanswer[k];
         beforetax = finalanswer[k];
         aftertax = beforetax * taxrate;
      System.out.println("The total cost with tax will be $" + aftertax);
    }

    I tried ot indent better so it is more readable and i removed the tax out of my loop along with another thing i knew wasnt supposed to be their. Ran it again and i still got the same total coast = 0.0
    import java.util.*;
    public class Cashregister
    static Scanner input = new Scanner(System.in);
    static double userinput = 1;
    public static void main(String[] args)
      int anykey;
      int howmanyproducts;
      System.out.println("This Program will allow you to enter the price and quanity of a set of items until you press zero, and after you press zero it will put the tax on it and give you the subtotal.");
      int whichproduct = 1;
      int whichquanity = 1;
      double beforetax = 0;
      double aftertax = 0;
      double [] finalproduct = new double [9];
      double [] finalquant = new double[9];
      double [] finalanswer = new double[9];
      double taxrate = .07;
      int index = 0;
      while(userinput !=0)
       System.out.println("Enter price " + whichproduct + ":");
       userinput = input.nextDouble();
       whichproduct ++;
            if(userinput != 0)
                    finalproduct[index] = userinput;
                    System.out.println("Enter quanity " + whichquanity + ":");
                    userinput = input.nextInt();
                    finalquant[index] = userinput;
                    whichquanity ++;
                    index ++;
            else
                    int [] indecies = new int [index];
                    for(int j = 0; j<indecies.length; j++)
                            finalanswer[index] = finalproduct[index] * finalquant[index];
                    for(int k = 0; k < indecies.length; k++)
                           finalanswer[0] = finalanswer[k] + finalanswer[k];
                    beforetax = finalanswer[0];
                    aftertax = beforetax * taxrate;
                    System.out.println("The total cost with tax will be $" + aftertax);
    }

  • Help with an array program

    I can't see how to calculate the lowest hours, highest hours, and average hours worked.
    These three values should be outputted each on a seperate line.
    I just can't figure this out.
    Any help please.
    This program will show the employees
    hours. It will have one
    class.It will use an array to keep
    track of the number of hours the employee
    has worked with the output to the monitor
    of the employees highest, lowest, and the
    average of all hours entered. */
    import java.util.*;
    import java.text.DecimalFormat;
    /*** Import Decimal Formating hours to format hours average. ***/
    public class cs219Arrays
         public static void main(String[] args)
              Scanner scannerObject = new Scanner(System.in);
              int[] employees ={20, 35, 40};
              int employeesHighest = 40;
              int employeesLowest = 20;
              double employeesAverage = 35;
              int[] firstEmployee = new int[1];
              int[] secondEmployee = new int[2];
              int[] thirdEmployee = new int[3];
              System.out.println("This program will accept hours worked for 3 employees.");
              System.out.println("Enter hours worked for employee #: ");
              System.out.println(".\n");
              employeesAverage = scannerObject.nextInt();
              for(int count =20; count <= 40; count++)
                             DecimalFormat df = new DecimalFormat("0.00");
                             System.out.println("pay: " employeesAverage " lowest " employeesHighest " Highest hours " employeesLowest " Lowest hours "+df.format(employeesAverage));
                             System.out.print("\n");
                             employeesAverage++;
    }/* end of body of first for loop */
              for(int count =20; count <= 40; count++)
                                       employeesAverage = employeesAverage + employeesHighest + employeesLowest;
                                       DecimalFormat df = new DecimalFormat("0.00");
                                       System.out.println("pay: $" employeesAverage " lowest " employeesHighest " Highest hours $" +df.format(employeesAverage));
                                       System.out.print("\n");
                                       employeesAverage++;
    }/* end of body of second for loop */
              for(int count =20; count <= 40; count++)
                                       employeesAverage = employeesAverage + employeesHighest + employeesLowest;
                                       DecimalFormat df = new DecimalFormat("0.00");
                                       System.out.println("pay: $" employeesAverage " lowest " employeesHighest " Highest hours $" +df.format(employeesAverage));
                                       System.out.print("\n");
                                       employeesAverage++;
    }/* end of body of third for loop */
              System.out.println("\n");
              DecimalFormat twoDigits = new DecimalFormat("0.00");
              /*** Create new DecimalFormat object named twoDigits ***/
              /*Displays number of hours, employees hours and average hours.*/
              System.out.println("You entered " + employeesAverage + " number of hours.");
              System.out.println("Your number of hours are " + twoDigits.format(employeesAverage));
              System.out.println("\n\n");
    }     /*End of method for data.*/
    {     /*Main method.*/
    }     /*End of main method.*/
    }     /*End of class cs219Arrays.*/

    Want help?
    Use the code formatting tags for starters. http://forum.java.sun.com/help.jspa?sec=formatting

Maybe you are looking for