pascals-triangle


Drawing a Pascal Triangle with user input (Java)


I'm asking "how many rows" and hoping it prints out the pascal triangle. But I'm getting an error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method nthPascalRow(int) in the type Pascal is not applicable for the arguments (Scanner) at Pascal.main(Pascal.java:39)
Here's my code
public class Pascal {
public static int[] nthPascalRow(int row) {
if (row == 0) {
int[] result = { 1 };
return result;
}
if (row == 1) {
int[] result = { 1, 1 };
return result;
}
int[] previous = { 1, 1 };
for (int r = 2; r <= row; r++) {
int[] next = new int[r + 1];
next[0] = 1;
for (int i = 1; i < r; i++) {
next[i] = previous[i - 1] + previous[i];
}
next[r] = 1;
previous = next;
}
return previous;
}
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("How many rows?");
input.nextInt();
int[] result = nthPascalRow(input);
for (int r : result) {
System.out.print(r + " ");
}
}
}
You should to add the import
import java.util.Scanner;
and to use one variable to put the integer entered by user
int index = input.nextInt();
int[] result = nthPascalRow(index);
You're getting that error because you're passing in input, which is a Scanner object, whereas the method you're calling expects an int row.
Re-consider what you want to pass to the method.
All you have to do is set
input.nextInt()
to a variable and then pass that variable to the method
example:
int a = input.nextInt();
int[] result = nthPascalRow(a);

Related Links

Drawing a Pascal Triangle with user input (Java)

Categories

HOME
xbox-live
cluster-computing
shopify
scipy
avro
amazon-product-api
sbt-assembly
spring-cloud-contract
xlsx
image-recognition
symfony2-easyadmin
quartus
pyephem
google-api-java-client
nmake
google-ima
countif
compare-and-swap
data-cleansing
pentaho-report-designer
mangodb
datadog
lmdb
receipt
salesforce-chatter
pox
pace
redhat-datavirt
linq-to-entities
productivity
stress-testing
scalafx
facebook-chatbot
3scale
azure-availability-set
nstouchbar
uiviewpropertyanimator
instruction-set
adler32
festival
pdfkit
barcode-printing
keycode
pydio
try-finally
angularjs-ng-pattern
foxit
spoofing
emokit
dac
amazon-kcl
magento-1.4
android-cursoradapter
persistence.xml
mirrorlink
abas
xaml-designer
jython-2.7
cpan
lexicographic
activity-streams
snmp4j
dalekjs
surrogate-key
alertifyjs
hamsterdb
visual-studio-addins
sanitization
sslexception
vertical-rhythm
nimrod
trimming
va-list
carddav
apache2.2
fileconveyor
icsharpcode
mvs
n-layer
unions
concurrent-programming
emacs23
serp
zend-form-element
libavformat
gwt-2.2-celltable
visual-c++-2008-express
windows-controls
integer-promotion
visual-studio-2010-beta-2

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App