How to fill my array from another class?(Java) - java

This is my program that makes some calculations to a numbers into the array named "initialMarks". However I would like to fill the initialMarks array from another class using scanner.Could you help me to figure it out how to do that? Is it possible to outprint the result array "result" in a third class?
public class testingN
{
public static void main(String[] args)
{
int [] initialMarks = new int [4];
int [] result = new int [6];
result[0] = computedMarks(initialMarks[0], initialMarks[1])[0];
result[1] = computedMarks(initialMarks[2], initialMarks[3])[1];
for(int i=0; i< result.length; i++)
System.out.println(result[i]);
}
public static int [] computedMarks(int mark1, int mark2)
{
int [] i= new int [6];
for (int j = 0; j < i.length; j++)
{
if ((mark1 < 35 && mark2 > 35) || (mark1 > 35 && mark2 < 35))
{
i[j] = 35;
}
else
{
i[j] = (mark1 * mark2);
}
}
return i;
}
}

Your other class can have a method that returns a stream, and you can feed that into your Scanner's constructor.
In your other class's String getInitialMarks() method:
// Generate a string with all the "marks" as "stringWithMarks", separated by "\n" characters
InputStream is = new ByteArrayInputStream( stringWithMarks.getBytes( "UTF-8" ) );
return is;
Then in your first class, otherClass:
Scanner scanner = new Scanner(otherClass.getInitialMarks());
And proceed to read in the marks as if it were user input.

public class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
public static void main(String[] args)
{
Declaring result as class member (global variable) and being static should help your cause
Example: using from another class
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}
Edit: Run the code below Here. You'll see it works just fine.
class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
}
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}

Related

How to fix my my loadData method with my main class

I am trying to load data from a txt file and it will only read one line of the txt file. When I specify what the int I variable is in my for loop within my loadData method it will print that particular line. I am not sure why it won't just add and print all my data.
I tried using an outer for loop to see if would print and add the data that way, but no luck
import java.io.*;
import java.util.*;
public class BingoSortTest
{
static BingoPlayer [] test;
public static void main (String [] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
test = new BingoPlayer [10];
loadData();
System.out.print(Arrays.toString(test));
}
public static void loadData() throws IOException
{
Scanner S = new Scanner(new FileInputStream("players.txt"));
double houseMoney = S.nextDouble();
S.nextLine();
int player = S.nextInt();
S.nextLine();
for(int i = 0; i < test.length; i++)
{
String line = S.nextLine();
String [] combo = line.split(",");
String first = combo [0];
String last = combo [1];
double playerMoney = Double.parseDouble(combo[2]);
BingoPlayer plays = new BingoPlayer(first, last, playerMoney);
add(plays);
}
}
public static void add(BingoPlayer d)
{
int count = 0;
if (count< test.length)
{
test[count] = d;
count++;
}
else
System.out.println("No room");
}
}
Here is the contents of the txt file I am using:
50.00
10
James,Smith,50.0
Michael,Smith,50.0
Robert,Smith,50.0
Maria,Garcia,50.0
David,Smith,50.0
Maria,Rodriguez,50.0
Mary,Smith,50.0
Maria,Hernandez,50.0
Maria,Martinez,50.0
James,Clapper,50.0
Every Time you put a BingoPlayer at Index 0 .
public static void add(BingoPlayer d)
{
int count = 0; // <-------------------- Here
if (count< test.length)
{
test[count] = d;
count++;
}
else
System.out.println("No room");
}
you have to define static counter variable where array of BingoPlayer is defined.
define count variable static
static BingoPlayer [] test;
static int count = 0;
and chane the add function definition like this.
public static void add(BingoPlayer d)
{
if (count< test.length) {
test[count] = d;
count++;
}
else
System.out.println("No room");
}

How to access an array from the main method?

public static int arraylistExample() {
int length [] = new int [10];
length[0] = 2;
length[1] = 3;
length[5] = 8;
return length [1];
}
I have written in the main method this:
System.out.println(length[5]);
Hence, my code looks like this:
import java.util.ArrayList;
public class Javanotes {
public static void main(String[] args) {
System.out.println(length[5]);
}
public static int arraylistExample() {
int length [] = new int [10];
length[0] = 2;
length[1] = 3;
length[5] = 8;
return length [1];
}
}
and I get a "0" what should I do?
1) The array is defined in the method scope of arraylistExample(). It is not visible from main().
2) The array will be empty if you never call the method.
You could change it in this way :
public class Javanotes {
static int length [];
public static void main(String[] args) {
System.out.println(arraylistExample()); // print 3
System.out.println(length[5]); // print 8
}
public static int arraylistExample() {
length = new int [10]; // init the static field
length[0] = 2;
length[1] = 3;
length[5] = 8;
return length [1];
}
}
But note that using static everywhere is generally not the right thing.
You do that for utility classes.
You should declare the function before calling it.
your function arraylistexample is not seen by the main class

How to pass a reference array into a setter?

Write a method called setTo5 which is passed a reference to an array of ints and sets the contents of that array to all 5s. This is what I have so far, and it's giving me an error on the second line after public static void main(...);. How to make a better setter?
public class Set5 {
private static int n = 8;
static int[] boop = new int[n];
public static void main(String[] args){
int[] roop = new int[n];
roop.setTo5(5);
}
public void setTo5(int poop){
for(int i = 0; i<n ; i++){
poop = boop[i];
}
}
}
Try something like this:
public class Set5 {
private static int n = 8;
static int[] boop = new int[n];
public static void main(String[] args){
int[] roop = new int[n];
//Create instance of Set5 class to call setter
Set5 set5reference = new Set5();
set5reference.setTo5(roop);
//All the values in roop will now be 5 as set by setter.
}
//change this method to accept array reference like this
public void setTo5(int[] poop){
for(int i = 0; i<n ; i++){
poop[i] = 5;
}
}
}
To fill an array entirely with some value use:
java.util.Arrays.fill(poop, 5)
In your case:
public class Set5 {
private static int n = 8;
//static int[] boop = new int[n]; // unused variable
public static void main(String[] args){
int[] roop = new int[n];
setTo5(roop);
print(roop);
}
public static void setTo5(int[] poop){
java.util.Arrays.fill(poop, 5)
// for(int i = 0; i<poop.length ; i++){
// poop[i]=5;
//}
}
public static void print(int[] poop){
for(int i = 0; i<poop.length ; i++){
System.out.println("array["+i+"] = "+poop[i]);
}
}
}

Main method not found in class ActivityTime, please define the main method as: public static void main(String[] args)

I tested my program on http://www.hackerearth.com/problem/algorithm/roys-life-cycle/ . However, I always got Error: Main method not found in class ActivityTime, please define the main method as:public static void main(String[] args)
This is my program
import java.util.Scanner;
/**
* Created by DUY on 10/12/2014.
*/
class EatSleepCode {
public static void main(String[] args){
int numberDay = 0;
int numberMinuteOfDay = 18;
Scanner input = new Scanner(System.in);
numberDay = input.nextInt();
input.nextLine();
String[] str = new String[numberMinuteOfDay];
for(int i = 0; i < numberDay; i++){
str[i] = input.nextLine();
}
ActivityTime code = new ActivityTime(numberDay,str,'C');
code.findLongestTime();
}
}
class ActivityTime{
public int longestTimeOfDay;
public int longestTimeOfTotal;
public int numberDay;
public String[] str;
public char act;
public ActivityTime(int numberDay, String[] str, char act){
this.numberDay = numberDay;
this.str = str;
this.act = act;
}
public void findLongestTime(){
int tmp1 = 0, tmp2 = 0;
for(int i = 0; i < numberDay; i++){
tmp1 = 0;
for(int j = 0; j < str.length; j++){
if(str[i].charAt(j) != act){
tmp1 = 0;
tmp2 = 0;
}
else {
tmp1 ++;
tmp2 ++;
}
if(tmp1 > longestTimeOfDay){
longestTimeOfDay = tmp1;
}
if(tmp2 > longestTimeOfTotal){
longestTimeOfTotal = tmp2;
}
}
}
System.out.println(longestTimeOfDay + " " + longestTimeOfTotal );
}
}
Can you help me solve this error? Thank you very much
You ought to separate these classes into two files, one called EatSleepCode.java and one called ActivityTime.java.
Once you've done that, you'll be clearer on which one you're running as your main class. It's EatSleepCode that has the public static void main in it, so presumably that is what you intend as your main class; ActivityTime doesn't have such a method, which is why you can't run that as your main class. That's what the error means that you're getting.

Why does println not show up - Java

I am a newb with Java so don't bite me please..
I've made this method, but it will not show up in the console screen, why?
public class ADSopgave2K1 {
public static void main(String[] args) {
}
public void print(String s, int pos) {
s = "";
pos = s.length();
int count = s.length();
char[] ray;
System.out.println("Enter a word: ");
Scanner userInputF = new Scanner(System.in);
s = userInputF.nextLine();
ray = s.toCharArray();
for (int t = 0; t < s.length(); t++) {
System.out.println(ray[t]);
return;
}
}
}
You did not call that method yet. Try to call your method.
public static void main(String[] args) {
ADSopgave2K1 intance=new ADSopgave2K1();
intance.print();
}
Edit
public void print() {
System.out.println("Enter a word: ");
Scanner userInputF = new Scanner(System.in);
String s = userInputF.nextLine();
char[] ray = s.toCharArray();
for (int t = 0; t < s.length(); t++) {
System.out.println(ray[t]);
}
}
When you run your program, Java will call main(String[] args).
But this is an empty function, so you will not see any output.
Becasue, you did not call anything in main(String[] args) method. Make your print method static and call this to your main method.
public static void print(String s, int pos){
}
EDIT:
public static void main(String[] args){
print("test",1);
}
ADSopgave2K1 r=new ADSopgave2K1();
r.print("jai", 4);
Create an object of your class in side main method and then call its method.
You should call print() method.
public class ADSopgave2K1 {
public static void main(String[] args)
{
print("Hello World", 1);
}
public void print(String s, int pos)
{
s = "";
pos = s.length();
int count = s.length();
char[] ray;
System.out.println("Enter a word: ");
Scanner userInputF = new Scanner(System.in);
s = userInputF.nextLine();
ray = s.toCharArray();
for (int t = 0; t < s.length(); t++) {
System.out.println(ray[t]);
return;
}
}
}
You have to call your method inside main() by creating instance for your class
either you make print method static and call it with correct argument
Or
make instance of ADSopgave2K1 class and call it with correct args

Categories