Using Different Methods for Input and Display of an Array - java

public class InputRealNums1
{
static double [] numArr = new double [(int) (Math.random()*100)];
static String res = "";
public static void main(String[] args)
{
inputArray();
displayArray();
}
static void inputArray()
{
for (int i = 0 ; i < numArr.length ; i++)
{
numArr[i] = Math.random()*100;
}
}
static void displayArray()
{
System.out.println(res.inputArray());
}
}
The aim of this code is to seperate the input and the output. The program would generate certain numbers and then it will display them. I just want to know how to seperate the the input and output with the above methods.

You can display that array by using this method.
public static void displayArray()
{
for (int i = 0 ; i < numArr.length ; i++)
{
System.out.println(numArr[i]);
}
}
But this is not a good way. You can do this by passing parameters to the method.
public static void displayArray(Double[] myArray)
{
for (int i = 0 ; i < myArray.length ; i++)
{
System.out.println(myArray[i]);
}
}
public static void main(String[] args){
// some code in here
displayArray(numArr);
}

Related

Printing from a method

public class Simple2DArray {
public static void main(String[] args) {
}
public static void fill()
{
int[][] grid = new int[5][5];
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;r++)
{
grid[r][c] = (int)(Math.random()*100);
}
}
}
public static int biggest(int[][] grid, int big)
{
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;r++)
{
if(grid[r][c]> big)
{
big = grid[r][c];
}
}
}
System.out.println(big);
return big;
}
}
I would like for it to print big. How do I do this? I tried putting System.out.println(biggest(null,big) in the main method but that did not work.
I got solution
But first of all go through https://stackoverflow.com/help/how-to-ask
Your solution
I made some changes in your code.
public class Simple2DArray {
public static void main(String[] args) {
int[][] grid = fill();
System.out.println(biggest(grid, 20));
}
public static int[][] fill()
{
int[][] grid = new int[5][5];
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;c++)
{
grid[r][c] = (int)(Math.random()*100);
}
}
System.out.println("Input 2D");
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;c++)
{
System.out.print(grid[r][c]);
System.out.print("\t");
}
System.out.println("");
}
return grid;
}
public static int biggest(int[][] grid, int big)
{
if (grid != null) {
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] > big) {
big = grid[r][c];
}
}
}
}
return big;
}
}
The following corrections need to be made:
fill needs to return grid.
biggest doesn't need big as a parameter (it makes little sense). It should start out as Integer.MIN_VALUE.
There's no need to print big within biggest.
In your for loop that loops through values of c, you should have c++ instead of r++.
public static int[][] fill()
{
int[][] grid = new int[5][5];
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;c++)
{
grid[r][c] = (int)(Math.random()*100);
}
}
return grid;
}
public static int biggest(int[][] grid)
{
int big = Integer.MIN_VALUE;
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;c++)
{
if(grid[r][c]> big)
{
big = grid[r][c];
}
}
}
return big;
}
Then you can call biggest on an actual two-dimensional array (null isn't an actual two-dimensional array):
public static void main(String[] args)
{
int[][] array = fill();
System.out.println(biggest(array));
}

Printing on same line

I am new to Java and I am trying to print the student numbers and numbers (cijfer in this case) on 1 line. But for some reason I get weird signs etc. Also when I'm trying something else I get a non-static context error. What does this mean and how does this exactly work?
Down here is my code:
import java.text.DecimalFormat;
import java.util.Arrays;
public class Student {
public static final int AANTAL_STUDENTEN = 50;
public int[] studentNummer = new int[AANTAL_STUDENTEN];
public String[] cijfer;
public int[] StudentNummers() {
for (int i = 0; i < AANTAL_STUDENTEN; i++) {
studentNummer[i] = (50060001 + i);
}
return studentNummer;
}
public String[] cijfers(){
for (int i = 0; i < AANTAL_STUDENTEN; i++) {
DecimalFormat df = new DecimalFormat("#.#");
String cijferformat = df.format(Math.random() * ( 10 - 1 ) + 1);
cijfer[i++] = cijferformat;
}
return cijfer;
}
public static void main(String[] Args) {
System.out.println("I cant call the cijfer and studentnummer.");
}
}
Also I'm aware my cijfer array is giving a nullpointer exception. I still have to fix this.
I am not java developer but try
System.out.print
You could loop around System.out.print. Otherwise make your functions static to access them from main. Also initialize your cijfer array.
Besides the things I noted in the comments, your design needs work. You have a class Student which contains 50 studentNummer and cijfer members. Presumably, a Student would only have one studentNummer and and one cijfer. You need 2 classes: 1 for a single Student and one to hold all the Student objects (StudentBody for example).
public class StudentBody {
// An inner class (doesn't have to be)
public class Student {
// Just one of these
public int studentNummer;
public String cijfer;
// A constructor. Pass the student #
public Student(int id) {
studentNummer = id;
DecimalFormat df = new DecimalFormat("#.#");
cijfer = df.format(Math.random() * ( 10 - 1 ) + 1);
}
// Override toString
#Override
public String toString() {
return studentNummer + " " + cijfer;
}
}
public static final int AANTAL_STUDENTEN = 50;
public Student students[] = new Student[AANTAL_STUDENTEN];
// StudentBody constructor
public StudentBody() {
// Create all Students
for (int i = 0; i < AANTAL_STUDENTEN; i++) {
students[i] = new Student(50060001 + i);
}
}
// Function to print all Students
public void printStudents(){
for (int i = 0; i < AANTAL_STUDENTEN; i++) {
System.out.println(students[i]);
}
}
public static void main(String[] Args) {
// Create a StudentBody object
StudentBody allStudents = new StudentBody();
// Print
allStudents.printStudents();
}
}
Just make all your methods and class variables as static. And then you have access to them from main method. Moreover you have got some errors in code:
public class Student {
public static final int AANTAL_STUDENTEN = 50;
// NOTE: static variables can be moved to local ones
// NOTE: only static method are available from static context
public static int[] StudentNummers() {
int[] studentNummer = new int[AANTAL_STUDENTEN];
for (int i = 0; i < AANTAL_STUDENTEN; i++)
studentNummer[i] = 50060001 + i;
return studentNummer;
}
// NOTE: only static method are available from static context
public static String[] cijfers() {
// NOTE: it is better to use same `df` instance
DecimalFormat df = new DecimalFormat("#.#");
String[] cijfer = new String[AANTAL_STUDENTEN];
for (int i = 0; i < AANTAL_STUDENTEN; i++)
// NOTE: remove `i++`, because we have it in the loop
cijfer[i] = df.format(Math.random() * (10 - 1) + 1);
return cijfer;
}
// NOTE: this is `static` method, therefore it has access only to static methods and variables
public static void main(String[] Args) {
String[] cijfer = cijfers();
int[] studentNummer = StudentNummers();
// TODO you can pring two arrays one element per line
for(int i = 0; i < AANTAL_STUDENTEN; i++)
Sytem.out.println(cijfer[i] + '-' + studentNummer[i]);
// TODO as alternative, you can print whole array
System.out.println(Arrays.toString(cijfer));
System.out.println(Arrays.toString(studentNummer));
}
}

How to reverse an array of numbers in java?

import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] xxx={1,3,5,7,9};
System.out.println(Arrays.toString(krakin(xxx)));
}
public static int[] krakin(int[]x) {
for(int i=x.length-1;i>=0;i--) {
int[]dev=new int[x.length-1];
dev[i]=x[i];
}
return dev[i];
}
I'm writing a method in java that reverses the order of the passed array.
I'm getting an error saying void is not allowed in my main method.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] xxx={1,3,5,7,9};
System.out.println(krakin(xxx));
}
public static void krakin(int[]x){
for(int i=x.length-1;i>=0;i--){
}
}
Your krakin method has a void return type, which means it returns nothing. Therefore you can't pass it as an argument to System.out.println.
You can change it, for example, to return an int array:
public static int[] krakin(int[]x){
int[] rev = new int[x.length];
...
return rev;
}
Then you could print it in your main:
System.out.println(Arrays.toString(krakin(xxx)));
With additional libraries it can be done within one line.
If we consider pure Java then I would write like this:
public static void main(String[] args) {
int[] xxx = {1,3,5,7,9} ;
int[] reversed = reverseWithStream(xxx);
int[] reversed2 = reverseWithTempArray(xxx);
Arrays.stream(reversed).forEach(System.out::println);
Arrays.stream(reversed2).forEach(System.out::println);
}
private static int[] reverseWithStream(int[] array) {
return Arrays.stream(array)
.boxed()
.sorted(Collections.reverseOrder())
.mapToInt(value -> value)
.toArray();
}
private static int[] reverseWithTempArray(int[] sourceArray) {
int[] tempArray = new int[sourceArray.length];
for (int i = 0; i < tempArray.length; i++) {
tempArray[i] = sourceArray[sourceArray.length - 1 - i];
}
return tempArray;
}
One of the biggest benefits of this one is the fact that the previously created array is not affected.
In case of:
java.util.Arrays#sort(int[])
Or apache commons methods, the previously created array is affected.
public static void main(String[] args) {
int[] numbers = new int [100];
int j = numbers.length ;
for (int i = 0; i <= 99; i++) {
numbers [i] = numbers [i] + j ;
j = j - 1 ;
System.out.println(numbers [i]);
}

All combination of string char

I have string and i need to print all the combination of the string Char's
Example
For the string "123" the output is:
1,2,3,12,13,21,23,31,32,123,132,213,231,312,321
It must be without loops, only with recursion.
Thanks!
public class CharacterRecursion
{
private String str;
private int counter;
public CharacterRecursion()
{
str = "";
counter = 0;
}
public CharacterRecursion(String str1)
{
str = str1;
counter = 0;
}
public String recurse(String str)
{
if (counter == 15)
{
return ;
}
counter++;
// return (recurse(String str _________) _________) _________;
}
public String [] toString()
{
String [] arr = new String[14];
for (int i = 0; i < 14; i++)
{
arr[i] = this.recurse();
}
return arr;
}
public static void main(String [] args)
{
CharacterRecursion recurse = new CharacterRecursion("123")
System.out.println(recurse.toString);
}
}
I think just giving you the full code would be a little to easy for you. This is the simple set up for the code that you would want. The recurse method is not completely finished, the return statement being one of the things that you will need to fix first. By answer the question, this way, I hope that I am still answering the question, but also still allowing you to fully learn and understand recursion on your one. By the way,
for the public static void main(String [] args) part
You would also put that in a separate class like so:
public class CharacterRecursion
{
private String str;
private int counter;
public CharacterRecursion()
{
str = "";
counter = 0;
}
public CharacterRecursion(String str1)
{
str = str1;
counter = 0;
}
public String recurse(String str)
{
if (counter == 15)
{
return ;
}
counter++;
// return (recurse(String str _________) _________) _________;
}
public String [] toString()
{
String [] arr = new String[14];
for (int i = 0; i < 14; i++)
{
arr[i] = this.recurse();
}
return arr;
}
public class CharacterRecursionClient
{
public static void main(String [] args)
{
CharacterRecursion recurse = new CharacterRecursion("123")
System.out.println(recurse.toString);
}
}
That would work just as well if you are required to have a client class. I hope that this help and cleared up at least a couple of things.

Keep getting an error insert Variable Declaration to complete VariableDeclaration on my Java program

Keeping getting errors and I am new to Java. I keep getting errors about Variable Declaration needed. Advice?
public class Trying
{
public static void main(String[] args)
{
nestedFor;
int i =0;
}
public static void nestedFor(int)
{
int i = 0;
int h =0;
for (int = i; i<=4; i++)
for (int = h; i <=6; h++)
System.out.println ("Testing 1,2,3");
}
}
If you mean showing "Testing 1,2,3" 5×7=35 times, your code should look like this:
public class Trying
{
public static void main(String[] args)
{
nestedFor();
}
public static void nestedFor()
{
int i = 0;
int h =0;
for (i=0; i<=4; i++)
for (h=0; h <=6; h++)
System.out.println ("Testing 1,2,3");
}
}

Categories

Resources