In Java Can Do Arrays With Variables?

Apr 7, 2014

So like, in lua programming language you can do things like,

Array = {1, 2, 3, abc = 5, efg = {123, 456, 789, hij = {"tests","works!"}}, hij = true}

Array[1] = 5

Array[3] = true

Can you do atleast something like this in java or?

I would like to do this because if let's say I was making a game, I could define what tiles are passable and which are not and then their location or something, so like this:

//p (passable) stands for if possible to walk on
//c stands for tile image
t = ["grass.png","water.png","chest.png"]
Tiles = [
[p = false, c = t[1], x = 3, y = -2 ],
[p = true, c = t[0], x = 4, y = 3 ],

[Code] ....

Or something...

View Replies


ADVERTISEMENT

Trying To Initialize Arrays - Assigning Variables

Apr 1, 2014

I'm making a card game and have a class I used to assign all my card variables.

However, it's giving me multiple errors and I can't seem to be able to access the variables from my other classes.

Java Code:

package com.summoners.Screen;
class Cards {
public static String[] names;
name = new String[1000];
atk = new int[1000];

[Code] .....

View Replies View Related

Why Does String Split Method Producing Arrays With Whitespace Variables

Jan 17, 2015

I used the string split method in the following lines of code like this:

String[] numbers = out.split("[^d.]", 0);
String[] operations = out.split("[.d()]", 0);

When out is equal to the String "2x2.5", the array operations ends up looking like this when it is printed using the toString method:

[, , , x]

As you can see, before the array element x, there are three String variables which only contain whitespace. Why does this occur, and how can I prevent this from happening?

View Replies View Related

What Is The Difference Between Regular Arrays And Multi Dimensional Arrays

Jun 28, 2014

I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.

View Replies View Related

Local Variables In Java

Jan 11, 2014

you can also refer this link Local variables in java?Local variables in java?To meet temporary requirements of the programmers some times..we have to create variables inside method or bock or constructor such type of variables are called as Local Variables.

----> Local variables also known as stack variables or automatic variables or temporary variables

----> Local variables will be stored inside Stack.

-----> The local variables will be created while executing the block

in which we declared it and destroyed once the block completed. Hence the scope of local variables is exactly same as the block in which we declared it.

package com.javatask.in;
class A{
public static void main(String args[]){
int i=0; // Local variable

[code]....

View Replies View Related

Int Variables And Printf In Java

Mar 22, 2014

I write the following statement:

int num1 = (int)( 1000 * ( generator.nextFloat() ) );
System.out.printf("%d", num1);

and I get an error!

The weirdest thing is that 'num1' does NOT show in variables window. How can it be?

View Replies View Related

Final Reference Variables In Java

Feb 28, 2015

I am unable to understand the meaning of this sentence "final reference variables must be initialized before the constructor completes.",What is trying to imply?

View Replies View Related

Java - Private Arrays / Integers?

Jan 16, 2015

I have a project for a class where I'm supposed to be something with private arrays and private integers and I still don't understand the point of private anything within java. if I want to change a variable I'll change it. if I want it to stay the same I'll leave it the same. so what point is all this private/public nonsense unless I'm trying to stop hackers or something?

View Replies View Related

Java Arrays - Count Above / Below Average

May 7, 2014

I have to write a program that takes an array of grades and calculate the average and then count the number of grades that are above average and also count the number of grades that are below average. This is what I have so far, it calculate the average and tells me how many are above average... where and how do I get to count how many are below average

public class arrays{
public static void main(String[] args) {
final int grades = 5;
double[] numbers = new double[grades];
double sum = 0;

[Code] .....

View Replies View Related

Reverse Numbers Using Java Arrays

Feb 27, 2014

I am trying to do this assignment but I can't get the needed output. Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.

Program is written to a class called ReverseNumbers.

Example output

How many floating point numbers do you want to type: 5
Type in 1. number: 5,4
Type in 2. number: 6
Type in 3. number: 7,2
Type in 4. number: -5
Type in 5. number: 2

Given numbers in reverse order:
2.0
-5.0
7.2
6.0
5.4

My code:

import java.util.Scanner;
public class apples {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double[] numbers;
System.out.print("How many floating point numbers do you want to type: ");

[Code] .....

View Replies View Related

Java Applet - Sharing Variables With Another Class

Mar 29, 2015

I have An Issue With My Java Applet. Im Trying To Share My Variables With Another Class, But Its Not Working.

Class 1

package com.Tobysmith10.game.main;
import java.applet.Applet;
import java.awt.Graphics;
public class Game extends Applet{
public void init(){
 setSize(850,480);
 public void paint(Graphics g){
 g.fillOval(x,y,20,20);
}
}

Class 2

package com.Tobysmith10.game.main;
import java.applet.Applet;
public class gameLoop extends Applet implements Runnable{
public int x, y;
public void run(){
while(true){
x = 100;
y = 100; 
}
}
}

So im sharing the x and y variables with the Class 1 , but I get yellow lines under it and when i run the program, it crashes, how do I get class 1 to recognize the variables from class 2 ?

View Replies View Related

Java Int Short And Byte Variables Are Same Thing?

Mar 15, 2015

okay so it says that java int short and byte variables are the same thing. They take whole numbers. But what is the point of byte and short to even exist if int covers it all? Is the short and byte just for fun?

View Replies View Related

Getting Error On Java Code Using Parallel Arrays

Dec 1, 2014

This is my code.

public class PA9 {

public static void main(String[] args) throws FileNotFoundException {
// create data file to read from
File inf = new File("cityPopulationData.txt");
//Create a file to write out to.
PrintWriter fileOut = new PrintWriter("output.txt");

[Code] .....

This is my error

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at PA9.getData(PA9.java:35)
at PA9.main(PA9.java:22)

View Replies View Related

Getting Error With Java Code - Using Parallel Arrays

Dec 1, 2014

Write a Java program to read from the data file, find the city with the highest population based on the 2010 census data, the city with the highest population growth from 2010 to 2013, the city with the lowest population growth from 2010 to 2013, and the city with the highest density (number of persons per sq. mile).

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

[code]....

View Replies View Related

Preferential Treatment To Order In Which Variables Are Declared In Java

Jun 25, 2014

public class Ball {
private int a=show();
int b;
Ball()
{
b=20;
}
public static void main(String args[])throws Exception {
System.out.println(new Ball().a);
}
private int show()
{System.out.println(b);
return b;
}
}

I wanted to know when are the objects created ? when are they initialized? and how is show() getting called without any reference or "this" ?

View Replies View Related

Java Arrays - Entering Lists Only Let To Enter 2 Numbers?

Oct 9, 2014

I am trying to write a code that allows you to input 2 lists and it tells you if they are identical or not. However, when I enter my fist list I can only enter two values and then it asks for the next list. How would I fix this to allow me to enter more than two values?

Also, If the second list is different it has me enter more values that list one.

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
  // Enter values for list1
System.out.print("Enter list1: ");
int size1 = input.nextInt();
int[] list1 = new int[size1]; 
for (int i = 0; i < list1.length; i++)
list1[i] = input.nextInt();
  // Enter values for list2
System.out.print("Enter list 2: ");

[code]...

My output looks like this:

Enter list1: 1
2
Enter list 2: 2
3
2

Two lists are not identical

View Replies View Related

Adding Data File Into Two Separate 2D Arrays In Java

Apr 27, 2014

I am having some trouble with a Java program. I have a txt data file, which I will display, that I need to add into two separate arrays. The text file is 8 lines long that is supposed to go into two separate 4x4 matrices. A little background info on this program, reads in two arrays, compares them and outputs the largest elements of the same index and outputs them in a separate array. I somehow cannot seem to figure out how to do this. My code is below:

Data File:

2 7 6 4
6 1 2 4
9 7 2 6
8 3 2 1
4 1 3 7
6 2 3 8
7 2 2 4
4 2 3 1

Code:

public class prog465a {
public static void main(String[] args) {
Scanner inFile = null;
try {
inFile = new Scanner(new File("prog465a.dat.txt"));

[Code] .....

View Replies View Related

Java Program That Specifies Three One-dimensional Arrays Names Price / Amount And Total

Dec 2, 2014

Recently I have missed a few weeks of my class due to family emergencies. Because of this I have missed very important lectures. My professor has assigned me the following assignment:

Prices Write a Java program that specifies three one-dimensional arrays names price, amount, and total. Each array should be capable of holding 10 elements. Using a loop, input values for the price and amount arrays. The entries in the total array should be the product of the corresponding values in the price and amount arrays. After all the data have been entered, display the following output:

Total Price Amount

==== ==== ======

Under each column heading display the appropriate value.

I know how to display everything using printf, how to create the actual arrays and define the array size, but I am confused on how and what loop to use and how to construct it. This is what I have managed to write up so far:

import java.util.Scanner; // Imports the Scanner class
import java.text.DecimalFormat; // Imports the DecimalFormat class

public class Prices
{
public static void main(String [] args)
{
double[] price = new double[10];
double[] amount = new double[10];
double[] total = new double[10];

// Create a new Scanner object
Scanner keyboard = new Scanner(System.in);
} //End of main method
} // End of public class

View Replies View Related

Inheritance In Java - Child Class Get Copy Of Methods And Variables Of Parent Class?

Mar 1, 2015

Does child class gets a copy of the methods and variables of parent class?

public class test1 {
public static void main(String a[]) {
Child c = new Child();
c.print();

[Code] ....

why is the output 1?

View Replies View Related

Comparing Two Variables

Mar 30, 2014

Let's get it out of the way -- I'm stumped. On something that should be pretty simple to solve. Code follows.

public class Sum {
public static void main(String [] args) {
Double nSum = 0.0;
Double aDouble = 100.0;
for (int i = 0; i < 1000; i++){
nSum += 0.1;

[Code] ....

The output:

100.000000
100.000000
false
false
false
false
false

Process finished with exit code 0

I wrote another simple program and hardcoded the values: aDouble = 100.0; bDouble= 100.0000

Using aDouble.equals(bDouble) returns true, just as one would expect.

So what am I overlooking?

View Replies View Related

JSF :: How To Compare Two Variables

Feb 25, 2014

Having two values

<c:set var="var1" scope="view" value="#{ID1}"/>
<c:set var="var2" scope="view" value="${ID2}" />

I tried in <c:if test=${var1 == var2}>

and eq also

above condition is not working. Both are same object. How to check?

View Replies View Related

Variables In While Construct

Apr 8, 2014

New to java/programming and i cant understand why the pen variable does not display the the correct value ... For example for input 1 ; 2 ; 3 ; 4 both variables will display 10 and i dont understand why pen does not have the value 6 .

import acm.program.*;
public class Chap4_ex12 extends ConsoleProgram {
public void run () {
int pen = 0;
int r = 1;
int sum = 0;
while (r !=SANTINEL) {
r = readInt(" ? ");
pen=sum ;

[code].....

View Replies View Related

Variables In Interface

Jan 21, 2015

Variables defined in interface are public static and final so I was thinking that we should not be able to override the variables in a class thats implementing the interface. But when I am compiling the below class, it compiles fine and gives the correct values. but when I did disp.abhi = 35; it gives a compile error (cannot override final variable)

interface display{
int abhi = 10;
void displayName();

[code]....

View Replies View Related

C++ Passing By Value With Two Variables

Jun 24, 2014

This is my first time working with C++ and I have put together this program and came up with two errors and I am unsure what it is wanting me to do. The errors I got are:

1>c:usersownerdocumentsvisual studio 2010projectsweek5week5passing_by_value.cpp(30): error C2064: term does not evaluate to a function taking 1 arguments
1>c:usersownerdocumentsvisual studio 2010projectsweek5week5passing_by_value.cpp(38): error C2064: term does not evaluate to a function taking 1 arguments

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
//initialize arrays
int incr10(int* numa,int* numb);

[Code] ....

View Replies View Related

How To Set Environment Variables

Apr 13, 2014

I am new in this programming language, java. I have a problem after I set my path ";C:Program Files (x86)Javajdk1.7.0_51in". I made a simple program but an error occurred. Here's the screenshot.....

Attached image(s)

View Replies View Related

Variables Not Have Been Initialized

Sep 23, 2014

I continuously get an error for lines 34, 36, and 37 saying that the variables may not have been initialized.

import java.util.Scanner;
import java.util.Random;
public class MathTutor {
public static void main(String[] args) {
Random r = new Random ();
Scanner input = new Scanner (System.in);
/*int min=1;
int max=10;*/
int num1,num2,operation;
int n1= r.nextInt((9+1)+1);
int n2= r.nextInt((9+1)+1);
operation= r.nextInt(3);
int correctAnswer;
int userAnswer;

[code]....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved