I don't know what's wrong with this code:
class A{
private int i,j;
public void get(int i, int j)
{
this.i=i;
this.j=j;
}
public void show()
{
System.out.println(i + " " + j);
}
}
public class App {
public static void main(String[] args) {
A[] c = new A[11];
for(int i=0; i<10; i++)
{
c[i].get(i, i);
}
for(int j=0; j<10; j++)
{
c[j].show();
}
}
}
can anyone please tell me the right way to call the get() method??
thanks in advance...
for(int i=0; i<10; i++)
{
c[i] = new A();
c[i].get(i, i);
}
You had an empty array. You need to fill that array with elements
Related
I am working on a selection sort in Java. It will prompt a user to enter in 10 names and then sort them alphabetically. It is sorting some of them but not completely and cannot figure out why it is sorting some values and not others. I believe I have implemented the sort and swap correctly but I feel as though I am missing something. Any help is appreciated as always.
import java.util.*;
public class sortingProgram {
static String studentName[] = new String[10];
static int i;
static Scanner scnr = new Scanner(System.in);
public static void enterNames() {
for (i = 0; i < studentName.length; i++) {
System.out.println("Please enter a student name: ");
studentName[i] = scnr.nextLine();
}
}
public static void sortNames(String sortArray[]) {
int smallindex;
for (int i = 0; i < sortArray.length; i++) {
smallindex = i;
for (int j = i + 1; j < sortArray.length; j++) {
if (sortArray[j].compareTo(sortArray[smallindex]) < 0) {
smallindex = j;
if (smallindex != i)
swap(sortArray, smallindex, i);
}
}
}
System.out.println(Arrays.toString(sortArray));
}
public static void swap(Object a[], int i1, int j1) {
Object temp = a[i1];
a[i1] = a[j1];
a[j1] = temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
enterNames();
sortNames(studentName);
}
}
You are doing your swap too early. You need to wait until the smallest has been determined before swapping. You are swapping whenever you detect a smaller one.
public static void sortNames(String sortArray[]) {
for (int i = 0; i < sortArray.length; i++) {
int smallindex = i;
for (int j = i + 1; j < sortArray.length; j++) {
if (sortArray[j].compareTo(sortArray[smallindex]) < 0) {
smallindex = j;
// Move this.
//if (smallindex != i)
// swap(sortArray, smallindex, i);
}
}
// To here.
if (smallindex != i)
swap(sortArray, smallindex, i);
}
}
public static void swap(Object a[], int i1, int j1) {
Object temp = a[i1];
a[i1] = a[j1];
a[j1] = temp;
}
private void test() {
String[] names = {"C", "B", "A", "9"};
System.out.println(Arrays.toString(names));
sortNames(names);
System.out.println(Arrays.toString(names));
}
use below changes in your program.
import java.util.*;
public class SortingProgram {
static String studentName[] = new String[10];
static int i;
static Scanner scnr = new Scanner(System.in);
public static void enterNames() {
for (i = 0; i < studentName.length; i++) {
System.out.println("Please enter a student name: ");
studentName[i] = scnr.nextLine();
}
}
public static void sortNames(String sortArray[]) {
int smallindex;
for (int i = 0; i < sortArray.length; i++) {
smallindex = i;
for (int j = i + 1; j < sortArray.length; j++) {
if (sortArray[j].compareTo(sortArray[smallindex]) < 0) {
smallindex = j;
//if (smallindex != i) No need here
//swap(sortArray, smallindex, i);
}
}
//place your swaping code here
if (smallindex != i)
swap(sortArray, smallindex, i);
}
System.out.println(Arrays.toString(sortArray));
}
public static void swap(Object a[], int i1, int j1) {
Object temp = a[i1];
a[i1] = a[j1];
a[j1] = temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
enterNames();
sortNames(studentName);
}
}
after making these changes your program works. your code not works properly because you perform swap early.
So i have a 2d array that i create in a tester class and then i am attempting to send it and create a duplicate in the constructor, but a get a null error. Where am i going wrong?
The constructor:
public TheaterSeatSeller(int[][] newSeats)
{
for(int i=0; i<newSeats.length; i++)
{
for(int j=0; j<newSeats[i].length; j++)
{
seats[i][j]=newSeats[i][j];
}
}
}
and then the tester class
public static void main(String[] args){
//initialize the available seats
int[][] emptySeats = {
{10,10,10,10,10,10,10,10,10,10},
{10,10,10,10,10,10,10,10,10,10},
{10,10,10,10,10,10,10,10,10,10},
{10,10,20,20,20,20,20,20,10,10},
{10,10,20,20,20,20,20,20,10,10},
{10,10,20,20,20,20,20,20,10,10},
{20,20,30,30,40,40,30,30,20,20},
{20,30,30,40,50,50,40,30,30,20},
{30,40,50,50,50,50,50,50,40,30}};
TheaterSeatSeller mySeats = new TheaterSeatSeller(emptySeats);
}
You have to initialize the seats array before assigning the value. This should fix it.
public TheaterSeatSeller(int[][] newSeats) {
seats = new int[newSeats.length][newSeats[0].length];
for (int i = 0; i < newSeats.length; i++) {
for (int j = 0; j < newSeats[i].length; j++) {
seats[i][j] = newSeats[i][j];
System.out.println(seats[i][j]);
}
}
}
I am trying to run my project, but the main class isn't being found. Whats wrong? Here's my code.
public class ArrayPrinter {
public static void printArray(int[] arr) {
int size = arr.length;
System.out.print("[");
for(int i=0;i< size; i++){
System.out.print(arr[i]);
if(i<size-1){
System.out.print(",");
}
}
System.out.println("]");
}
}
You need a main method, not class. See below:
public class ArrayPrinter {
public static void main(String[] args){
//Sample use
int[] arr = new int[2];
arr[0] = 4;
arr[1] = 2;
printArray(arr);
}
public static void printArray(int[] arr) {
int size = arr.length;
System.out.print("[");
for(int i = 0;i < size; i++){
System.out.print(arr[i]);
if (i < size-1){
System.out.print(",");
}
}
System.out.println("]");
}
}
The signature of the main function always looks the same:
public class ArrayPrinter {
public static void main(String[] args) {
// put code here
}
public static void printArray(int[] arr) {
int size = arr.length;
System.out.print("[");
for(int i=0;i< size; i++){
System.out.print(arr[i]);
if(i<size-1){
System.out.print(",");
}
}
System.out.println("]");
}
}
You need to have a main method in order to run a Java application.
The signature of the main method is
public static void main (String[] args)
More info can be found
here
I want to create all combinations with two states(1 and 0).
If i do this with two for-loops, this works. But when I use a self-calling function it doesnt. Does anybody can tell me why, please ?
For-Loops :
public class Counter {
public Counter() {
loop(iter);
for (int i=0; i < 2; i++) {
for (int i2=0; i2 < 2; i2++) {
System.out.println(Integer.toString(i)+Integer.toString(i2));
}
}
}
public static void main(String args[]){
new Counter();
}
}
Self-Calling Function :
class Stringhelper {
public Stringhelper() {
}
public String getstring(String string,int beginning,int ending) {
if (string.length() != 0) {
String newstring="";
for (int iter=Math.abs(beginning); iter < ending && iter < string.length(); iter=iter+1) {
newstring=newstring+Character.toString(string.charAt(iter));
}
return newstring;
}
else {
return "";
}
}
}
public class Counter {
public String abil="";
public int iter=1;
public Stringhelper shelper=new Stringhelper();
public void loop(int iter) {
for (int i=0; i < 2; i++) {
abil=abil+Integer.toString(i);
if (iter==0) {
System.out.println(abil);
abil=shelper.getstring(abil,0,abil.length()-1);
}
else {
loop(iter-1);
}
}
}
public Counter() {
loop(iter);
}
public static void main(String args[]){
new Counter();
}
}
And using the self-calling Function the output is 00,01,010,011 instead of 00,01,10,11
Explaining your code here (ignore abil for now):
public void loop(int iter) {
for (int i=0; i < 2; i++) {
abil=abil+Integer.toString(i);
if (iter==0) {
System.out.println(abil);
abil=shelper.getstring(abil,0,abil.length()-1);
}
else {
loop(iter-1);
}
}
}
When iter is 0 it will print it
In the next iteration, when it is not equal to 0, another loop is created, added to the stack, where it starts again from 0, and prints abil's new stack value.
When you create a new stack it recreates all the variables in temporary storage until the code exits. In this case it keeps creating stacks and never exits. In order to exit a stack, use return.
In summary, you need to learn more about how stacks and recursion work in order to fix your problem.
public void loop(int iter) {
for (int i=0; i < 2; i++) {
if (i==1) {
abil=shelper.getstring(abil,0,iter);
}
abil=abil+Integer.toString(i);
if (iter==4) {
System.out.println(abil);
}
else {
loop(iter+1);
}
}
}
this did the trick
Receiving 5,9,7 as output.. Where as the expected should be only 9..Below is the code:
public class GreatestNoInArray {
public static void main(String[] args) {
int a[]= new int[] {1,2,5,9,7};
int big=a[0];
for (int i=1; i<a.length; i++){
if (big<a[i])
big=a[i];
System.out.println(a[i]);
}
}
}
Please help
for expected answer you need to print big (not a[i]) out side of loop
public class GreatestNoInArray {
public static void main(String[] args) {
int a[]= new int[] {1,2,5,9,7};
int big=a[0];
for (int i=1; i<a.length; i++){
if (big<a[i])
big=a[i];
}
System.out.println(big);
}
}
code should be like this:
public class GreatestNoInArray {
public static void main(String[] args) {
int a[] = new int[] {1,2,5,9,7};
int big = a[0];
for (int i=1; i<a.length; i++){
if (big < a[i])
big = a[i];
//System.out.println(a[i]);
}
System.out.println("Big: " + big);
}
}
This works!