0

Here is my main method:

public class Main {

    public static void main(String[] args){
        Comp.give(f, s);
    }
}

And here is my other class:

import java.util.Scanner;

public class Comp {
    private void look() {
        Scanner iscan = new Scanner(System.in);
        int f = iscan.nextInt();
        int s = iscan.nextInt();
    }

    public static Object give(int f, int s) {
        return f + s;

    }
}

I'd like to be able to use the two Ints f and s (first and second) in the main method.

And if that's a stupid question, I'd just like to be able to use a getter/call the give method from the main method. How do I do this?

I'm new to coding, so assume that I know next to nothing. Explain very thoroughly. Thanks!

EDIT - Code is supposed to take two ints and return the sum.

nbray
  • 81
  • 5
  • maybe you can take a look of what a DTO is, it could help you – jpganz18 Sep 14 '15 at 16:22
  • 1
    The code wouldn't even compile. `f`and `s`in main are not declared. What are you trying to do? Get the sum of two integers? Then why do you return `Object` not `int`? What is the method `look`supposed to do? – Würgspaß Sep 14 '15 at 16:22
  • 4
    [this](https://stackoverflow.com/questions/457629/how-to-return-multiple-objects-from-a-java-method) could help, but I suggest you to read a basic Java book... – Alex S. Diaz Sep 14 '15 at 16:22
  • Can you edit your question and explain what your code is supposed to achieve? What the original assignment was? And explain what each of your methods is supposed to do in that assignment? – RealSkeptic Sep 14 '15 at 16:38
  • @RealSkeptic - I will do so now. – nbray Sep 14 '15 at 23:34

2 Answers2

1

You could do this by setting the variables f and s as instance variables. You could do by setting like so in the Comp class:

public int f;
public int s;

Then you can reference each variable by doing this in the main:

Comp example = new Comp();
int f = example.f;
int s = example.s;
the_Kid26
  • 59
  • 2
  • 9
0

So you need the look method to return the input ints as well as the sum of those ints.

So let's create a class to contain this information:

public class IntsAndSum {
    private List<Integer> ints;
    private int sum;
    // constructor
    public IntsAndSum(List<Integer> ints, int sum) {
        this.ints = ints;
        this.sum = sum;
    }
    // + getters
}

Now let's write look so that it returns an instance of IntsAndSum:

private IntsAndSum look(){
    Scanner iscan = new Scanner(System.in);
    int f = iscan.nextInt();
    int s = iscan.nextInt();
    // let's put them into a List<Integer>
    List<Integer> ints = new ArrayList<>();
    ints.add(f);
    ints.add(s);
    // let's compute the sum
    int sum = f + s;
    // let's return a new IntsAndSum
    return new IntsAndSum(ints, sum);
}

And now you can access the information in the main method:

public static void main(String[] args){
    IntsAndSum is = Comp.give(f, s);
    // let's print the numbers:
    for(int n : is.getInts()) {
        System.out.println(n);
    }
    // let's print the sum
    System.out.println("sum is: " + is.getSum());
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • 4
    First, I think your solution goes way over the head of the OP. I think OP's problem is not knowing how to use arguments and return values - how data flows through calls. Second - `f` and `s` are not defined in the `main`, so we're back to square 1. – RealSkeptic Sep 14 '15 at 16:32