arraylist displays zeros instead of my inputs - java

i have this problem with my array list and i cant seem to solve it. i made sure that it directly stores into the array list but it still displays zeros instead of what i input. I've asked my friends about it and they don't know either. it keeps displaying zeros instead.
i showed this to my friends and they say nothing is wrong with it but i think there is something wrong with it though.
import java.util.*;
public class ArrayListAgain{
public static void main(){
ArrayList<Integer> integers = new ArrayList<Integer>();
ArrayList<Integer> divby2 = new ArrayList<Integer>();
ArrayList<Integer> divby3 = new ArrayList<Integer>();
ArrayList<Integer> divby5 = new ArrayList<Integer>();
int s;
Scanner scan = new Scanner(System.in );
System.out.println("How many integers do you want to enter?" );
s=scan.nextInt();
Integer ints;
for (int i = 0; i < s; i++){
ints = new Integer(scan.nextInt());
integers.add(ints);
if(ints.getInteger()%2 == 0){
divby2.add(ints);
}
if(ints.getInteger()%3 == 0){
divby3.add(ints);
}
if(ints.getInteger()%5 == 0){
divby5.add(ints);
}
}
System.out.println("Integers Entered: " );
for(int a=0; a<integers.size(); a++)
{
System.out.print(integers.get(a).getInteger()+ ",");
}
System.out.println("Divisible by 2:");
for(int a =0; a<divby2.size(); a++){
System.out.print(divby2.get(a).getInteger()+ ",");
}
System.out.println("Divisible by 3:");
for(int a =0; a<divby3.size(); a++){
System.out.print(divby3.get(a).getInteger()+",");
}
System.out.println("Divisible by 5:");
for(int a =0; a<divby5.size(); a++){
System.out.print(divby5.get(a).getInteger()+",");
}
}
}}
//2nd class//
public class Integer(){
private int Number;
public Integer(int x){
this.Number=Number;
}
public void setInteger(){
this.Number=Number;
}
public int getInteger(){
return Number;
}}

This is because the constructor of your self-defined Integer class is wrong.
It should be
private int number;
public Integer(int x){
this.number = x;
}
Also, you don't need to define setter if you already have a constructor.
Also, I would advise you to use the java.lang.Integer class instead of defining your own.

Related

Can't use toString to display array

getting the following error message when trying to use toString to display array:
java.lang.NullPointerException
Here's the code:
import java.util.Scanner;
import java.util.Random;
public class RandomArray {
private int data[];
private int value;
public RandomArray(int x)
{
Random gen = new Random();
int[] data = new int[x];
for (int index = 0; index<x; index ++)
data[index] = gen.nextInt(x);
}
public String toString()
{
String output = "";
for(int i = 0; i<data.length; i++)
{
output +=data[i];
}
return output;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x;
int data;
System.out.println("please enter the number of integers you would like to create an array for");
x = scan.nextInt();
RandomArray table = new RandomArray(x);
table.toString();
From what I can tell this error means the toString is throwing null? But I do not know why that is, can anyone help me out?
You are redeclaring your data array which is hiding the one you are filling.
Random gen = new Random();
int[] data = new int[x]; // remove the int[] declaration
Also, it might be easier if you just did the following:
// your toString method
public String toString() {
return Arrays.toString(data);
}
In response to your question, you can do this.
int[] data; // you did this - leave it alone
// and later you should do this.
public RandomArray(int x) {
Random gen = new Random();
data = new int[x]; // designated as an array above
for (int index = 0; index<x; index ++)
data[index] = gen.nextInt(x);
}
}

When compiling program I get the error 'void' type not allowed here

I'm trying to create a program that asks for 10 integers and puts those numbers into an array of negative, positive, and odd arrays. In the end, I want the program to print out 3 rows of numbers that separate the users 10 numbers into "odd", "even", and negative". When I run this I get "error: 'void' type not allowed here"
import java.util.Scanner;
public class ArrayPractice{
private static void showArray(int[] nums)
{
for (int i=0; i<nums.length;i++)
{
if(nums[i]!=0)
{
System.out.println(nums[i] + " ");
}
}
}
public static void main(String[] args){
int evenArray[] = new int[10];
int evenCount = 0;
int oddArray[] = new int[10];
int oddCount = 0;
int negArray[] = new int[10];
int negCount = 0;
Scanner input = new Scanner(System.in);
for(int i = 0; i<10; i++)
{
System.out.println("Number? " + (i + 1));
int answer = input.nextInt();
if(answer<0)
{
negArray[negCount++] = answer;
}
else
{
if (answer % 2 == 0)
{
evenArray[evenCount++] = answer;
}
else
{
oddArray[oddCount++] = answer;
}
}
}
System.out.println(showArray(evenArray));
System.out.println(showArray(oddArray));
System.out.println(showArray(negArray));
}
}
showArray is void, it does not return anything. And, on inspection, it prints in the method itself. So this
System.out.println(showArray(evenArray));
System.out.println(showArray(oddArray));
System.out.println(showArray(negArray));
should just be
showArray(evenArray);
showArray(oddArray);
showArray(negArray);

Basic arraylist using Fibonacci sequence

I want this program to take an array list "max" that the size is created by the user. Then the program uses the Fibonacci sequence to store all the Fibonacci numbers in the arraylist that are less than the arraylist "max" which is created by the user input.
import java.util.*;
import javax.swing.*;
public class FibonacciArrayList {
public static ArrayList<Integer> Fibonacci (Integer Max){
ArrayList<Integer> A = new ArrayList<Integer>();
int n0;
int n1;
int n2;
for(int i= 0; i = max; i++){
n2= n1 + n0;
system.out.println(n2);
n0=n1;
n1=n2;
return A;
}
public static void main (String[] arg){
Integer max;
String Title = "Fibonacci ArryList";
String data = JOptionPane.showInputDialog(null, "Enter the upper bound", Title, 1);
max = new Integer(data);
ArrayList<Integer> A = Fibonacci(max);
System.out.println("There are " + A.size()+ " Fibonacci numbers less than "+max);
}
}
Modified and simplified the logic to the Fibonacci function. Comments have been added to help you understand the changes.
public static ArrayList<Integer> Fibonacci (Integer max) { //Instead of 'Max' use 'max'
ArrayList<Integer> A = new ArrayList<Integer>();
//Initialize value of n0, n1 and n2
int n0=0;
int n1=1;
int n2=1;
//Handling the base conditions
if(max == 0) return A;
if(max == 1) {
A.add(n0);
return A;
}
//Add first 2 elements in the array
A.add(n0);
A.add(n1);
//A 'while' loop will be more suitable to what you want to achieve
while(n2 < max) {
A.add(n2); //Instead of printing the values, add them into ArrayList A
n0=n1;
n1=n2;
n2 = n1 + n0;
} //Add a closing bracket for the 'for' loop
return A;
}
Try this code.
import java.util.*;
import javax.swing.*;
public class FibonacciArrayList {
public static ArrayList<Integer> Fibonacci (int Max){
ArrayList<Integer> A = new ArrayList<Integer>();
int n0=0;
int n1=1;
int n2;
if(Max==0){
}
else if(Max==1)
{
System.out.println(n0);
A.add(n0);
}
else if(Max==2)
{
System.out.println(n0);
System.out.println(n1);
A.add(n0);
A.add(n1);
}
else{
System.out.println(n0);
System.out.println(n1);
A.add(n0);
A.add(n1);
for(int i= 2; i <=Max; i++){
n2= n1 + n0;
A.add(n2);
System.out.println(n2);
n0=n1;
n1=n2;
}
}
return A;
}
public static void main (String[] arg){
int max;
String Title = "Fibonacci ArryList";
String data = JOptionPane.showInputDialog(null, "Enter the upper bound", Title, 1);
max = Integer.parseInt(data);
ArrayList<Integer> A = Fibonacci(max);
System.out.println("There are " + A.size()+ " Fibonacci numbers less than "+max);
}
}

Iterate through ArrayList

I'm currently trying to iterate through an ArrayList and see if it contains the following numbers I input into the winners array. However, the ticket object won't allow me to utilize the .contains() method which is where I'm getting the error. Any idea on how to work around this?
int[] winners = new int[6];
for(int i = 0; i < winners.length; i++)
{
winners[i] = in.nextInt();
}
in.close();
Scanner scan = new Scanner(file);
ArrayList<Ticket> info = new ArrayList<Ticket>();
for(int i = 0; i < lim; i++)
{
String name = scan.nextLine();
String num = scan.nextLine();
String[] t = num.split(" ");
int[] tichold = new int[t.length];
for(int j = 0; j < t.length; j++)
{
tichold[j] = Integer.parseInt(t[j]);
}
Ticket ticket = new Ticket(name, tichold);
info.add(ticket);
}
**for(Ticket t : info)
{
if(t.contains(winners))
{
System.out.println("Yes");
}
}**
scan.close();
}
**public static class Ticket
{
public String name;
public int[] tarray;
public Ticket(String name, int[] tarray)
{
this.name = name;
this.tarray = tarray;
}**
You can't use a method that doesn't exist for that class. Since you don't have contains defined for Ticket, I'm not surprised that it isn't working.
From inference, winners is an int[]. In that case, you'd define a new method contains inside of Ticket.
public boolean contains(int[] winningNumbers) {
// logic here
}
Depending on how the winning numbers for a given ticket are stored, and given how you define different conditions of winning, you'd handle your logic here.
If they're stored as an array and you want an exact match, then you can use Arrays.equals for that.
public boolean contains(int[] winningNumbers) {
return Arrays.equals(numbers, winningNumbers);
}
Try this Ticket class with and added contains method:
public class Ticket {
public String name;
public int[] tarray;
public Ticket(String name, int[] tarray)
{
this.name = name;
this.tarray = tarray;
}
public boolean contains(int[] winners) {
for (int i = 0; i < winners.length; i++) {
for (int j = 0; j < tarray.length; j++) {
if (winners[i] == tarray[j])
return true;
}
}
return false;
}
}

How to run one Java class and then another Java class in Eclipse?

I know it may be weird question but I'm really stuck. I have simple program with two classes. I need pass array from class A to class B. I did it but I cannot test it because I have no idea how to run program. When I click on the run then only one class started. I wanted test whole program and cannot find anything how to do it. Is there any command or something which say run class A and then class B? Without it I cannot test class B because values from Array (class A) are not loaded :/ Hope you understand what I mean.
I'm using eclipse.
Thanks!
Class MarkCalculator
import java.util.Scanner;
public class MarkCalculator {
public static int[] exam_grade = new int[6];
public static int[] coursework_grade = new int[6];
public static int[] coursework_weight = new int[2];
public static int[] module_points = new int[6];
public static String module_grade, holder;
public static int counter1 = 0, counter2 = 0;
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
for (int i=0; i<3; i++){
System.out.printf(i+1+". Modelue"+" Enter grade of exam:");
while (!input.hasNextInt() ){
System.out.printf("Enter only numbers! Enter grade of your exam: ");
input.next();
}
exam_grade[i]=input.nextInt();
System.out.printf(i+1+". Modelue"+" Enter grade of coursework:");
while (!input.hasNextInt()){
System.out.printf("Enter only numbers! Enter grade of your coursework: ");
input.next();
}
coursework_grade[i]=input.nextInt();
}
computeMark(coursework_grade, exam_grade, module_points);
// calculate module grade
for(int i = 0 ;i < 3; i++){
if (module_points[i] < 35){
System.out.println(i+1+".Module: Fail");
}
else if (module_points[i] >= 35 && module_points[i] <= 40){
System.out.println(i+1+".Module: Pass by compensation");
counter1++;
}
else {
System.out.println(i+1+".Module: Pass");
counter2++;
}
}
holder = computeResult(module_points, counter1,counter2, module_grade);
System.out.println("Your stage result is: "+ holder);
input.close();
}
public static int[] computeMark (int coursework_grade[], int exam_grade[], int module_points[]){
coursework_weight[0]= 50;
coursework_weight[1]= 50;
for(int i=0;i<3;i++)
{
if (coursework_grade[i] < 35 || exam_grade[i] < 35){
module_points[i]=(coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100;
if (module_points[i] > 35){
module_points[i] = 35; }
else {
module_points[i] = 0;
}
}
else {
module_points[i]=((coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100); }
}
return module_points;
}
public static String computeResult (int module_points[], int counter1, int counter2, String module_grade ){
int sum = 0;
double average = 0;
for (int i = 0; i < 3; i++){
sum = sum + module_points[i];
average = sum / 3;
}
for (int i = 0; i < 3; i++){
if (counter2 == 3){
module_grade = "Pass";
}
else if (average >= 40 && counter1 <= 2) {
module_grade = "Pass by compensation";
}
else {
module_grade = "Fail";
}
}
return module_grade;
}
}
Class StudentChart
public class StudentChart {
public static void main(String[] args) {
for (int i = 0; i < 3; i++){
System.out.println(MarkCalculator.coursework_weight);
}
}
}
You only need one main method.
class A {
String s;
public A(String s){
this.s = s;
}
}
public class B {
public static void main(String[] args){
A a = new A("Hello");
System.out.println(a.s + " world!");
}
}
class B will be the application program, the one with the main method. It will get values from class A. class A does not need to run for class B app to work, even though it uses values from class A.
You can have a method with a different name in another class, and call that method from your main method.
Do not call it public static void main though - that should only be used for standalone programs. If the method requires some other code to be run prior to it, it should not be the main method of a Java program.

Categories

Resources