What is my error in making method to call an array? - java

My code will not compile and I can not figure out how to fix the error. It is a compile time run error I think. My error is in my method to print out array. Then, it says that there is something wrong with my closing bracket for the class. I am using a scanner class and I did import it, it is just not shown. Any help?
My code:
public static void main (String[] args){
int[] getArray; //reference to an int array of type and takes no parameters
System.out.println("How many numbers would you like to enter?" );
int size = scan.nextInt();
int[] nums; //reference to nums array of type int
nums = new int[size]; /* new array of type int. assigned to nums.
size, to specify number of elements array will have */
for(int i=0; i<nums.length; i++){
System.out.println("Enter a number." +(i+1)+ "left");
nums[i] = scan.nextInt(); //storing user input into array
return nums;
}//closing for loop
int[] minimumValue;
min = scan.nextInt();
min = nums[0]; //assigning min value to first element in array
return min;
for(int i=0; i<min.length; i++){
if(i<min){
min = nums[0];
}
}
return min;
public static void printArray (int[] getArray){ //method to print out array
for(int i = 0; i < nums.length; i++){
System.out.print(nums.length); //print out array
}
}
public static void printArrayInReverse(int[] getArray){ //method for arrayInReverse
for(int i = nums.length - 1; i >= 0; i--){
System.out.print(nums[i]);
}
}
int[] numbers = getArray();// calling getArray method
public static void main (String[] args){
System.out.print("************");
printArray(numbers); //calling printArray method and passing numbers through it
printArrayInReverse(numbers);// calling printArrayInReverse method and passing numbers through it
System.out.print(minimumValue(numbers)); /* calling minVal through print statement and passing
numbers through it*/
}
}
}

It is very hard to tell what you are trying to accomplish here from your code. There is no class here which means your program will not even compile, please remember to post all applicable code to your question as it makes it easier for people to help you.
Firstly, you can only have one entry point (ie. main(String[] args) for each class. This is better explained here Can there exist two main methods in a Java program?.
Within this main method, you cannot have any other methods, you can only call other methods and perform operations ad such.
The variable "scan" cannot ever do anything if it is not instantiated prior to use. The variable getArray is being used as a method, which it is not.
Please take a look at Simple Java array program where it shows more in-depth how to use arrays.
Take a look at this as see if it even accomplishes what you want to do, which is still somewhat unclear. I shortened everything so that it is simpler, with a program this small multiple methods are not needed unless there is some logic to denote when to print the array or when to print the array reversed.
import java.util.Scanner;
public class CLASSNAME {
static Scanner scan = new Scanner(System.in);
static int[] nums;
public static void main(String[] args){
// Get Size Of Array
System.out.println("How many numbers would you like to enter?" );
int size = scan.nextInt();
nums = new int[size];
// Fill Array
for(int i = 0; i < nums.length; i++){
System.out.println("Enter a number." +(i+1)+ "left");
nums[i] = scan.nextInt();
}
// Set 0th Number To
System.out.println("Enter 0th Number");
int min = scan.nextInt();
nums[0] = min;
// Print Array
System.out.println("\n" + "Printing Array Index 0 -> ArraySize");
for(int i = 0; i < nums.length; i++){
System.out.print(nums.length);
}
// Print Array Reversed
System.out.println("\n" + "Printing Array Index ArraySize -> 0");
for(int i = nums.length - 1; i >= 0; i--){
System.out.print(nums[i]);
}
}
}

you need to create a Scanner object to use it
you can't create more than one main method
you must create methods outside the main method
Example:
import java.util.Scanner;
public class a {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//code here
}
public static void method_name() {
//code here
}
//If you want to return integer value for example
public static int method_name() {
//code here
}
}

Related

Why does my method that I am trying to call to inside main not work?

minGap(array); is not being recognized. I don't know what I have done wrong, but I am sure it is a super simple fix. Trying to figure out if it is something to do with the data type being used or if it has something to do with the arrangement of the line " " added. Any hints?
package Lab8;
import java.util.*;
import java.util.Scanner;
public class Question_One {
public static void main(String args[]) {
int length;
Scanner input = new Scanner(System.in); //scanner to input any size array user wants
System.out.println("Please enter the numbers for the array.");
length = input.nextInt();
String[] array = new String[length];
for(int i = 0;i <length;i++) { //counter logic
System.out.println("How many integers are in the array?"+(i+1));
array[i] = input.nextLine();
}
System.out.println("Enter the numbers for the array (individually):");
for(int i = 0;i <length;i++) { //counter logic
System.out.print(array [i]);
array[i] = input.nextLine();
}
input.close();
minGap(array);
}
private static int minGap(int a[], int gapMin) {
int []gap = new int[a.length];
//a
for (int i=0;i<a.length-2;i++) {
if (gapMin>gap[i]) {
gapMin=gap[1];
}
}
return gapMin;
}
}
I believe you wanted a method to find the minimum gap. As such, you should not be passing that into the method. Your logic is also a bit off, you want to take the minimum value after gapMin>gap[i] (not a hardcoded gap[1]). So you could do,
private static int minGap(int a[]) {
int gapMin = Integer.MAX_VALUE;
int[] gap = new int[a.length];
for (int i = 0; i < a.length; i++) {
if (gapMin > gap[i]) {
gapMin = gap[i];
}
}
return gapMin;
}
or (if you're using Java 8+)
private static int minGap(int a[]) {
return Arrays.stream(a).min().getAsInt();
}
Then you need to actually save that value or print it. That is, change
minGap(array);
to (just print it)
System.out.println(minGap(array));
And you need an array of int (not a String[]).
int[] array = new int[length];
for(int i = 0; i < length; i++) {
System.out.printf("Please enter integer %d for the array%n", i + 1);
array[i] = input.nextInt();
}

Trying to remove the 5th name entered from my array. I'm getting a cannot find symbol error and I can't figure out why

import java.util.Scanner;
public class Assignment6APt2 {
public static void main(String[] args) {
int n = 10;
Scanner scan = new Scanner(System.in);
System.out.println("Enter 10 names: ");
String [] names = new String[n];
for (int i = 0; i < names.length; i++){
names[i] = scan.nextLine();
}
names.remove(4);
arrayMethod(names);
}
private static void arrayMethod(String[] names)
{
for (String a : names)
{
System.out.printf( "%s",a);
}
}
}
There isn't a remove method for arrays, and this operation really doesn't make sense anyway. What does it even mean to "remove" an item from a fixed-size array?
The closest thing you can do is set array index 5 to null, which is just a simple assignment. I'm not sure I recommend doing that, though. You probably want to either rethink doing that or just use a variable-size data structure.

Passing arrays in java

I know i can't pass arrays the way I'm doing it. Would I need to pass by reference if so how? The question is at the bottom for reference.
import java.util.Scanner;
public class MethodsArrays {
public static int[] fillArray() {
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int array[] = new int[size];
int pos=0;
for(int i=0; i<array.length; i++) {
pos=i+1;
System.out.println("Enter element " + pos);
array[i]=scan.nextInt();
}
return array;
}
public static int sumArray(int [] array) {
int sum=0;
for(int i=0; i<array.length-1; i++) {
sum=array[i]+array[i+1];
}
return sum;
}
public static int avgArray(int [] array) {
int avg=0;
int sum = sumArray(array);
avg = sumArray(array)/array.length-1;
return avg;
}
public static void printArray(int [] array) {
for(int i=0; i<array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void main(String[] args) {
fillArray();
System.out.println("Sum=" + sumArray(array));
System.out.println("Average=" + avgArray(array));
printArray(array);
}
}
Write a Java program, called MethodsArrays that has 4 static methods
called fillArray(), sumArray(), avgArray(), and printArray(). The
fillArray() method should be called from the main method. The
fillArray() method should use a Scanner to take in a number
representing the length of the array and then read in numbers to fill
the array. The sumArray() method should take an int array as its input
parameter and returns an integer value that is the sum of all the
elements in the array. The avgArray() method should take an int array
as its input parameter and returns an integer value that is the
average of all the elements in the array. The printArray() method
should take an int array as its input parameter and has no return
value. It should then print out the elements of the array on the same
line separated by a space (“ “). All methods should work for integer
arrays.
Your code looks OK. You just need to assign the result of fillArray() to a variable, in order to use this result for further methods.
It would look like:
int[] array = fillArray();
System.out.println("Sum=" + sumArray(array));
System.out.println("Average=" + avgArray(array));
printArray(array);

Arranging array accending

Aim is to get an array and arrange it accending, and print
package habeeb;
import java.util.*;
public class Habeeb {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] num = new int[10];
int i, count=0, m;
System.out.println("Enter the integers, with 0 to end the array" );
for( i=0; i<num.length; i++){
num[i]= input.nextInt();
Zero breaks the array here
if(num[i]==0)
break;
count++;
Calling the function here\
}
Sorting(num, count);
The function sorting is here
}
public static void Sorting(int[] sort, int con){
if(con<0)
return;
int j, max=0, coun=0, temp;
for(j=0; j<con; j++){
if(sort[j]>max)
max=sort[j];
coun=j;
}
here am swaping the last value in the array for any index thats the highest
temp=sort[con];
sort[con]=sort[coun];
sort[coun]=temp;
Calling the function again here(recursive)
Sorting(sort, con-1);
Here am printing, why is it not printing
for(j=0; j<con; j++){
System.out.println(sort[j]);
}
}
}
Is there a reason not to use Arrays.sort() ?
All you have to do is call
Arrays.sort(num);
Be careful since this will modify your array and will not create a sorted coyp of it!

java display method

Here im required to Write a method printArray that displays the contents of the array num and Display the contents of the array with each
number separated by a space. and i have to start a new line after every 20 elements.
i wrote this code but whenever i try to execute it, it shows the array without the new line
public class project2 {
public static void main(String[] args) {
int num []= new int [100];
for (int i=0;i<num.length;i++){
num[i]=-1;
num[7]=7;
}
printArray(num);
System.out.println(num);
}
public static void printArray (int array1[]){
int count =20;
for (int x=0;x<array1.length;x++){
System.out.print(array1[x]+" ");
if (array1[x]==count){
System.out.println(" ");
count=array1[x]+count;
}
}
}
}
import java.util.Arrays;
import java.util.Random;
public class project2 {
public static void main(String[] args) {
int num[] = new int[100];
Random random = new Random();
for (int i = 0; i < num.length; i++) {
num[i] = random.nextInt(100);
}
printArray(num);
System.out.println('\n' + Arrays.toString(num));
}
public static void printArray(int array1[]) {
int count = 20;
for (int i = 0; i < array1.length; i++) {
System.out.printf("%2d ", array1[i]);
if ((i + 1) % count == 0) {
System.out.println("");
}
}
}
}
You should use the modulo (or remainder) operator (%), that suits your usage much better:
for (int x=0;x<array1.length;x++){
System.out.print(array1[x]+" ");
if (x>0 && (x%count)==0){
System.out.println(" ");
}
}
This way, you will get a new line every count characters, and the first line will not have it (that is why the x>0 check is there).
Also, in the original post, this line is frankly totally bad:
count=array1[x]+count;
Just what would it do? Why do you add the value stored in the array to the fixed counter? Considering this line, I advise that you should really sit back a bit, and try to think about how things work in the background... There is no magic!
Take a closer look at your if-statement:
if (array1[x]==count)
According to your array values, this will never return true
i have to start a new line after every 20 elements.
Change to following code:
if (x%20 == 0)
{
System.out.println();
}
in place of
if (array1[x]==count)
{
System.out.println(" ");
count=array1[x]+count;
}
Problem is with
if (array1[x]==count)
You are comparing count with value present in array. Instead compare it with desired count ie 20 or Use modulo operator as suggested in other answers / comments .
int count = 1;
for (int x=0;x<array1.length;x++){
System.out.print(array1[x]+" ");
if (count == 20){ // Check if its 20th element
System.out.println(" ");
count=1; // reset count
}
count++;
}

Categories

Resources