0

For an assignment in my CS class I am having to take in an input of floats, save them in an array, then display them and add them together to get a sum of the floats. Currently I am having an issue with getting the sum of the floats.

As far as I can tell the below code should work, but I get an error: "can not add an object and an int".

My code is:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner; // load the scanner utility

class Lab7 {

    public static void main(String[] args) {

        double n;
        double s;
        Scanner input = new Scanner( System.in );  //define the scanner

        List L = new ArrayList();
        s=0.0;
        n=1.0;
        // read in the floats
        while ( n != 0.0 )
          {
             System.out.println("Please input a number");
             n = input.nextFloat();
             if ( n != 0.0) L.add(n);
             System.out.println("read in " + n);

          }

        for (int i=0; i< L.size(); i= i+1)
         {
           System.out.println("List contains " + L.get(i));
           s = s + L.get(i);
           System.outprintln("Sum of nunbers " + s); 
         }



     }// of main

   } // of Lab7
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • What do you mean you are having trouble with the sum? Where is the code you have tried so far to compute the sum? Or are you just expecting us to do this part for you? – nhouser9 Oct 03 '16 at 21:27
  • I'm voting to close this question as off-topic because questions involving homework must show some attempt to solve the question on their own. – nhouser9 Oct 03 '16 at 21:27
  • What part is stumping you specifically? It would help if you could review what you have tried. It doesn't appear as if you have code that actually attempts to sum all the items in your list. – Bennett Yeo Oct 03 '16 at 21:28
  • While I agree on other folks to vote down, I still like to answer it based on what I understood. Hey! @Mason - In order to sum the floats, you need to sum them by yourself. Please Loop through your arraylist (which you are already doing) and add the elements yourself instead of just printing them. Something like - double sum = 0.0d; for (int i=0; i< L.size(); i= i+1) { System.out.println("List contains " + L.get(i)); sum = sum + L.get(i); } System.out.println(sum); – Sanjeev Dhiman Oct 03 '16 at 21:31
  • My bad everyone I had previously uploaded outdated code, I reuploaded with my most current code which is where I am having issues. @nhouser9 – Mason Salcido Oct 03 '16 at 21:42
  • @SanjeevDhiman I have already tried that but then realized I can not add an Object and Double – Mason Salcido Oct 03 '16 at 21:50
  • Great you gave us the current code. That's a good first step. Now you need to tell us what the problem with it is. Give us a sample input and tell us what it outputs vs what you would expect it to output. – nhouser9 Oct 03 '16 at 21:52
  • Well when I try running my code I currently have I can not add an object and an int together, I am completely lost on how to get the values I enter in and then add them together @nhouser9 – Mason Salcido Oct 03 '16 at 22:12
  • @MasonSalcido Thanks for the update. I posted an answer below. If it helps please remember to upvote and accept it. – nhouser9 Oct 03 '16 at 22:29

2 Answers2

1
System.outprintln("Sum of nunbers " + sum);

should be

System.out.println("Sum of numbers " + sum);
clipovich
  • 45
  • 8
  • 2
    While this correctly points out a syntax error it is pretty clear that this is not the actual issue the OP asks about. Looks more like a typo when posting the question. – arkascha Mar 27 '23 at 14:47
  • You are correct. I pointed out the syntax error for others that may be reading this post. The error tripped me up when I tried running the code myself. My intent is to make things a little easier for the next person to come along and try the code. – clipovich Apr 02 '23 at 16:53
  • 1
    Then this should have been posted as a comment to the question, not as an "answer", since it does not answer anything. – arkascha Apr 02 '23 at 19:47
0

I have fixed up the code and put the new version below.

The main problem was the way you were using ArrayList. What you used was called a "raw type". See this link for info on why that's bad: What is a raw type and why shouldn't we use it?. Basicaly, you should be providing ArrayList with a type parameter, like: ArrayList<String>.

However, in this case the List was not even needed. We can simply keep a running total of the sum as each new number is entered. I changed the code to do that instead.

I also renamed your variables. You should never name things like n or x. Name variables something that describes what they are used for. That makes it way easier for you, or people you are asking for help (like people on StackOverflow) to read your code. Making your code easy to understand is almost as important as making it work correctly, especially as you start working on bigger projects.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner; // load the scanner utility

class Lab7 {

    public static void main(String[] args) {

        double currentNumber = 1.0;
        double sum = 0.0;
        Scanner input = new Scanner( System.in );  //define the scanner

        // read in the floats
        while ( currentNumber != 0.0 )
        {
            System.out.println("Please input a number");
            currentNumber = input.nextFloat();
            System.out.println("read in " + currentNumber);
            sum = sum + currentNumber;
        }
        System.outprintln("Sum of nunbers " + sum); 

    }// of main

} // of Lab7

If you absolutely have to use an array for this lab (keep in mind that it is not needed in any way, the only reason to do it is if the lab explicitly says you must do it), you can use the following:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner; // load the scanner utility

class Lab7 {

    public static void main(String[] args) {

        double currentNumber = 1.0;
        double sum = 0.0;
        Scanner input = new Scanner( System.in );  //define the scanner
        ArrayList<Float> floats = new ArrayList<>(); //notice how we give a type argument

        // read in the floats
        while ( currentNumber != 0.0 )
        {
            System.out.println("Please input a number");
            currentNumber = input.nextFloat();
            System.out.println("read in " + currentNumber);
            floats.add((float) currentNumber);
            sum = sum + currentNumber;
        }
        System.outprintln("Sum of nunbers " + sum); 

    }// of main

} // of Lab7
Community
  • 1
  • 1
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • The thing about that is I need to have the list in order to later find the MEAN of the set of number. – Mason Salcido Oct 03 '16 at 22:46
  • @MasonSalcido Seems like you might not have read my whole post... I say "If you absolutely have to use an array for this lab you can use the following" and then give a second example where the numbers are stored in an array. – nhouser9 Oct 03 '16 at 22:49
  • I did not see that, but even with that I was looking over it and realized arnt you not able to convert doubles in floats? – Mason Salcido Oct 03 '16 at 22:53
  • @MasonSalcido of course you can convert doubles to floats... have you even tried the code i wrote for you...? – nhouser9 Oct 04 '16 at 02:34
  • @MasonSalcido Really? I ran the second version and it worked perfectly. – nhouser9 Oct 04 '16 at 06:08