0

My current homework assignment has a method that prompts the user for matrix values and returns the array. The scanner that inputs a string value into the array titled "userInput", at index 0 isn't prompting for some reason.

Here's the method:

public static double[][] setArray()
{

    //initiate variables
    String stringValue = "";
    double doubleValue = 0;

    //instantiate return array
    double[][] array = new double[3][4];

    //instantiate string array for user input values
    String[] userInput =  new String[3];
    //prompt user for array values by row
    System.out.println("Enter matrix values by row: ");
    userInput[0] = in.nextLine();//???
    userInput[1] = in.nextLine();
    userInput[2] = in.nextLine();

    System.out.println();

    for(int eachString = 0; eachString < 3; eachString++)
    {
        for(int index = 0; index < userInput[eachString].length(); index++)
        {

            //if string does not contain a space, period, or valid digit


            //while index holds invalid character
            while(userInput[eachString].charAt(index) < '0' 
                || userInput[eachString].charAt(index) > '9')

            { 
                if(userInput[eachString].charAt(index) == ' ' 
                || userInput[eachString].charAt(index) == '.')
                {
                    break;
                }    
                else
                {
                    System.out.println("Invalid input. Digits must be in integer"
                            + " or double form. (i.e. 4  5.0  2.3  9)");  
                    System.out.println("Re-enter row " +  eachString + ": ");
                    userInput[eachString] = in.nextLine();    
                }


            }
        }

        //given string is valid at this point//

        int valueCounter = 0;

        //for each index in string value
        for(int eachIndex = 0; eachIndex < userInput[eachString].length(); eachIndex++)
        {
            //if value != ' '... += string... if value == ' ' stop loop
            if(userInput[eachString].charAt(eachIndex) != ' ')
            {
                stringValue += userInput[eachString].charAt(eachIndex);
            }
            //parse string to double value and add to array
            //last digit will not parse unless condition also contains..
            //userInput[eachString].length() - 1
            if(userInput[eachString].charAt(eachIndex) == ' ' 
                    || eachIndex == userInput[eachString].length() - 1)
            {
                doubleValue = Double.valueOf(stringValue);
                array[eachString][valueCounter] = doubleValue;
                valueCounter++;
                //value counter is column index
                stringValue = "";//clear string   
            }
        }
    }

    return array;
}

And here's the rest of the program, including another method and the main. The purpose of the complete program is to sum the elements of a column in a multidimensional array. An explanation of why this is occurring (unless it's a simple fix) would be highly appreciated, thank you!

    public static double getSum(int col, double[][] num)
    {
        //initiate return value
        double sum = 0;

        //sum column of multidimensional array
        for(int row = 0; row < num.length; row++)
        {
            //row increments, user selected col is a fixed value
            sum += num[row][col];       
        }

        return sum;
    }

    public static void main(String[] args) 
    {
        //TEST//
//        double[][] array = 
//        {
//            {3,4,5},
//            {3,4,5},
//            {3,4,5},
//        };
//
//        //sum col 0 == 9, col 1 == 12, col 2 == 15
//        double sum = getSum(0, array);
//        System.out.println(sum);


    double[][] array = new double[3][4];

    double[] columnSum = new double[4];

    int userSelection = -1;

    System.out.println("Welcome to the sum column program.");


//        TEST
//        for(int index = 0; index < array.length; index++)
//        {
//            for(int col = 0; col < 4; col++)
//            {
//               System.out.println("index " + index + " at " + col + " is " + array[index][col]);  
//            }
//            
//        }

//        column[0] = getSum(0, array);
//        column[1] = getSum(1, array);
//        column[2] = getSum(2, array);
//        
//        System.out.println(column[0]);

        //---
        do
        {

            System.out.println("0) Exit program");
            System.out.println("1) Run program");
            System.out.print("Enter choice: ");


            while(!in.hasNextInt())
            {
                System.out.print("Invalid choice. Re-enter: ");
                in.next();
            }

            userSelection = in.nextInt();

            switch(userSelection)
            {
                case 0: System.exit(0); break;
                case 1: 
                {
                    array = setArray();//prompt user for array
                    columnSum[0] = getSum(0, array);
                    columnSum[1] = getSum(1, array);
                    columnSum[2] = getSum(2, array);
                    columnSum[3] = getSum(3, array);
                    System.out.println("Sum in column 1 is " + columnSum[0]);
                    System.out.println("Sum in column 2 is " + columnSum[1]);
                    System.out.println("Sum in column 3 is " + columnSum[2]);
                    System.out.println("Sum in column 4 is " + columnSum[3]);

                }break; 

            }

        }while(userSelection != 0);
DevEarl
  • 18
  • 1
  • 3
  • 1
    It's possible that the `Scanner` still has a new line character in it's stream when you call `nextLine`, this tends to happen when you use `in.nextInt` – MadProgrammer Apr 20 '16 at 01:58
  • Answered, thanks to the duplicate post I wasn't aware of :). Thank you for the help. – DevEarl Apr 20 '16 at 02:03

0 Answers0