Program To Initialize And Display Variable Size Array

Aug 18, 2014

Write a program to initialize and display variable size array.

View Replies


ADVERTISEMENT

How To Initialize Variable Only Once

May 2, 2015

I'm working on a program where the user inputs song information (name, artist, filesize, duration) which is stored in a database. Once the user adds a song, the details should be stored in the first empty song slot (4 song slots total) and then taken back to the menu interface.

What I'm having trouble with is that I can add the first song fine (song1), but when I try to add another song, the information entered into song1 is gone. I've worked out that this is because every time I call upon the addNewSong() method, I'm creating four Song objects that are brand new with the line:

Song song1 = new Song();
Song song2 = new Song();
Song song3 = new Song();
Song song4 = new Song();

How to fix this problem is where I'm stuck because if I move these lines elsewhere, I get a 'cannot find symbol - variable song1' error.

Here is my code:

public class SongDatabase {
Scanner console = new Scanner(System.in);
public void addNewSong() {
Song song1 = new Song();
Song song2 = new Song();
Song song3 = new Song();
Song song4 = new Song();
if (song1.isEmpty()) {

[code]....

View Replies View Related

Switch Statements To Initialize Variable Due To Multiple Outcomes?

May 20, 2014

After this problem is fixed, my math game will run. The answer variable is supposed to be equal to the cases in the second switch statement by random but I get errors whenever I try to initialize it this way. I have been writing this program for almost 5 days now and it is finally wrapping up. Why I can't map the answer variable to the second switch statement the way I want to?

package pkgnew;
import java.util.Scanner;
import java.util.Random;
public class New {
  public static void main(String args[]) {
//Declare and construct variables
Random randomnum = new Random();
int fnum, snum, answer;

[Code] .....

View Replies View Related

Write A Program To Create Integer Array Of Size 20

Aug 2, 2014

Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

Then, the final array should be printed as output. There is no user input for this program.Your program must include, at least, the following methods:

-insertNumbers, which will take as input one integer array and store the random numbers in it.
-createTremor, which will generate the random number as the location and return it.

A sample run of the program is shown below:

Sample output #1:
Array:1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
Random position: 5
Final Array:1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1

View Replies View Related

How To Initialize Multidimensional Array Just After Declaration

Feb 13, 2014

I tried this:

String[][]f = new String[1][1] {{"Harry"}{"Hairy"}};

I also tried this:

String[][]f = new String[1][1] {{"Harry"},{"Hairy"}};

but I get an error

View Replies View Related

Cannot Find Symbol Class Object When Trying To Initialize Array?

Sep 9, 2014

I'm trying to make an array of objects, which I then initialize using objects that I have already created. I have main class, a secondary "Other" class, and a third "Other2" class. In the first Other class, I create three objects of the Other2 class, an object array of the type Other2 , and I then try to add the three objects to the object array, which results in the errors:cannot find symbol: class objectArray, ] expected, identifier expected

here is my code:

Other class
public class Other
{
Other2 object1 = new Other2();
Other2 object2 = new Other2();
Other2 object3 = new Other2();

[code]....

View Replies View Related

Write A Program That Declare And Initialize Three Floating-point Variables

Oct 3, 2014

Write a program OutCircle.java that declares and initializes three floating-point variables (r, x, y): the first variable represents the radius r of a circle centered at (0,0) and the second and third variables are the coordinates (x, y) of a point in the plane.Your program should print true if the point is outside the circle and false otherwise. Hint: A point is outside the circle when its distance to the center is greater than the radius.

View Replies View Related

Display Phrase In Every Font Size From 6 Through 20

Jun 1, 2014

I'm trying to display a phrase in every font size from 6 thru 20. As it stands now, the code below cascades the phrase, sort of, down the frame, but the font size doesn't change.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JFontSizes extends JFrame implements ActionListener
{
JButton button = new JButton("Press Me");
String aPhrase = new String("This is a phrase.");

[Code] ......

View Replies View Related

Display A Phrase In Every Font Size From 6 Through 20

Jun 1, 2014

This is what I have thus far.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

[Code]....

The result looks something like this:
 
               Button
 
     This is a phrase.
        This is a phrase.
            This is a phrase.
                This is a phrase.
                    etc.

View Replies View Related

Display Image On JLabel Or JPanel With Respect Of Components Size?

Mar 27, 2015

i have very critical problem in my collage project i have try many code but still i cant solve this problem, i want to display image on jlabel or jpanel with respect of components size without use of drawimage method

View Replies View Related

Create 2D Array Out Of CSV File And Find Number Of Elements To Determine Array Size

Mar 24, 2015

I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.

I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.

The list consists of the following wifi related values:

MAC-Adress, SSID, Timestamp, Signalstrength.

These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.

The catch is, we are not allowed to use any of the following:

java.util.ArrayList
java.util.Arrays
and any class out of java.util.Collection.

So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.

Below is my Code (Its still an active construction zone):

public class WhatsThere {
public WhatsThere(String wifiscan) throws IOException {
}
public static void main(String[] args) throws IOException {
// WhatsThere Liste = new WhatsThere(String wifiscan);
String[][] arrayListe = new String[0][0];

[Code] ....

View Replies View Related

Why Cannot Get Value Of Variable To Display

Jul 18, 2014

I trying to display the value of a variable from a method, but I don't know why it will I can't do it.

//Java extension package
import javax.swing.JOptionPane;
public class Palindrome
{
public static void main(String[] args) {
int number = 0;
int palindrome = 0;

[Code] ....

View Replies View Related

How To Get Size Of Array From Textfile

Feb 15, 2015

I have the textfile which has lines:

A B 2 midterm 100

C D 1 final 90

C D 1 midterm 90

A B 2 final 80

**NO ARRAYLIST IS ALLOWED!** And the textfile is passed into the method. How to get the size for the array non-randomly inside the method from the passed Scanner file?? What if you have lots of numbers of lines, so how could that be done?

I have doubts about this line Exam[] object = new Exam[12];

Java Code:

public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
{
String firstName = "";
String lastName = "";

[Code].....

View Replies View Related

Increasing Array Size Automatically

Dec 8, 2014

The array size is fixed improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array into it .I am using a course class and here is the code for the class

public class Course {
private String courseName;
private int numberOfStudents;
private String[] students = new String[100];
public Course(String courseName)

[Code] ....

As you can see I have the array set to size 100, how do i make so it increments each time the user adds a student.

View Replies View Related

Multidimensional Array With Size Only In The First Square

Jan 14, 2014

I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:

int[][] multiArr = new int[2][];

the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!

View Replies View Related

Automatically Increasing Array Size?

Feb 5, 2014

I'm working on an assignment that says the following.

" The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it.Implement the dropStudent method.Add a new method named clear() that removes all students from the course.

Write a test program that creates a course, adds three students, removes one, and displays the students in the course."

10.6 Listing

public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
 } 
public Course(String courseName) {
this.courseName= courseName;

[Code]...

My Test Code based off of book

public static void main(String[] args) {
Course course1= new Course("Business");
course1.addStudent("Jay");
course1.addStudent("Silent Bob");
course1.addStudent("Dante");
course1.dropStudent("Jay");
 
[Code]....

My adjusted 10.6

public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
 } 
public Course(String courseName) {
this.courseName= courseName;

[Code]...

The problem I'm having is, for the first part of the question where I need to automatically increase the array size. I'm really not great at this stuff. I have tried breaking it down, but can't "get it", I guess.

I assume, it'd be a loop that checks to see if the student array is full and if so, do the increaseArray() part, by maybe multiplying the student array and then assigning it. I just don't know how to do it haha.

My *best* attempt at the loop so far has been

if (students == students.length){
int bigArray = 2*students.length;
String increaseArray()= new String[students];
System.arraycopy(students, 0, increaseArray, 0, students.length);
students= increaseArray;

but,yeah... Doesn't seem to be right.

View Replies View Related

User Input For Array Size

Oct 28, 2014

I am having trouble with an assignment. I need the user to input the size of the array and print when asked. In my program, it prints 100 numbers instead of the user input number, such as 15.

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class Lab9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] values = new int [100];

[Code] .....

View Replies View Related

Increasing / Decreasing Size Of Array

Jul 11, 2014

I am trying to make a code that copies the users String to a char array. However, I am in a predicament: since I would not know the exact size of the users String I am left with the options of either making my array large by default, filled in with, lets say 25, empty spaces per index OR starting out with a default size of 1, with an empty space, and then some how increase the size from there.

At this moment I am leaning on the first option, creating a large default array and then deleting the unused values. However, this brings me to my actual question: if I set the non used indexes to null, if that wont give me an error, would that change the size of my array?

Ex:
//lets say i finally copied all of the values and this is the result
char[] word = {'b', 'o', 'b', ' ', ' '};
for(int i = word.length(); i > 0; i--){
if(word[i] == ' ')//delete the value so the size decreases
word[i] = null;//if possible
}

View Replies View Related

Array Is Not The Right Size - Causing Errors

Oct 17, 2014

protected void randomise() {
int[] copy = new int[];
// used to indicate if elements have been used
boolean[] used = new boolean[array().length];
Arrays.fill(used,false);
for (int index = 0; index < array().length {

[Code] .....

View Replies View Related

Pass Predefined Size Array As Argument

Jan 9, 2014

Is this possible in Java?

public void doSomething(int[4] year){
}

What I am trying to do is to get the person who is using the method to send a year in the format 1998 and so on.

What would be the best way to accomplish this?

View Replies View Related

Field Size In Array List Class

Jan 29, 2015

I'm just putting together a little 'horse racing' program while I'm learning Java, and I have a class called Race that creates an array list of thorougbred horses called field, and asks the user to enter then names of the each horse in the field, with a maximum of 14 horses. The problem occurs with the current code that I have:

import java.util.*;
public class Race {
RaceHelper helper = new RaceHelper();
ArrayList<thoroughbred> field = new ArrayList<thoroughbred>();
public void setField() { //enter the horses in the race and determine the size of the field

[code]....

the statement of the index position and current size was for me, so I could see what was going on.What I don't understand is the while loop. not that the class doesn't compile and run (you can see that it does), it's the output. Why does the <= sign allow one more entry and increase the size of the field to 15?

Less than or equal to 14 should give a maximum field size of 14, right, With the starting object at index position at zero and going up to 13, for a total size of 14 thoroughbred objects if I just use while (field.size()<14) or a for loop, then the output is fine; it allows a max of 14 entries and prints the results. I thought it had something to do with the size being zero based, but that doesn't seem to matter -- unless it does matter and I'm missing it. why the comparison I'm using produces this output? a field of 14 horses shouldn't matter whether it's zero or 1 based, as long as the size of the field is 14, so why the extra entry with this while condition?

View Replies View Related

Increase Size Of Array But Getting Null Values

Dec 3, 2014

I'm working on a program to create a blackjack game using objects (one for card, deck. and hand). Withing my hand object I am trying to add cards to the hand but it is only adding the last card i try to add and giving null values for the the ones before.

class BlackJackHand {
private BlackJackCard [] hand;
public void addToHand(BlackJackCard c) {
if (hand == null) {
BlackJackCard [] tempHand = new BlackJackCard[1];
tempHand[0] = c;
hand = tempHand;

[Code] ....

What I want this section to do is add cards to the current hand. I was intending for it the hand to be null at first and the if(hand == null) piece to add the card the first time and then the else piece would be used when the hand already has at leas one card. I want the else section to create a temporary array that is one larger than my current hand, copy the cards from the old hand to the new hand, and then add a new card to the last space before rewriting the old hand as what the temporary hand is.

The code I am using to test if the addToHand() is working is

class BlackJackTest
{
public static void main (String[]args) {
BlackJackCard c1= new BlackJackCard(1,0);
BlackJackCard c2= new BlackJackCard(1,4);
BlackJackCard c3= new BlackJackCard(1,5);
BlackJackHand h1 = new BlackJackHand();

[Code] .....

BlackJackCard has the parameters (int suit, int value)

This should print:
ace of clubs
4 of clubs
5 of clubs

but instead prints:
null
null
5 of clubs

View Replies View Related

Display Month Using Enumerated Data Type From String Variable

Apr 1, 2014

I have created an enumerated data type that represents months. In my program I have asked the user for the month. I hold the month entered by user into a string variable. From that string variable I'm trying to display the month using my enumerated data type. I know I can use if statements, but is there another simple way to do it?

import java.util.Scanner;
public class demo {
 //Enum class called month that has constants in it representing months
enum month{january,february,march,april,may,june, july,august,september,
october,november, december};
 
[Code] ....

View Replies View Related

Swing/AWT/SWT :: Mini Paint Program - Changing Size Of Shapes

Jun 4, 2014

I am currently writing a small drawing program and I am having trouble with changing the size of the shapes. To do this, I have to access the arraylist shapes, check whether pressedX/pressedY is on any of the shapes in the arraylist using the findShape() method and then when released, uses moveBy() in the Rectangle/Oval/Line class and moveShape() in the miniDraw class to move the shape and draw it in the newreleasedX/releasedY position.

So far I think I have pin pointed the problem to being the method in all the shapes classes, that checks whether the pressedX/pressedY which is the on() method, and the findShape() method in the miniDraw class.

This is the minidraw class

import ecs100.*;
import java.awt.Color;
import java.io.*;
import java.util.*;
import javax.swing.JColorChooser;

[Code] .....

View Replies View Related

Generate 10 Random Integers / Store In Array And Then Calling A Method To Display Array

Nov 8, 2014

So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:

public static void displayArray(int[] array)

This is what I have done
 
public class RandomIntegers {
 static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");

[Code] .....

View Replies View Related

Populate Array Using Nested Loops With Letter From A Until Y And Display Array To Screen

Nov 15, 2014

We were given a class lab that asks us to write a program that create a multidimensional array ( 5 x 5 ), populates the array using nested loops with letter from A until Y, and displays the array to the screen. and the result should look like this:

A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y

How to write this program.. I have tried all my best but the results are not coming like this..

View Replies View Related







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