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


ADVERTISEMENT

How To Use Nested Loops To Print Out A Letter Pattern Wraps From Z

Jan 6, 2015

//method class
import static java.lang.System.*;
public class TriangleFive
{
private char letter;
private int amount;
public TriangleFive()
{

[Code]...

View Replies View Related

Demonstrate Two Dimensional Array - Nested For Loops

Feb 4, 2015

Java Code:

// Demonstrate a two-dimensional array
class TwoDArray {
public static void main(String args[]) {
int twoD[] [] = new int[4] [5];
int i, j, k = 0;

[Code] .....

Output:

0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19

(1) I don't understand how repeating the loop creates the structure of the output. Taking the second loop away and putting the "System.out.print(twoD[i] [j] + " " );" under k++ creates the output to print a number on each line. How do I write the code not having the second loop, assigning k to each value that is moved through the grid then printing it out but having the output the same?

Java Code:

for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
System.out.print(twoD[i] [j] + " " );
System.out.println();
} mh_sh_highlight_all('java');

(2) I don't understand why you can't put a new line sandwiched between the first for loop

Java Code:

for(i=0; i<4; i++)
System.out.println();
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++; mh_sh_highlight_all('java');
It compiles, but I get the message:

" Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at TwoDArray.main(TwoDArray.java:11) "

View Replies View Related

Nested For Loops - Check If There Is A Duplicate Element In The Array

Aug 4, 2014

Nested for-loops always throw me in a loop.I found a snippet that uses 2 for-loops to check if there is a duplicate element in the array:

/*
* brute force way of checking if array contains duplicates in Java comparing each elements to all other elements of array complexity on order of O(n^2) not advised in production
*/
public static boolean bruteforce(String[] input) {
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < input.length; j++) {
if (input[i].equals(input[j]) && i != j) {
return true;
}
}
}
return false;
}

Let us say we have:
String[] input = new String[] {"one","two","three","one"}

View Replies View Related

Blue Pelican Java - Array Of Hope - Char Array For Loops?

Feb 13, 2014

i am working on the same project and i got the code to make them print going down but not sideways.

public class ArrayofHope
{
public static void main(String args[])
{
System.out.println("Decimal Character
");
for(int j = 65; j <= 90; j++)
{
System.out.print(j);
System.out.println(" " + (char)j); //Character
}
}
}

View Replies View Related

Populate Array From Database

Apr 14, 2015

I have this working manually, but need creating this from a DB Connection and populating the Array with the results. I just need populating "DestinationItem[]" from the SQL below
 
DestinationBean.java
//  Manual Array works but I need this to be populated from DB using the below Query and DB Connection info.
    private DestinationItem[] destinationResults = new DestinationItem[]{
            new DestinationItem("58285", "Dodge Grand Caravan"),
            new DestinationItem("57605", "Dodge SX 2.0"),
            new DestinationItem("58265", "Chrysler 300 Touring")

[Code] .....

View Replies View Related

Populate A Database Using Loops

Jan 6, 2014

I am relatively new to Java and I am only beginning to learn about SQL. I have some basic's down but I have been wondering is there a way that I can add data to my database using loops instead of having to physically code every row/column individually ?

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

Declare Array And Populate It With Log Formula

Feb 10, 2014

Still trying to get a handle on arrays! So, I declare an array to be a 46x1 and I am trying to populate it with a Log formula that I am using but I keep getting an ArrayIndexOutofBoundsException.

Code :

private double[][] LNValues = new double[46][1];
//Calculating y=LN(E-k) and Initializing the Array
for(int x=0; x<LNValues.length; x++)
{
double i = Math.log(eValues[x][1] - kValue);
if(i > 0)

[Code] ....

View Replies View Related

Populate Array With Leading Zeros

Nov 18, 2014

why I need to populate an array with leading zeros. It sounds like it wants me to populate the entire array for one number (string). When I use next(), it separates the numbers so why do I need to go the leading zeros route?

This assignment will give you practice with external input files and arrays. You are going to write a program that adds together large integers. The built-in type int has a maximum value of 2,147,483,647. Anything larger will cause what is known as overflow. Java also has a type called long that has a larger range, but even values of type long can be at most 9,223,372,036,854,775,807.The approach you are to implement is to store each integer in an array of digits, with one digit per array element. We will be using arrays of length 50, so we will be able to store integers up to 50 digits long. We have to be careful in how we store these digits. Consider, for example, storing the numbers 38423 and 27. If we store these at the front of the array with the leading digit of each number in index 0 of the array, then when we go to add these numbers together, we're likely to add them like this:

38423
27

To simulate this right-shifting of values, we will store each value as a sequence of exactly 50 digits, but we'll allow the number to have leading 0's. For example, the problem above is converted into:

0000000000000000000038423
0000000000000000000000027

Now the columns line up properly and we have plenty of space at the front in case we have even longer numbers to add to these.The data for your program will be stored in a file called sum.txt. Each line of the input file will have a different addition problem for you to solve. Each line will have one or more integers to be added together. Take a look at the input file at the end of this write-up and the output you are supposed to produce. Notice that you produce a line of output for each input line showing the addition problem you are solving and its answer. Your output should also indicate at the end how many lines of input were processed. You must exactly reproduce this output.

You should use the techniques described in chapter 6 to open a file, to read it line by line, and to process the contents of each line. In reading these numbers, you wont be able to read them as ints or longs because many of them are too large to be stored in an int or long. So youll have to read them as String values using calls on the method next(). Your first task, then, will be to convert a String of digits into an array of 50 digits. As described above, youll want to shift the number to the right and include leading 0s in front.

The String method charAt and the method Character.getNumericValue will be useful for solving this part of the problem.You are to add up each line of numbers, which means that youll have to write some code that allows you to add together two of these numbers or to add one of them to another. This is something you learned in Elementary School to add starting from the right, keeping track of whether there is a digit to carry from one column to the next. Your challenge here is to take a process that you are familiar with and to write code that performs the corresponding task.

Your program also must write out these numbers. In doing so, it should not print any leading 0s. Even though it is convenient to store the number internally with leading 0s, a person reading your output would rather see these numbers without any leading 0s.You can assume that the input file has numbers that have 50 or fewer digits and that the answer is always 50 digits or fewer. Notice, however, that you have to deal with the possibility that an individual number might be 0 or the answer might be 0. There will be no negative integers in the input file.You should solve this problem using arrays that are exactly 50 digits long. Certain bugs can be solved by stretching the array to something like 51 digits, but it shouldnt be necessary to do that and you would lose style points if your arrays require more than 50 digits.The choice of 50 for the number of digits is arbitrary (a magic number), so you should introduce a class constant that you use throughout that would make it easy to modify your code to operate with a different number of digits.

Consider the input file as an example of the kind of problems your program must solve. We might use a more complex input file for actual grading. The Java class libraries include classes called BigInteger and BigDecimal that use a strategy similar to what we are asking you to implement in this program. You are not allowed to solve this problem using BigInteger or BigDecimal. You must solve it using arrays of digits.Your program should be stored in a file called Sum.java.

Input file sum.txt
82384
204 435
22 31 12
999 483
28350 28345 39823 95689 234856 3482 55328 934803
7849323789 22398496 8940 32489 859320
729348690234239 542890432323 534322343298
3948692348692348693486235 5834938349234856234863423
999999999999999999999999 432432 58903 34
82934 49802390432 8554389 4789432789 0 48372934287
0
0 0 0
7482343 0 4879023 0 8943242
3333333333 4723 3333333333 6642 3333333333

Output that should be produced
82384 = 82384
204 + 435 = 639
22 + 31 + 12 = 65
999 + 483 = 1482
28350 + 28345 + 39823 + 95689 + 234856 + 3482 + 55328 + 934803 = 1420676
7849323789 + 22398496 + 8940 + 32489 + 859320 = 7872623034
729348690234239 + 542890432323 + 534322343298 = 730425903009860

[code]....

View Replies View Related

JButton Cycling - Display Whole Array Instead Of Just Next Array Position

May 17, 2014

JButton btnNext = new JButton("Next");
btnNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < bkSorted.length; i++)
{
textArea.append("
" + bkSorted[i+1]);
}
}
});

When I click the button, it displays the whole array, instead of just the next array position. What am I doing wrong?

View Replies View Related

Populate Array From Text File Or Scanner

May 26, 2014

I have a simple txt file, each line simply containing 1 word.I would like each work to represent an index of the array..im having some difficulty populating an array from either a txt file or a scanner.i seem to be able to fill the scanner so to speak with the contents of the text file but not the array. I don't know how to syntax it

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Arrays;
public class ReadFile
public static void main(String[] args) {

[code]....

View Replies View Related

Why Can't Populate Array Of Objects From Inside Constructor

Feb 21, 2015

The experiments will slowly converge towards one big experiment: a simple game. I have just a little interest in games (perhaps I should have more), but making one - even a simple one - should be self rewarding.

However, now to the point.

* The experiment creates an array of rectangle objects.
* The rectangles are painted inside a Frame object at random x,y coords generated by a random number generator
* The rectangles are stationary.
* The rectangles are each assigned their own random colour.
* The array of rectangle objects is created inside the constructor of the class.

The actual code contains various other variables and methods which would distract from a quick analysis, so below is code which has the same logical structure which also fails (instead of array of rectangles, I have used arrays of integers).

import java.util.Random;
/**
* Experiment 14 - see if it works simply - (with integer arrays)
*/
public class TestingArrays {
// instance variables
int N = 10; // the size of the array - 10 elements
int[] a;

[Code]...

View Replies View Related

Using For Loop To Populate Array With Random Double Values

Oct 26, 2014

Write a program that creates an array that can hold 9 double values that represent baseball batting averages for a starting baseball lineup. Use a for loop to populate array with random double values in the range of 0.00 to 0.500. Recall that "double" values are what Java calls "real" numbers. Use a second for loop to print the values in the array with one number per line. Finally, use a third for loop to traverse the array and find and print the maximum batting average in the array. Note: you will need to use String.format to control the precision of a double number when you print it- Here is my code so far:

public class P2F {
public static void main (String[] args) {
double [] player= new double [9];
//player[0]= Math.random();
for (int index=0; index < player.length; index++) {

[Code] ....

When I open the terminal window I get different variations of this [D@4545c5]. I would like to know all the things I am doing wrong.

View Replies View Related

Nested For Loops

May 15, 2014

how a nested for loop increments the outer loop and then the inner loop and then when it runs the inside code.I've ran the program a few times but I'm just not understanding it, need little narrative to it?

for(int i=0; i < 5; i++){
System.out.println("i - " + i);
for(int j=0; j < i; j++){
System.out.println("j - " + j);
count++;
}
}

View Replies View Related

Return Array Containing All Strings That Start With Letter A

May 13, 2014

Write a method that accepts an array of Strings.  The method must return an array containing all strings that start with the letter 'A'.  

There must be no 'null' values in the resulting array.  The resulting array may be of size zero.

I don't think I am returning ALL the strings that start with A, just the last one of the for loop. Does this look somewhat correct?

public String[] startsWithA(String[] strings) {
String startsWithA = strings[0];
for(int i = 0; i < strings.length; i++) {
if(strings[i].charAt(0) == 'A') {
startsWithA = strings[i];
}
return startsWithA;
}
}

View Replies View Related

Nested Patterns Using For Loops

Feb 9, 2015

i want to use nested loops to print patterns but i cant seem to make a pattern that will look like V and K this should be the outcome.

* *
* *
* *
* *
*

@ @
@ @
@@
@ @
@ @

View Replies View Related

Nested Loops Or Set Operations?

Oct 10, 2013

Is there any advantage to using Java's operations defined in sets or collections over inspecting in nested for-loops?

For example, suppose I want to keep items from one array that do not appear in another, I could do:

for( PrimitiveType prim_1 : someArray ){
for( PrimitiveType prim_2 : otherArray ){
if( prim_1 != prim_2){
///put in some return list
}
}
}

Or else I could do:

ArrayList<NotPrimitive> excludedElements =
new ArrayList(Arrays.asList(primitiveArrayToExclude));
for( PrimitiveType elem : someArray){
if( ! excludedElements.contains( elem ) {
//put in some return list
}
}

I personally see no improvement in legibility nor any reduction of error potential.....

View Replies View Related

Check If Letter Entered By User Matches The One In Array

Jun 10, 2014

/**
* Auto Generated Java Class.
*/
import java.util.*;
public class Hangman {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int guess;
boolean revealed[] = {false, false, false, false, false};
String word [] = {"c", "a", "n", "a", "d", "a"};

[Code] ....

I am not sure how to make the program check if the letter entered by the user matches the one in the array. also i am not sure how to make the program run again with a new word.

View Replies View Related

Displaying Pattern - Nested For Loops

Jan 24, 2015

Write a program that will display the following pattern.

$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$

I need to write a program that uses nested for loops to display that patter but I have no clue on how to do it....

View Replies View Related

Line Spacing - Nested For Loops

Sep 21, 2014

I am having a problem understanding line spacing. I need to write a program that has nested FOR LOOPS to produce the following output:

1
2
3
4
5

I can get a for loop that counts down. The problem I have is using for loop for line spacing. How a for loop for a line works.

View Replies View Related

Nested FOR Loops For Rows And Columns

Apr 21, 2014

I need to write a class,that will give me output like this:

*
***
****
***
*

I have to use for loop,i know that i have to use nested for loops, for rows and columns. I just cant figure it out the thing with spaces,and how to turn it to count back.

View Replies View Related

Create A Checkerboard Pattern With 2 Nested For Loops?

Feb 12, 2015

Im trying to create a checkerboard pattern with 2 nested for loops . I need to output asterisk characters. Im supposed o use an n int so I dont know if im limited to that 1 int. Im only getting 1 line of asterisk.

* * * * *
* * * * *
* * * * *
* * * * *
package checkerboard;
public class Checkerboard {
public static void main(String[] args) {
for (int row = 1; row < 6; row++){
System.out.print("* ");
for (int col = 6; col < col; col++) {

[code]...

View Replies View Related

Nested For Loops - Determine If Inputted Integer Is Even Or Odd

Sep 29, 2014

So I cant figure out why my output for my for loop isn't working properly. So the output for the square comes out right but the for loop isn't working properly for the H. I have tried to figure it out and it should go to the next line but its not.

import java.util.Scanner;
public class Random
{
public static void main(String [ ] args)
{

[Code] ....

Output

Enter a positive odd integer: 5

Square:
*****
* *
* *
* *
*****

H:
* *

View Replies View Related

Using Nested Loops In A Class Called Print Patterns

Mar 7, 2014

Write a method to print each of the following patterns using nested loops in a class called PrintPatterns. The signature of the methods are:

public static void printPatternX(int size) //'X' from 'A' to....., size is a positive integer.

#
##
###
####
#####
######
#######
########

View Replies View Related

Making Patterns (Diamond Shape) Using Nested Loops

Mar 23, 2015

I have to make a pattern that is of a diamond shape such as this:

Java Code:
1
1 2 3
1 2 3 4 5
1 2 3
1
mh_sh_highlight_all('java');

I wrote this (for the variable name, this is just a small part of my program and I'm trying to get the pattern right so I didn't mind my variables at the moment):

Java Code:

public class NewFile{
public static void main(String []args){
int k = 0;
for (int i=1 ; i<=5 ; i++) {
{ for (int h=2 ; h >= i ; h--)

[Code] ....

My output is the following:

Java Code:
1
123
12345
1234567
123456789
mh_sh_highlight_all('java');

I realize I should divide the code into a lower and upper triangle using two loops. However, I don't know how to break the first part. I did find the "trend" but I don't see how to implement it.

View Replies View Related







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