0

Alright, so I'm supposed to work with this as the beginning of my code:

public static int addOdds(int[] input){}

That would return the sum.

I have the add odds part finished with pre loaded arrays. That was simple enough, but what is flustering my person is how to get this to take an array as input from user.

I know of Scanner from java.utils, but I'm not sure how to get it to run with my code to make it take an array (if it can).

I considered using:

public string void main(String [] args){}

And call Scanner with it, and use Integer.parseint(), but I don't think that could parse an array.

Then call the input the array from scanner to be handed off to the addOdds method.

An example would be {2,3,7,8,4,1} would be what the code must take as input.

Thanks in advance, I'm a bit stumped.

If it helps, here is an example query; inputTwo isn't relevant to the question:

public class Proj2Tester {
    public static void main(String[] args){
        int[] inputOne = {4,8,9,12,7};
        int[] inputTwo = {41,38,19,112,705};
        System.out.println("Problem 1 is correct on test input = " +  (16 == Problem1.addOdds(inputOne)));
        System.out.println("Problem 2 is correct on test input = " + (686== Problem2.getRange(inputTwo)));

    }

Following T.J.'s advice I tried the following:

public class Problem1 { public static void main(String args[]){

    System.out.println("Your array is: "+input);

    }

}

public static int addOdds(int[] input){
    int[] input = new int[args.length]; //Begin T.J.'s segment
    int n = 0;
    for (String arg : args) {
    input[n++] = Integer.parseInt(arg);


    int sum =0; //Initializing sum as 0;

    for(int i =0; i < inputOne.length; i++){
        if(inputOne[i] % 2 !=0){
            ;
            sum = sum + inputOne[i];
            break;
        }
        if(inputOne[i] % 2 == 0){

            i++;
            break;

    }
    }


    return sum; // placeholder for my answer, not zero should be returned
}
Nikki
  • 69
  • 1
  • 10

2 Answers2

2

At least two options:

  1. Have the user specify the array entries as individual command line arguments, e.g.:

    java YourClass 2 3 7 8 4 1
    

    ...and then parse the entries in args into an int array:

    int[] input = new int[args.length];
    int n = 0;
    for (String arg : args) {
        input[n++] = Integer.parseInt(arg);
    }
    
  2. Have the user specify the array as a single command-line argument with commas:

    java YourClass "2,3,7,8,4,1"
    

    ...and then split args[0] on comma and parse the entries in the resulting string array into an int array.

#1 seems simpler to me, as you start out with a string array.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the response! So, calling would be: java addOdds( 2 3 7 8 4 1) Correct me if I am wrong, but this doesn't require Scanner because it's given it's args at command line? I'm guessing I fumbled some where with my code, it keeps on telling me that it can't find my class. I'm posting an update to the question. – Nikki Feb 02 '15 at 18:24
  • @Yarou: No, you'd call it the way I showed calling it in the answer. :-) And yes, it doesn't require `Scanner` because you're using command-line args instead. – T.J. Crowder Feb 02 '15 at 18:41
  • Thank you! I cd'ed into the directory and tried to run it as you mentioned, terminal returns " cannot find my class". I'm wagering that I messed something up in the rest of the code. I posted what I have so far. If it helps, I'm using the Netbeans IDE. – Nikki Feb 02 '15 at 19:07
1

You might want to consider splitting one String on a comma? See here : How to split a string in Java

Community
  • 1
  • 1
Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121