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
Related
public class Main {
public static void main(String[] args) {
print(10);
}
static void print(int s)
{
if (1==s) {
System.out.print(s);
}
System.out.print(s);
print(s-1);
}
}
But I want output like this:
12345678910
You existing recursion never ends. You should only make the recursive call if s >= 1.
And to print the numbers in increasing order, you need to first make the recursive call and then print the current number:
static void print(int s)
{
if (s >= 1) {
print(s-1);
System.out.print(s);
}
}
Here, try this.
public static void main(String[] args) {
print(1, 10);
}
static void print(int startValue, int endValue)
{
System.out.print(startValue);
if(startValue < endValue)
print(startValue+1, endValue);
}
Solution 1:- If you want the end result-oriented then you can try this
public static int doCalculaton(int numVal, int endVal) {
System.out.println(endVal - numVal);
if (numVal == 0) {
return numVal;
}
return doCalculaton(numVal - 1, endVal);
}
public static void main(String[] args) {
int toBeCalculateNum = 10;
doCalculaton(toBeCalculateNum, toBeCalculateNum);
}
Solution 2:- If you want to use the result after calling
public static int doCalculaton(int numVal, ArrayList<Integer> numerList) {
numerList.add(numVal);
if (numVal == 0) {
return numVal;
}
return doCalculaton(numVal - 1, numerList);
}
public static void main(String[] args) {
ArrayList<Integer> numerList = new ArrayList<>();
doCalculaton(10, numerList);
Collections.reverse(numerList);
System.out.println(numerList);
// TODO loop and do whatever you want to do
}
I wanted to create a simple program for user to insert 3 strings to a private string array in a class and then print it back by creating a new object using object reference but I think I am facing problem in the setter/getter.(Pretty new to class and setter/getter) Here is what I have so far:
import java.util.Scanner;
public class Stringtest {
public static void main(String[] args)
{ Scanner input=new Scanner(System.in);
Stringer Strung=new Stringer();
System.out.println("Strings:"+Strung.print());
}
}
class Stringer
{ Scanner input=new Scanner(System.in);
private String[] aa=new String[3];
aa[0]="zero";
aa[1]="one";
aa[2]="two";
Stringer()
{}
{ System.out.println("Please enter 3 strings:");
for(int i=0;i<4;i++)
{
aa[i]=input.next();
}
}
public void setaa(String[] a)
{
aa=a;
}
public String[] getaa()
{
return aa;
}
public void print(String[] a)
{
for(int b=0;b<4;b++)
{
System.out.printf("%s",a[b]);
}
}
}
Due to populating the array while creating a class instance, you don't require any setters. The only getter requires.
Divide the logic from the runner.
Always use array.length() while looping or use a simple for loop otherwise you'll be getting an indexOfBoudException error.
Didn't get why you are using printf() while printing results.
My solution:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
App.run();
}
private static void run() {
Stringer stringer = new Stringer();
stringer.print(stringer.getStrings());
}
}
class Stringer {
private String[] strings = new String[3];
Stringer() {
System.out.println("Please enter 3 strings:");
for (int i = 0; i < 4; i++) {
Scanner scanner = new Scanner(System.in);
strings[i] = scanner.next();
}
}
String[] getStrings() {
return strings;
}
void print(String[] strings) {
System.out.println("Strings are:");
for (String string : strings) {
System.out.println(string);
}
}
}
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]);
}
}
}
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 am having to write a program that loads the first 10 integers' into an array. I've tried it every way and have not been able to make it work with an array. This is as far as I have gotten.
public class midtermcube2 {
public static void main(String[] args) {
int totz;
for (int numz=0; numz<=9;numz++)
{
totz=numz*numz*numz;
System.out.println(totz);
}
}
}
try this:
public class AddArray
{
public static void main(String[] args)
{
int arr[]=new int[10];
for(int i=0;i<10;i++)
{
arr[i]=i;
System.out.println(arr[i]);
}
}
}