Wondering how to direct out into array then output from array - java

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]);
}
}
}

Related

Why does this simple code throw ArrayIndexOutOfBoundsException?

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.

Write out the table in other method

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]);
}
}
}

Getting Certain Method Into Main Method in Java

I've written up a code with several different methods. I can't seem to find the correct way to get the last method I have into the main method so it can print out the correct output.
CODE:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
int num = in.nextInt();
} // this is my main method
public void results (int num) {
for (int i = 1; i < num; i++) {
System.out.print(space(num - i));
System.out.println(method1(i));
}
for (int i = 0; i < num; i++) {
System.out.println(method2(num-i));
System.out.print(space(i));
}
} //this is the method that I want inside my main method
I thought I could simply put System.out.println(results(num)); into my main method but that doesn't work. Can anyone explain what I'm doing wrong and help me with this problem?
Your main method is static, but your results method is not. Either make results be static, or declare a new instance of your class to use inside of main.
public class MyClass
{
public static void main(String[] args) {
results(1);
}
public static void results (int num) {
}
}
or
public class MyClass
{
public static void main(String[] args) {
new MyClass().results(1);
}
public void results (int num) {
}
}
You can't pass what results returns to anything such as System.out.println, because results has a void return type. But results already is printing out information, so just call results.
results(num);

Java Grid fit problems in input

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

Syntax error on token ";", "{" expected after this token

Just a simple class calls a class that prints an array. I get a syntax error in Eclipse. I also get an error that I don't have a method called Kremalation.
public class AytiMain {
public static void main(String[] args) {
AytiMain.Kremalation();
}
}
public class Kremalation {
String[] ena = { "PEINAW", "PEINOUSA", "PETHAINW" };
int i; // <= syntax error on token ";", { expected after this token
for (i = 0; i <= ena.lenght; i++)
System.out.println(ena[i]);
}
}
You have code (which is not declaring a variable and/or initializing it) ouside a method, which is:
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
In Java, code MUST reside inside a method. You can't call a class, you have to call a method that is declared inside a class.
WRONG:
class ClassName {
for (...)
}
CORRECT:
class ClassName {
static void method() {
for (...)
}
public static void main(String[] args) {
ClassName.method();
}
}
You can not define method as class.
It should be
public static void kremalation()
{
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
public class AytiMain {
public static void main(String[] args) {
AytiMain.Kremalation();
}
public static void Kremalation() {// change here.
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
}
Two possible answers.
1) Remove public from second one if you would like to define it as class.
2) Move Kremalation inside closing brace and replace class with void and make it as static method.
You cannot have executable code directly inside a class.. Add a method and use instance of that class to call that method..
public class Kremalation {
public void method() {
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
}
Now, in your main method, write: -
public static void main(String[] args) {
new Kremalation().method();
}
Two approaches to solve this problem.....
1st there are 2 classes in the same file :
public class AytiMain {
public static void main(String[] args) {
new Kremalation().doIt();
}
}
class Kremalation {
public void doIt(){ // In Java Codes should be in blocks
// Like methods or instance initializer blocks
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
}
2nd change the class to method :
public class AytiMain {
public static void main(String[] args) {
AytiMain.Kremalation();
}
public static void Kremalation() { // change here.
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
}

Categories