0

I am trying to make a program that finds the maximum sum through an input that looks something like this:

5 <-this is the number of rows.
1
2 3
1 5 7
9 1 1 2
8 5 4 13 2

But the amount of triangles is unknown. I need to take an input from the console, but I don't know how to see if there is anything left to be input.

In better words. How do I scan the System.in, an input that could be

2
18
430 302
6
102
76 76
268 153 185
492 480 112 405
286 436 126 25 391
390 32 399 76 29 174

or maybe just

2
18
430 302

I know that if I use scan.hasNext() it will block if there isn't anything next. All the numbers for the input could possibly be entered all at once.

Andrew McKeighan
  • 121
  • 1
  • 11
  • What are you trying to find the sum of? Where do triangles come into the mix? What code do you have so far? – pjcognetta Jul 29 '16 at 16:36
  • Possible duplicate of [Stop scanner from reading user input java?](http://stackoverflow.com/questions/10560468/stop-scanner-from-reading-user-input-java) – J. Titus Jul 29 '16 at 16:36
  • @J.Titus That problem has a string to signify that the input is complete. This one has nothing to signal that. Which is why I'm having this problem. – Andrew McKeighan Jul 29 '16 at 18:06

3 Answers3

1

It seems the input has the following format:

    N
    X    ]
    X X  ] pyramid height
    X X X]
    ^---^  pyramid width

    In this case, N=3, T=6

The way I would normally do this is to just read it and check if the input is valid. But in this case the input contains a leading integer N that tells you the size of the 'pyramid' that follows. You might as well use that rather than rely on whitespace to determine where in the character sequence N and the Xs are. Or where one pyramid ends and the other begins. Never rely on whitespace, kids!

I would scan for N and from that determine the total number of elements T in the pyramid. I would then try to read that many numbers, ignoring all whitespace except for using them as separators between the numbers. You can try to keep reading Ns as long as possible, because a successful N read means a pyramid will follow (as long as the input is correct).

Failure to read a number might be because of invalid input or because of reaching the end-of-file. Check the documentation of the I/O facilities of your language.

I always wrote small programs like this to always exit on any input error. It's safe to assume in programming challenges that input errors mean end-of-file and not invalid input.

As this sounds like an extremely familiar coding challenge to me, so I leave the actual coding and finding the appropriate functions to use as an exercise for the reader.

Xunie
  • 437
  • 4
  • 21
  • But the problem is if you have a pyramid coming after the next pyramid, how do you check for that. If I want to know that there are no more pyramids left, what do I do because if I look to see if there is another N it will block if there isn't one. – Andrew McKeighan Jul 29 '16 at 18:05
  • You check if you have reached the end-of-file. Other programs might signal your program an "end of file". See: https://en.wikipedia.org/wiki/End-of-file – Xunie Jul 29 '16 at 21:12
  • I have to add that usually you just TRY the input operation and check to see if it failed. Two reasons it can fail: there's no input yet (timeout) or there will never be input (reached EOF). – Xunie Aug 09 '16 at 12:23
0

Your question is very vague. You said you were looking for a maximum sum. You showed us the input but no result. Are you summing all numbers in the input? Or are you only only summing by line? You didn't try to show us the code that you've written so that we can then find the problem and solve it.

Since You know the number of rows, use for loop to iterate that number of rows and then use scanner.nextLine to get a row. Create a method maybe called parseIntFromString(). You can then use the extensive String manipulations to get the integers and add them together.

I have added an example to get you started:

import java.util.Scanner;


public class TestScannner
{
    private static String temp;
    private static int maximumRowSum ;

    public static void main( String[] args )
    {
        int numberOfRows;
        Scanner input = new Scanner( System.in );

        System.out.println( "Enter the number of rows: " );
        temp = input.nextLine();
        numberOfRows = Integer.parseInt( temp );
        int rowSum[] = new int[ numberOfRows ];

        for( int i = 0; i < numberOfRows; i++ )
        {
            temp = input.nextLine();
            rowSum[ i ] = parseAndAdd( temp );
        }
        System.out.printf(  "The maximum row sum is: %d",  findMax( rowSum ) );

    }

    private static int findMax(int[] rowSum) {
        for( int i = 1; i < rowSum.length; i++ )
        {
            maximumRowSum = Math.max( rowSum[ i - 1 ] , rowSum[ i ] );
        }
        return maximumRowSum;

    }

    private static int parseAndAdd( String combination ) {
        // TODO Auto-generated method stub
        int total = 0;
        String[] numbers = temp.split(  " " );
        for( int i = 0; i < numbers.length; i++ )
        {
            total += Integer.parseInt( numbers[ i ] );
        }
        //total += Integer.parseInt( substr )
        return total;
    }

}
0

Don't be afraid of using hasNext(). If you redirect your input, it will work automatically. If you intend to enter your input manually, you will have to type a special end-of-input character (CTRL-D under *nix, or CTRL+Z for Windows) in the end.

Community
  • 1
  • 1
Leon
  • 31,443
  • 4
  • 72
  • 97