why does it stop half way through my code? - java

So the first part creates a vector and adds a digit to the 10 slots. Then after this nothing happens, i have no errors in my code but it just stops.. why?
package ovn7;
import java.util.Scanner;
public class ovn7a {
int []vektor;
Scanner scan = new Scanner(System.in);
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
}
public int find(int tal) {
System.out.println("tal");
tal = scan.nextInt();
int i = 0;
while(i<10 && vektor[i] != tal) {
i++;
}
return (i <10) ? i : -1;
}
}

This is your main method:
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
}
That's all your program does - when it hits the closing right brace of the main method, execution ends. If you want it to execute public int find(int tal) as well, you need to include a method call to your main method:
int index = find(5); //for example
Remember, the main method is the only one that is called automatically when executing the program! You'll have to call find yourself inside main.
EDIT: per request, an example of main with the method call included:
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
int index = find(5); // <-- this find(5) here is a method call for find!
System.out.println("The method returned a value of " + index + ".");
}
You can replace that "5" with any integer, as the method find accepts an integer as an argument. (as a side note, it doesn't matter which integer you pass to find - it overwrites the argument with a new value anyway)

Related

How do I pass the array to the main method after calculating the sum of values?

So this is the code I currently have. I am trying to calculate the sum of all the numbers in the second method and then return it to the main method to display but I am getting confused with how to do this properly. Any help is welcome!
public class Main {
public static void main(String[] args) {
int[] population = {
693417,
457502,
109985,
107360,
103773,
13145,
5469
};
int[] total = computeTotal(population);
for (int i = 0; i < total.length; i++);
System.out.print(total + " ");
}
public static int computeTotal(int[] population) {
int[] population2 = {
693417,
457502,
109985,
107360,
103773,
13145,
5469
};
return population2;
}
}
If want to calculate sum via method, you can just return an integer.
public static void main(String[] args) {
int[] population = { 693417, 457502, 109985, 107360, 103773, 13145, 5469 };
int total = computeTotal(population);
System.out.print(total + " ");
}
public static int computeTotal(int[] Popu) {
int sum=0;
for(int i=0;i<Popu.length;i++)
sum+=Popu[i];
return sum;
}
By the way the for loop you write is going to do nothing because it just run length times with no command according to ; is the first command each time the executing loop see .
You should write like this
for(int i=0;i<Popu.length;i++)
only one line code end with ;
or
for(int i=0;i<Popu.length;i++){
...
}
to run mutiple code.

Array Method issue

having a problem with my java program. I am a newbie to Java and just can't figure out what is exactly the issue with it. In short I've declared an array and a variable in main, I've created my method call and would like my array be passed into my method with the variable. I would then like the method to take my array and count the number of times my variable "8" occurs, get rid of the 8 out of the array and return a new smaller array back to main. Here is my code below. I feel as if I am just missing one block code any suggestions?
public class Harrison7b
{
public static void main(String [] args)
{
int[] arrayA = {2,4,8,19,32,17,17,18,25,17,8,3,4,8};
int varB = 8;
// Call with the array and variable you need to find.
int[] result = newSmallerArray(arrayA, varB);
for(int x = 0; x < arrayA.length; x++)
{
System.out.print(arrayA[x] + " ");
}
}
public static int[] newSmallerArray( int[] arrayA, int varB)
{
int count = 0;
for(int x = 0; x < arrayA.length; x++)
{
if(arrayA[x] == varB)
{
count++;
}
}
int [] arrayX = new int[arrayA.length - count];
for(int B = 0; B < arrayA.length; B++)
{
if(arrayA[B] != varB)
{
}
}
return arrayX;
}
}
you do not actually need to return the array because when you pass an array to a method you also pass its memory address meaning its the same address that you change so, it will also change the arraysA of main method because you are just changing the values of the same memory adress
import java.util.*;
public class Help
{
public static void main(String[] args)
{
ArrayList<Integer> arraysA = new ArrayList<Integer>();
arraysA.add(Integer.valueOf(2));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(19));
arraysA.add(Integer.valueOf(32));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(18));
arraysA.add(Integer.valueOf(25));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(3));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
int varB=8;
newSmallerArray(arraysA,varB);
for(Integer i:arraysA)
{
System.out.println(i);
}
}
public static void newSmallerArray(ArrayList<Integer> arraysA,int varB)
{
for(int i=0;i<arraysA.size();++i)
{
if(Integer.valueOf(arraysA.get(i))==varB)
{
arraysA.remove(i);
}
}
}
}
Try this code it will not require for loop:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(arrayA));
list.removeAll(Arrays.asList(8));
arrayA = list.toArray(array);

printing method class randTest (int n)

I am having trouble printing out the method randTest (int n) as errors keep appearing. I want to print this method in the main as i want to print the results in a clear tabular form. The method must stay static void. This is my code below:
public class RandNumGenerator {
public static int RandInt(){
double n = Math.random()*10;
return (int) n;
}
public static void randTest(int n){
int [] counts = new int [10];
for(int i=0;i<n;i++){
counts[i] = RandInt();
System.out.println(counts[i]);
}
}
public static void main(String[] args) {
System.out.println(RandInt());
System.out.println(randTest());
}
}
Just call the method randTest() instead of printing it. The work of printing is already done inside the method. And randTest takes in an argument of the type int. So, you need to save the return of RandInt and then pass it into the method.
public static void main(String[] args) {
int randInt = RandInt();
System.out.println(randInt);
randTest(randInt);
}

How do I implement "this" into my Java program?

It's a requirement of my school assignment that I use "this" in the following program. However, I can't quite figure out where I could put this. I keep getting a "non-static variable this cannot be referenced from a static context" error.
import java.util.Scanner;
public class PrimeNumber
{
public static void main(String args[])
{
System.out.println("Enter the upper limit for the prime numbers computation: ");
int upperLimit = new Scanner(System.in).nextInt();
int count = 0;
for(int number = 2; number<=upperLimit; number++)
{
if(isPrime(number))
{
System.out.println(number);
count++;
}
}
System.out.println("Number of primes generated: " + count);
}
public static boolean isPrime(int number)
{
for(int i=2; i<number; i++)
{
if(number%i == 0)
{
return false;
}
}
return true;
}
}
The Java keyword this refers to the instance of your class that invoked an instance method. A static method is general to its class, and so you cannot reference any instance (non-static) variables from within it. You can only access instance variables like this from within an instance method, that is, a method that is not defined as static.
So, you would need to create an instance method (of which there are none in your class), in order to use this.
This is nothing more than a reference to the object on which the method was called. Static methods on the other hand can operate without any instance of the class even exisiting, therefore they can't have reference to any object. That's why you can't use this in static method. If you really need this, you have to remove static keywords from your functions and use instance variables in those functions anyhow.
public class PrimeNumber
{
public int count = 0;
public int upperLimit;
public static void main(String args[])
{
PrimeNumber pn = new PrimeNumber();
System.out.println("Enter the upper limit for the prime numbers computation: ");
pn.upperLimit = new Scanner(System.in).nextInt();
pn.doCheck();
System.out.println("Number of primes generated: " + pn.count);
}
public void doCheck() {
for (int number = 2; number <= this.upperLimit; number++)
{
if (this.isPrime(number))
{
System.out.println(number);
count++;
}
}
}
public boolean isPrime(int number)
{
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
}

Incorrect output while using methods in recursion. The return statement in a method returns an integer at the end of the output

package abcde;
public class Abcde{
public static void main(String[] args){
System.out.println(recursion(97));
}
public static int recursion (int n) {
for ( int i = n; i< 123; i++)
{
String achar = new Character((char)i).toString();
System.out.print(achar);
}
return -1;
}
}
The desired output has to be abcdefghijklmnopqrstuvwxyz while the output is abcdefghijklmnopqrstuvwxyz-1
I dont want the -1 at the end.
When i change the input type to void, the method does not work.
What can we do about it?
The final -1 gets printed outside the recursion method: main() prints what recursion returns, which happens to be -1.
If you change the return type of recursion to void, you need to call it as a method, not as an expression. In other words, you can no longer call println(recursion(97)) because println expects a value to print.
As a side note, in order to be recursive a method needs to have a code path that calls the same method, directly or indirectly. Since the "calling itself" feature is missing from your recursion method, consider renaming it to iteration.
rewrite it to:
public static void main(String[] args){
noRecursion(97);
}
public static void noRecursion (int n) {
//^^^^
for (int i = n; i< 123; i++) {
System.out.print((char)i);
}
}
Actually, your recursion function doesn't return anything but -1, but It does print all of the other characters before it returns.
if you change it to
public static void notRecursion (int n) {
for ( int i = n; i< 123; i++)
{
String achar = new Character((char)i).toString();
System.out.print(achar);
}
}
and then just call it from your main like
notRecursion(97);
It will print what you want it to print
Or
public static String recursion (int n)
{
if(n >97 +1)
{
System.out.print(recursion(n-1));
}
String achar = new Character((char)n).toString();
return(achar)
}
and then call it with
System.out.println(recursion(123));
Choose pure functions over side effects:
public static String iteration (int n) {
final StringBuilder b = new StringBuilder();
for (char c = (char)n; c < 123; c++)
b.append(c);
return b.toString();
}
Now you can call this method as you are calling it now, and print its result.

Categories