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]);
}
}
}
Related
I want to get increasing value starting with 1 so that I get TK-1 all the way to TK-n.
What I've tried is:
public class Main {
addTicket();
public void addTicket() {
int a;
String ticket;
a = getA();
ticket = "TK-"+ a
System.out.println(ticket)
}
public int getA() {
int a, b;
a = 0;
b = a++;
return a;
public static void main(String[] args) {
new Main();
}
}
Sorry it's my first time learning to code, can someone explain to me why it isn't working and what should I do to make it work ?
Thanks in advance.
If you need an autoincrease ID, you need a static int:
public class Ticket {
static int ticketID = 1;
public void addTicket() {
int a;
String ticket;
a = getticketID();
ticket = "TK-" + a;
System.out.println(ticket);
}
public int getticketID () {
return ticketID++;
}
public static void main(String[] args) {
Ticket test = new Ticket();
test.addTicket();
test.addTicket();
test.addTicket();
}
}
output:
enter image description here
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.
I am trying to get multiple inputs in a single code of line..
for example in c++, we could have it like -
int a,b,c;
cin>>a>>b>>c;
is it possible in java also??
You can use an array for this purpose, like:
public static void main(String[] args) {
int[] values = new int[3];
Scanner in = new Scanner(System.in);
for(int i = 0; i < values.length; i++) {
values[i] = in.nextInt();
}
System.out.println(Arrays.toString(values));
}
UPDATE 2
In java 8 the above solution can have a shorter version:
Scanner in = new Scanner(System.in);
Integer[] inputs = Stream.generate(in::nextInt).limit(3).toArray(Integer[]::new);
UPDATE 1
There is another way, which is closer to cin:
public class ChainScanner {
private Scanner scanner;
public ChainScanner(Scanner scanner) {
this.scanner = scanner;
}
public ChainScanner readIntTo(Consumer<Integer> consumer) {
consumer.accept(scanner.nextInt());
return this;
}
public ChainScanner readStringTo(Consumer<String> consumer) {
consumer.accept(scanner.next());
return this;
}
}
public class Wrapper {
private int a;
private int b;
private String c;
public void setA(int a) {
this.a = a;
} /* ... */
}
public static void main(String[] args) {
ChainScanner cs = new ChainScanner(new Scanner(System.in));
Wrapper wrapper = new Wrapper();
cs.readIntTo(wrapper::setA).readIntTo(wrapper::setB).readStringTo(wrapper::setC);
System.out.println(wrapper);
}
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();
}
}
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