When compiling this java program, I get errors like cannot find symbol... any suggestions?
import java.io.*;
import java.util.Scanner;
public class joel001
{
public int d;
// find the smallest number of an array
public static int small(int a[])
{
int smallest=0;
for(int i=0;i<a.length();i++)
{
if(a[i]<smallest)
{
smallest=a[i];
}
}
return smallest;
}
// subtract the smallest number of an array from all its elements
public static int[] sub(int a[],int d)
{
this.d=d;
for(int i=0;i<a.length();i++)
{
a[i]=a[i]-d;
}
return a;
}
// count the array's non zero elements
public static int count(int a[])
{
int countn=0;
for(int i=0;i<a.length();i++)
{
if(a[i]!=0)
{
countn=countn+1;
}
}
return countn;
}
public static void main(String args[])
{
int b,c,z,k=1;
int a[]=new int[1000];
Scanner s=new Scanner(System.in);
b=s.nextInt(); //input
for(i=0;i<b;i++)
{
a[i]=s.nextInt();
}
while(k==1)
{
z=count(a);
if(z==0)
{
break;
}
System.out.println(z);
c=small(a);
a=sub(a,c);
}
}
}
For a start, the length of an array is arr.length, not arr.length().
Secondly, in sub(), there is no this because it's a static function.
Thirdly, in main(), you need to declare i before trying to use it.
That will take care of all your compile-time errors. Run-time, or logic, errors are something you need to learn to fix in a debugger.
You are declaring all your methods of joel001 as static. Static methods are not related to an instance or object of the joel001 class. You need to research OOP before you continue.
Try something like this:
import java.io.*;
import java.util.Scanner;
public class joel001
{
public int d;
// find the smallest number of an array
public int small(int a[])
{
int smallest=0;
for(int i=0;i<a.length();i++)
{
if(a[i]<smallest)
{
smallest=a[i];
}
}
return smallest;
}
// subtract the smallest number of an array from all its elements
public int[] sub(int a[],int d)
{
this.d=d;
for(int i=0;i<a.length();i++)
{
a[i]=a[i]-d;
}
return a;
}
// count the array's non zero elements
public int count(int a[])
{
int countn=0;
for(int i=0;i<a.length();i++)
{
if(a[i]!=0)
{
countn=countn+1;
}
}
return countn;
}
public void main2() {
int b,c,z,k=1;
int a[]=new int[1000];
Scanner s=new Scanner(System.in);
b=s.nextInt(); //input
for(int i=0;i<b;i++)
{
a[i]=s.nextInt();
}
while(k==1)
{
z=count(a);
if(z==0)
{
break;
}
System.out.println(z);
c=small(a);
a=sub(a,c);
}
}
public static void main(String args[])
{
joel001 obj = new joel001();
obj.main2();
}
}
Related
This is just a dummy code.
I fail to understand what is wrong as I am new to JAVA.
I have already referred:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
The answers there only pertain to using wrong length indices.
Code:
class abc{
static int n;
static int[] arr=new int[n];
static void print_arr(){
for(int x: arr) System.out.print(x+" ");
}
}
class Main {
public static void main(String[] args) {
abc.n=5;
for(int i=0;i<abc.n;i++){
abc.arr[i]=10;
}
abc.print_arr();
}
}
I want this code to print 10 five times.
One possible way.
class abc{
static int n;
static int[] arr;
static void init(int size) {
arr=new int[size];
}
static void print_arr(){
for(int x: arr) System.out.print(x+" ");
} }
class Main {
public static void main(String[] args) {
abc.n=5;
init(abc.n);
for(int i=0;i<abc.n;i++){
abc.arr[i]=10;
}
abc.print_arr();
} }
Better way
class ABC{
private int size;
private int[] arr;
ABC(int n) {
size = n;
arr = new int[n];
}
public void print_arr(){
for(int x: arr)
System.out.print(x+" ");
}
public int getSize() {
return size;
}
public int[] getArray() {
return java.util.Arrays.copyOf(arr,arr.length);
}
public void setArray(int [] array) {
arr = array.clone();
} }
class Main {
public static void main(String[] args) {
int size = 5;
ABC abc = new ABC(size);
int [] array = new int[size];
for(int i=0;i<abc.getSize();i++){
array[i]=10;
}
abc.setArray(array);
abc.print_arr();
} }
class abc
{
static int n=5;
static int[] arr=new int[n];
static void print_arr()
{
for(int x: arr) System.out.print(x+" ");
}
}
class Main
{
public static void main(String[] args)
{
for(int i=0;i<abc.n;i++)
{
abc.arr[i]=10;
}
abc.print_arr();
}
}
In your case ArrayIndexOutOfBounds exception occurs because you are trying to initialize the array by a variable which has not been initialized yet. So either initialize n with a value before array initialization or use dynamic sized array.
Hey i want to write out table in second method.
In first i changed int x into table(each digit in other array index) and in second method i want to write out the table. How to do it ?
int tab [] =new int [4];
public int[] change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());
return tab;
}
public void writeout(int a[]) {
this.tab=a;//how to connect tab from change() with int a[]
for( int i=0;i<=3;i++) {
System.out.println(a[i]);
}
You can use newest Java 8 API to print your array. Your code may look like this:
import java.util.Arrays;
public class App {
int tab[] = new int[4];
public int[] change(int x) {
tab[0] = x/1000;
tab[1] = (x/100)%10;
tab[2] = (x/10)%10;
tab[3] = x%10;
return tab;
}
public void writeout(int array[]) {
Arrays.stream(array).forEach(System.out::println);
}
public static void main(String[] args) {
App app = new App();
app.writeout(app.change(2));
}
}
I fiddled with it. It appears to work after adding a closing brace at the end. tab.toString() doesn't result in sensible results though.
public class MyClass {
public static void main(String args[]) {
MyClass c = new MyClass();
c.writeout(c.change(3));
}
public int[] change(int x) {
int tab [] =new int [4];
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());
return tab;
}
public void writeout(int a[]) {
for( int i=0;i<=3;++i) {
System.out.println(a[i]);
}
}
}
If you want to use class fields, then you could do it like this:
public class MyClass {
public static void main(String args[]) {
MyClass c = new MyClass();
c.change(3);
c.writeout();
}
private int tab [] = new int [4];
public void change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
}
public void writeout() {
for( int i=0;i<=3;i++) {
System.out.println(tab[i]);
}
}
}
What is identifier expected error?
import java.util.Scanner;
class MyClass {
public static void fizzBuzz(Integer)
{
int x=0,n;
System.out.println("give any number");
Scanner Scan = new Scanner(System.in);
int n = Scan.nextInt();
for(x=0;n<x;x++)
{
if(x==3)
{
System.out.println("fizz");
x=x+1;
}
else if(x==5)
{
System.out.println("buzz");
x=x+1;
}
else
{
System.out.println("x");
x=x+1;
}
}
}
}
error
user_file.java:5: error: <identifier> expected
public static void fizzBuzz(Integer)
^
Two changes
1.) You should have if not done already public static void main(String[] args) { // call your method here}
2.) n is declared twice.
3.) public static void fizzBuzz(Integer) is wrong, variable name is missing.
change to public static void fizzBuzz(Integer a)
int x=0,n; and int n = Scan.nextInt();
Here public static void fizzBuzz(Integer)
You have given only Type Integer not the variable which will hold Integer type value.
public static void fizzBuzz(Integer)
app a variable like code below
public static void fizzBuzz(Integer z)
You have declared n tow time int x=0,n; and at int n = Scan.nextInt();
remove int from second declation.
How would I write the toString method? And how could I change the way users can fill numbers?
Here's the code:
public class NArray
{
private int[] intArray;
private NArray[] array;
public NArray(int n, int size, int fillNum)
{
if(n==1)
{
intArray = new int[size];
for(int i=0;i<size;i++)
{
intArray[i]=fillNum;
}
}
else
{
array=new NArray[size];
for(int i=0;i<size;i++)
{
array[i] = new NArray(n-1,size, fillNum);
}
}
}
}
For the fill method, you could use Arrays.fill (see the java API javadocs).
What do you want the toString() method to do? Can't really answer that part without a spec.
To use toString. An Example is shown Below. Please refer and implement..:) Happy Coding
public class Test{
public static void main(String args[]){
Integer x = 5;
System.out.println(x.toString());
System.out.println(Integer.toString(12));
}
}
For a tricky use..:)
class Bank
{
String n;
String add;
int an;
int bal;
int dep;
public Bank(String n,String add,int an,int bal)
{
this.add=add;this.bal=bal;
this.an=an;this.n=n;
}
public String toString()
{
return "Name of the customer.:" + this.n+",, "+"Address of the customer.:"
+this.add +",, "+"A/c no..:"
+this.an+",, " +"Balance in A/c..:"+this.bal;
}
}
public class Demo2
{
public static void main(String[] args)
{
List<Bank> l=new LinkedList<Bank>();
Bank b1=new Bank("naseem1","Darbhanga,bihar",123,1000);
Bank b2=new Bank("naseem2","patna,bihar",124,1500);
Bank b3=new Bank("naseem3","madhubani,bihar",125,1600);
Bank b4=new Bank("naseem4","samastipur,bihar",126,1700);
Bank b5=new Bank("naseem5","muzafferpur,bihar",127,1800);
l.add(b1);
l.add(b2);
l.add(b3);
l.add(b4);
l.add(b5);
Iterator<Bank> i=l.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
I ran my code... there was an error on line 28. it's the one that says
hey.remove(whatnumber);
I can't figure out what's wrong with it. I tried using debug, but
I don't get how to use it.
Here is the code.
import java.util.ArrayList;
import java.util.Scanner;
public class ACSL_Grid_Fit {
public static void main(String args[]) {
run();
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
}
public static ArrayList<Integer> hey = new ArrayList<Integer>();
public static int howmany;
public static int whatnumber;
public static int choice;
public static Scanner sc = new Scanner(System.in);
public static void run() {
input();
countcalc();
}
public static void input() {
{
sc.useDelimiter(", |\n");
howmany= sc.nextInt();
for(int x = 1; x<=howmany;x++) {
whatnumber = sc.nextInt();
hey.remove(whatnumber);
}
}
choice = sc.nextInt();
switch(choice) {
case 1:
int i = 0;
while(i<hey.get(0)) {
i++;
}
System.out.println(i);
hey.remove(i);
case 2:
case 3:
}
}
public static void countcalc() {
}
}
To avoid an IndexOutOfBoundsException, you need to add values to list before you try and run()
And with this code here, you have to keep in mind there is no index 0
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
You are starting the index at 1, so 0 is null;
Edit: I just caught this
You're trying to run() before you even have any values in your list
public static void main(String args[]) {
run();
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
}
Just switch them around
public static void main(String args[]) {
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
run();
}
EDIT: I knew this was a problem to begin with
hey.add(w,w);
Your trying to add at index w when no index w exists. hey has a size of 0, until you add to it.
Just do this.
for(int w=1;w<=25;w++) {
hey.add(w);
}
When you want to remove the numbers use this
hey.remove(whatnumber - 1)
// example, since 20 is at index 19, you want to remove whatnumber - 1