How to call another method inside of the main method? - java

I am trying to call a method, readAndFill(); in my main method. I am getting an error message saying " readAndFill cannot be applied to given types. readAndFill is a method to just put numbers into an array.
I've renaming the method, putting readAndFill(double[]array, int numOfNumbers) instead of readAndFill();.
package program9;
import java.util.Scanner;
import java.util.Arrays;
public class Program9 {
static void readAndFill(double[]array, int numOfNumbers) {
Scanner keyboard = new Scanner(System.in);
for(int i=0;i < numOfNumbers; i++){
System.out.print("Enter a number: ");
array[i] = keyboard.nextDouble();
}
}
public static void main(String[] args) {
int numOfNumbers;
Scanner keyboard = new Scanner(System.in);
double arrayNumbers;
System.out.print("How many numbers will be read in the array?:
");
numOfNumbers = keyboard.nextInt();
double[] array = new double[numOfNumbers];
readAndFill();
}
static void readAndFill(double[]array, int numOfNumbers) {
Scanner keyboard = new Scanner(System.in);
for(int i=0;i < numOfNumbers; i++){
System.out.print("Enter a number: ");
array[i] = keyboard.nextDouble();
}
}

readAndFill methods need two parameter.
Replace the method call in main() with below.
readAndFill(array , numOfNumbers);

call your function this way readAndFill(array , numOfNumbers);

Related

Java for-loop is not executed

The first for-loop you see does not execute and I'm not sure why. It is completely ignored, I tried it in a separate method and I tried it in the main method but something seems to be ignoring but I'm not sure how to get it to run, it simply goes to the next method run in the main method.
package math;
import java.util.Scanner;
public class mathAverageValue {
static int numOfVals;
static double total;
static double average;
static double[] arr = new double[numOfVals];
static String boole;
public static void input() {
Scanner s = new Scanner(System.in);
System.out.println("How many values will be averaged ? : ");
numOfVals = s.nextInt();
for(int i=0; i<arr.length; i++){
System.out.print("Enter Element No."+(i+1)+": ");
arr[i] = s.nextDouble();
}
}
public static void process() {
for (int i=0; i < arr.length; i++) {
total = total + arr[i];
}
average = total / arr.length;
}
public static void output() {
System.out.println("Your average is : " + average);
System.out.println("Would you like to average again? Y or N : ");
Scanner i = new Scanner(System.in);
boole = i.next();
if ("Y".equals(boole)) {
input();
output();
}
}
public static void main(String[] args) {
input();
output();
}
}
Assign some value to static int numOfVals. Java by default assign 0 to it. Hence your for loop will never run. Also modify your array declaration like below:-
static double arr = new double[numOfVals];
The problem is that you have assigned a value to numOfVals and then created the array in the wrong order.
public static void input() {
Scanner s = new Scanner(System.in);
System.out.println("How many values will be averaged ? : ");
numOfVals = s.nextInt();
arr = new double[numOfVals]; // <-- PUT THIS HERE
for(int i=0; i<arr.length; i++){
System.out.print("Enter Element No."+(i+1)+": ");
arr[i] = s.nextDouble();
}
}
It is ignored because it is a zero length array:
static int numOfVals; // This implicitly equals 0.
static double total;
static double average;
static double[] arr = new double[numOfVals]; // so this has no elements.
hence
for(int i=0; i<arr.length; i++){ //arr.length is 0
System.out.print("Enter Element No."+(i+1)+": ");
arr[i] = s.nextDouble();
}
doesn't iterate
According to java primitive data types initialization, all types have a default value. In your case, static int numOfVals will be assigned with 0. This is the reason why the for loop is ignored. see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Nothing shows up on output screen

I'm trying to do a really simple code, but whenever I try to make it print the first statement to get an input information from the user, nothing shows up on the output screen.
Here's the code:
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int grade[] = new int[3];
for(int i = 0; i < grade[i]; grade[i]++){
System.out.println("Input the student's degree");
grade[i] = sc.nextInt();
if(grade[i] < 10 ){
grade[i] += 0.5;
}
System.out.println(grade[i]);
}
}
I can't really see the problem here. If anyone is wondering, I am using Eclipse Neon 0.2 IDE. Thanks for reading
grade[] is empty, so the for-loop never runs. An int-array is initialized with 0's.
Grade array is empty. It has 0 in all indices. grade[0]=0, grade[1]=0 and so on...
Therefore the loop never runs and nothing happens.
This will help you understand:
int grade[] = new int[3];
Add this line under the above code.
System.out.print(grade[0]);
The Value of grade[i] is zero as has not been set. Thus you are not entering the loop.
for(int i = 0; i < grade[i]; grade[i]++){
// ^^^^^^^^ here
Try this:
for(int i = 0; i < 3; i++){
maybe like this:
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int grade[] = new int[3];
for(int i = 0; i < grade.length; i++){
System.out.println("Input the student's degree");
grade[i] = sc.nextInt();
if(grade[i] < 10 ){
grade[i] += 0.5;
}
System.out.println(grade[i]);
}
}
Try with this before for loop
Scanner sc = new Scanner(System.in);
int grade[] = new int[3];
System.out.println("Input the student's degree");
grade[0] = sc.nextInt();
grade[1] = sc.nextInt();
grade[2] = sc.nextInt();
you must rewrite your code, this is a sample code similar to yours with some modifications.
import java.util.Scanner; // import scanner package
public class demo {
public static void main(String args[]){
// create a scanner
Scanner sc = new Scanner(System.in);
// double data type array
double grade[] = new double[3];
for(int i = 0; i < grade.length; i++){
System.out.println("Input the student's degree");
// input double data type value
grade[i] = sc.nextDouble();
if(grade[i] < 10 ){
grade[i] += 0.5;
}
System.out.println(grade[i]);
}
}
}
here is your code with a little modifications.
import java.util.Scanner; // import scanner package
public class demo{
public static void main(String args[]){
// create a scanner
Scanner sc = new Scanner(System.in);
//create a double data type array
double grade[] = new double[3];
// create a for loop with modified condition
for(int i = 0; i <= grade[i]; grade[i]++){
// prompt input message
System.out.println("Input the student's degree");
// assign a double data type value
grade[i] = sc.nextDouble();
// test if grade is less than 10
if(grade[i] < 10 ){
grade[i] += 0.5;
}
System.out.println(grade[i]);
}
}
}

How to get array of strings or integers from users inside looping statement?

When I tried to run this code noOfSub() methods executed properly;
but GC() method faces the following problem:
Enter the number of subjects:
2
Enter Your Subject 1 Grade:
s
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at GPA.GC(GPA.java:21)
at GPA.main(GPA.java:35)
Java Result: 1
Here is my code:
import java.util.Scanner;
public class GPA {
public int noOfSubjects;
public int i=1;
Scanner gradeInput = new Scanner(System.in);
String[] grade = new String[noOfSubjects];
int[] credit = new int[noOfSubjects];
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
}
public void GC() {
while(i<=noOfSubjects)
{
System.out.println("Enter Your Subject "+i+" Grade:" );
grade[i] = gradeInput.nextLine();
System.out.println("Enter the Subject "+i+" Credit:");
credit[i] = gradeInput.nextInt();
i++;
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}
When you do:
public int noOfSubjects;
noOfSubjects is set to 0 which is its default value
So when you have the following code:
String[] grade = new String[noOfSubjects];
it essentially means,
String[] grade = new String[0]; //create a new String array with size 0
which creates an empty array for you.
So when you do,
grade[i] = gradeInput.nextLine(); //where i is 1
you get:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at GPA.GC(GPA.java:21)
at GPA.main(GPA.java:35
because there is no index 1 in String[] grade.
Problem in your array initialization. You can initialize your array after take the input from user.
For example :
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
}
And change your while condition. Instead of this you use
while(i < noOfSubjects)
and set i = 0
If you want to get the size for the array from the user, create the array after getting it from stdin. Otherwise it will create a array with the size of 0 which is the default value for int in java.
Separate your declaration and initalization
String[] grade = null;
int[] credit = null;
...
noOfSubjects = scan.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
Why don't you use ArrayList because the size of array isn't know for you
public class GPA {
public int noOfSubjects;
public int i=0;
Scanner gradeInput = new Scanner(System.in);
List<String> grade = new ArrayList<>();
List<Integer> credit = new ArrayList<>();
public void noOfSub(){
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
}
public void GC(){
while(i<noOfSubjects)
{
System.out.println("Enter Your Subject "+(i+1)+" Grade:" );
grade.add(gradeInput.nextLine());
System.out.println("Enter the Subject "+(i+1)+" Credit:");
credit.add(gradeInput.nextInt());
gradeInput.nextLine();
i++;
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}
Note : i added gradeInput.nextLine() after i++ because the Scanner.nextInt() method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner.nextLine() so i fire a blank gradeInput.nextLine() call after gradeInput.nextInt() to consume rest of that line including newline
Since the noOfSubjects has run time value so the code should be:
import java.util.Scanner;
public class GPA {
public int noOfSubjects;
public int i = 0;
Scanner gradeInput = new Scanner(System.in);
String[] grade;
int[] credit;
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
}
public void GC() {
while (i < noOfSubjects) {
System.out.println("Enter Your Subject " + (i + 1) + " Grade:");
grade[i] = gradeInput.next();
System.out.println("Enter the Subject " + (i + 1) + " Credit:");
credit[i] = gradeInput.nextInt();
i++;
}
for (int j = 0; j < grade.length; j++) {
System.out.println(grade[j] + " " + credit[j]);
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}

User based Multiplication Table

Hi I need help with my code. Im trying to make a user base input multiplication table using a Scanner and a method, but i dont know how to get the user base input from the main method to the method i created, here is what i have so far:
public static void multiplicationTable(int i){
for (int i=1;i<=size;i++){
for (int j=1;j<=size;j++)
System.out.print("\t"+i*j);
System.out.println(); }
}
public static void main (String[] args) {
System.out.println("This program displays a multiplication table.");
Scanner size = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = size.nextInt ();
}
You can pass like :
System.out.println("This program displays a multiplication table.");
Scanner size = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = size.nextInt ();
multiplicationTable(n); \\ pass here
Actually your codes are pretty closed. Please see below (I have tested the codes):
public static void multiplicationTable(int size) {
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
System.out.print("\t" + i * j);
}
System.out.println();
}
}
public static void main(String[] args) throws Exception {
System.out.println("This program displays a multiplication table.");
Scanner size = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = size.nextInt();
multiplicationTable(n);
}

How do I Store 6 integers from Scanner Console into a Set

I know this is simple. How would I take input from my console and store the input into a Set that can later be used to be returned on a Method. This is what I have so far.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class InputConsole {
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>(6);
int[] numbers = new int[6];
Scanner input = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.print("Please enter number ");
numbers[i] = input.nextInt();
{
}
}
}
}
I am using and Array just to test with. The Array is set to 6 so if I type 6 numbers in the console it will stop. I have instantiated the HashSet but I don't know how to go about storing the numbers from the console into it.
Use method Set::add()
for (int i = 0; i < numbers.length; i++)
{
System.out.print("Please enter number ");
s.add(input.nextInt());
}
You don't need int[] array
EDIT:
Whole main()
public static void main(final String ... args)
{
final int inputs = 6;
final Set<Integer> s = new HashSet<Integer>(6);
final Scanner input = new Scanner(System.in);
for (int i = 0; i < inputs; i++)
{
System.out.print("Please enter number #" + (i + 1) + ":");
s.add(input.nextInt());
}
System.out.println("Well done!");
System.out.println(s);
}
import java.util.*;
class Hashsetdemo
{
public static void main(String args[])
{
HashSet h=new HashSet(6);
int [] no = new int[6];
Scanner s=new Scanner(System.in);
for (int i=0;i<no.length;i++)
{
System.out.println("please enter number");
h.add(s.nextInt());
}
System.out.println(h);
}
}

Categories