javahelp javahelp - 4 months ago 14
Java Question

using methods in java programming

import java.util.Scanner ;
public class ProcessNumbers
{
public static void main( String[] args )
{
Scanner in = new Scanner(System.in) ;
System.out.print("Please enter an integer between 6 and 12, inclusive: ") ;
int num = in.nextInt() ;
System.out.print(num) ;
System.out.println();
boolean result = shouldProcess(num);
processInput(result) ;
}
public static boolean shouldProcess(int n)
{
if (n>=6 && n<12)
{
return true;
}
else
{
return false;
}
}
public static void processInput(boolean result2)
{
if (result2 == true)
{
int sum = findSum(num) ;
public static void findSum(int num)
{
int sum = 0 ;
for (int i = 1; i <=num; i++0)
{
sum +=i ;
System.out.println(sum) ;
}
}
else
{
System.out.println("Number is outside of acceptable range") ;
}
}
}
}


With this code I am trying to get the user to input a number. The computer will read in this number and return a boolean value in the method shouldProcess if the given number is between 6 and 12(inclusive). If it is not within these two numbers it will return false. Next I have made the method processINput which uses the first method shouldProcess to deterermine if it can compute the sum, only if shouldProcess returns true.

I am running into these errors in my shouldProcess method block when I am trying to compute the sum

[File: /ProcessNumbers.java Line: 34, Column: 10] illegal start of expression
[File: /ProcessNumbers.java Line: 34, Column: 17] illegal start of expression
[File: /ProcessNumbers.java Line: 34, Column: 23] ';' expected
[File: /ProcessNumbers.java Line: 34, Column: 41] '.class' expected
[File: /ProcessNumbers.java Line: 34, Column: 44] ';' expected
[File: /ProcessNumbers.java Line: 36, Column: 21] ';' expected
[File: /ProcessNumbers.java Line: 37, Column: 38] ')' expected
[File: /ProcessNumbers.java Line: 37, Column: 39] ';' expected
[File: /ProcessNumbers.java Line: 40, Column: 37] ';' expected
[File: /ProcessNumbers.java Line: 43, Column: 6] 'else' without 'if'
[File: /ProcessNumbers.java Line: 47, Column: 3] reached end of file while parsing
[File: /ProcessNumbers.java Line: 48, Column: 1] illegal character: \26

Answer

You are defining a function findSum inside a function processInput which is not allowed and that function is actually not required. Check this code:

import java.util.Scanner;
public class ProcessNumbers
{
    public static void main( String[] args )
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter an integer between 6 and 12, inclusive: ");
        int num = in.nextInt();
        System.out.print(num);
        System.out.println();
        boolean result = shouldProcess(num);
        processInput(result, num); // passing the result and num
    }

    public static boolean shouldProcess(int n) 
    {
        if (n>=6 && n<12)
        {
            return true;
        }
        else
        {
            return false;
        } 
    }

    public static void processInput(boolean result2, int num) // added int num argument
    {
        if (result2 == true)
        {
            //int sum = findSum(num);

            //public static void findSum(int num) // not allowed
            //{
                int sum = 0; // missed the semi-colon
                for (int i = 1; i <=num; i++)
                {
                      sum +=i;
                }
                System.out.println(sum); // missed the semi-colon
            //}
        }
        else 
        {
            System.out.println("Number is outside of acceptable range");
        }
    }  
}