How To Count From First Loop To Last

Feb 16, 2015

I made this "lesson" to get the result of a pyramide which I succeded but not understand why im affraid..The thing is I dont understand exactly how to count it.. Isnt "j" incrementing every loop ?? In my head it should be just one star per line( the height is set to 3)how to count from the first loop to the last?I understand that println change the line though, but not why j would loop more than one time on each line..

for(int i = 1; i <= height; i++){
for(int j = 0; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}

result:

*
**
***

View Replies


ADVERTISEMENT

For Loop To Count Values Of Stack Of Quarters From 1.00 To 3.00 And Print Value Of Each Iteration

Nov 2, 2014

I had to write a foor loop to count the values of a stack of quarters from 1.00 to 3.00 and I had to print the values, that I understood and got it working so I taught the next assignment was going to be easier but I am having a hard time with it. For this one I have to write a for loop to print all the positive integer factors of 144 and I am supposed to print of factor per line but I tried doing that but it doesn't work it just prints out 144.

This is my code. The quarter assignment is also in there because it is part of a lab so just ignore that part since it is working correctly.

public class ForLoopPractice
{
public static void main(String [] args)
{
// Write a for loop to count out the values of a stack of quarters from $1.00 to $3.00
// Print the value of each iteration. Print this all on one line, rounded to the nearest cent.
// To print rounded, use printf, with a placeholder of %.2f
// (%f is the floating-point placeholder; the .2 indicates the number of decimal places)
/* YOUR CODE HERE */
for (double q = 1.00; q <= 3.00; q += .25)

[Code] ....

View Replies View Related

Using Count Element Method To Count Occurrence Of Characters In Array

Jun 30, 2014

I have an array with the following characters {'E', 'L','E','P','H','A','N','T','P','O'}

now, I need an array that will store the first array such that only the occurence occurs e.g {'E','L','P','H','A','N','T','O'} Notice that the characters 'E' and 'P' occur twice and as a result were not repeated the second time in the new array.

How would one go about this using the counting elements technique?

I tried this but not sure how to use the counting elements technique.

char [] arr = new char{'E', 'L','E','P','H','A','N','T','P','O'};
char[] bucket = new char[(arr[0] * arr.length)];
for (int i = 0; i < len; i++)
bucket[arr[i]]++;

View Replies View Related

Unable To Print A Loop Inside Array Of Greater Size Than Loop Itself

Jan 27, 2015

I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.

int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;

now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.

View Replies View Related

While Loop Inside A For Loop To Determine Proper Length Of Variable

Feb 25, 2014

Here's the code: it's while loop inside a for loop to determine the proper length of a variable:

for (int i = 0; i < num; i++) {
horse[i]=new thoroughbred();
boolean propernamelength = false;
while (propernamelength==false){
String name = entry.getUserInput("Enter the name of horse "

[code]....

I was just wondering what was going on here -- I've initialized the variable, so why do I get this message? (actually the carat was under the variable name inside the parentheses.

View Replies View Related

When Type Quit To Close Outer Loop / It Still Runs Inner Loop

Nov 2, 2014

I have everything else working. My problem is that when i type "quit" to close the outer loop. It still runs the inner loop. The National Bank manager wants you to code a program that reads each clients charges to their Credit Card account and outputs the average charge per client and the overall average for the total number of clients in the Bank.

Hint: The OUTER LOOP in your program should allow the user the name of the client and end the program when the name entered is QUIT.In addition to the outer loop, you need AN INNER LOOP that will allow the user to input the clients charge to his/her account by entering one charge at a time and end inputting the charges whenever she/he enters -1 for a value. This INNER LOOP will performed the Add for all the charges entered for a client and count the number of charges entered.

After INNER LOOP ends, the program calculates an average for this student. The average is calculated by dividing the Total into the number of charges entered. The program prints the average charge for that client, adds the Total to a GrandTotal, assigns zero to the Total and counter variables before it loops back to get the grade for another client.Use DecimalFormat or NumberFormat to format the numeric output to dollar amounts.

The output of the program should something like this:

John Smith average $850.00
Maria Gonzalez average $90.67
Terry Lucas average $959.00
Mickey Mouse course average $6,050.89
National Bank client average $1,987.67

Code:

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = "";
int charge = 0;
int count = -1;
int total = 1;
int grandtotal = 0;
int average = 0;
 
[code]....

View Replies View Related

Java Count Up Timer

Mar 6, 2014

How to do a count up timer in java? like "0:00:00" hh:mm:ss

View Replies View Related

Count How Many Duplicates Are In ArrayList?

May 14, 2015

I'm trying to count the number of elements in an ArrayList which also have duplicates. So for example, in an ArrayList of strings which contains cat, cat, dog, horse, zebra, zebra, the answer should be two.

If an element is found to be a duplicate, that element should then be exempt from the search so if that element is found again it should not increase the duplicate count.

Here is my code:

public int countDuplicates() {
int duplicates = 0;
// TODO: Write the code to get the number of duplicates in the list
for (int i = 0; i < list.size()-1;i++) {
boolean found = false;

[Code] ....

I know it's wrong because right now it's still increasing the duplicate count for elements that have already been detected as duplicates. How can I make it stop doing this?

View Replies View Related

How To Count Primitive Operations

May 2, 2014

I have having some trouble on counting the primitive operations on the pseudocode given below:

Algorithm 4. MaximumArray(Arr)
Input: A 1-D numerical array Arr of size n
1) Let CurrentMax = a0
2) For i = 1 to n-1
3)If ai > CurrentMax Then CurrentMax = ai
4) End For
Output: CurrentMax, the largest value in Arr

As of now, I know that for Line 1 there are 2 operations (one set and one read). I don't know how to figure out the for loop and If statement (line 2 and line 3 too).

View Replies View Related

Trying To Count All The Vowels In A String

Feb 13, 2015

I am currently trying to count and display all the vowels in a set of given strings and can't seem to figure out what to do. I was able to print the line with the most vowels, but i also need to display them. The code is listed below and the given output.

public class Strings
{
public static void main(String[] args) {
String sentence = "I am currently studing Computer Science."
+ "My name is whatever and I am orginaially from the state of Virginia."
+ "I recenetly separated from the Air Force where I served on the Presidental Honor Guard.";

[Code] ....

The output that i am getting is:

I am currently studying Computer Science .
My name is whatever and I am originally from the state of Virginia.
I recently separated from the Air Force where I served on the Presidential Honor Guard.

The line with most vowels is:

I recently separated from the Air Force where I served on the Presidential Honor Guard.

View Replies View Related

Count Occurrence Of Int In Array

Feb 28, 2014

(Count occurrence of numbers) Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:

Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time

Note that if a number occurs more than one time, the plural word "times" is used in the output.

View Replies View Related

Incrementing Count By 1 Each Time

May 25, 2014

I am currently take a class, and trying to increment the count of employees I enter into an ArrayList by 1 for every entry. However, instead of incrementing by 1 each time, the current number of employeeIDs adds itself to itself, then adds 1, giving a non-sequential number (i.e. an employeeID of 75 will add itself to 75 + 1 to get a new employeeID of 151).

The following code is what I have done in terms of incrementing. (More coding has been done, but it is not used to increment, so I have omitted it.

public class Employee
{
// instance variables
private String firstName;
private String lastName;
private int employeeID;
static int newEmployeeID = 0;

[Code] ....

View Replies View Related

Count Single Digits

Apr 25, 2014

why exactly this isn't printing out the correct way.I'm supposed to print out 100 elements from an array but assign them random integers between 0 and 9.I've tried troubleshooting the issue and when I do it seems i'm only getting one element assigned.

public class Exercise_6_7{
public static void main(String[] args){
int[] counts = new int[100];
for(int i=0; i<counts.length; i++){
counts[i] = (int)(Math.random()*10);

[code]....

View Replies View Related

How To Count 1st Duplicates Value In Array By Using Java

Apr 12, 2015

I have taken array int[] a = {33,33,5,5,9,8,9,9};

In this array so many values are duplicates means 33 comes twice & 5 also comes twice & 9 comes three times. But I want to count the first value which is duplicate means 33 is first value which comes twice so answer would be 2.

I try:

public class FindFirstDuplicate {
public static void main(String[] args) {
int c=0;
int[] a = {33,33,5,5,9,8,9,9};
outerloop:
for(int i = 0; i < a.length; i++) {

[code]....

Output:

33
Count: 1

View Replies View Related

Counter To Count Random Occurrence

Jan 21, 2015

I have 2 classes a main one and another class. In the other class I have a method that has:

String gInfo(){
String[] Behavior = {"Sleeping", "Running home", "Studying", "playing pool", "walking to class"};
int rNum = (int) (Math.random() * Behavior.length);
return Behavior[rNum];
}

I call the gInfo() in the main class and run it through a loop 20 times. What I'm trying to do is get it to count the number of times that each behavior happens. and how to get it to work correctly.

View Replies View Related

How To Count Certain Number Of Characters In Any Txt File

Mar 24, 2014

So I have 2 args[] one that reads the file name in the same directory and the other that tries to count how many of any letter (maybe even words) in the txt file ....

View Replies View Related

Count Number Of Books In Arraylist?

Jan 29, 2015

We have been given an assignment to create a small library, with the following classes Library, Book, Author and Copy. With a given class Biblio which has predefined code and adds the books to the class book in an arraylist in Class Copy.

The UML Domain is attached so you know the flow of the classes Everything is working fine and the generated output is correct.

There is just one method in class Library that is not working, the int method has to count the number of Copy's based on the Class Book (String):

I have to go through the Arraylist in Class Copy and look for a specific book and return the number of copy's.

I have tried multiple steps using a for loop Now I have found a similar post the uses hashset, I have tried below code but the return comes back with 0. (There are 3 copy's)

package domein;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import domein.Boek;
import domein.Exemplaar;

[code]....

View Replies View Related

Count Length Of Each Word In A String

Aug 5, 2014

I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:

hello world how are you
There are 0 words of length 0
There are 0 words of length 1
There are 0 words of length 2
There are 3 words of length 3
There are 0 words of length 4
There are 2 words of length 5

This is my code so far it sort of does the job but not the way i want it too

import java.util.Scanner;
import java.util.StringTokenizer;
public class Brown_Matthew_13117002{
public static int count(String s, int len){
int result=0;
StringTokenizer st=new StringTokenizer(s,"[ ,;]");

[Code] ....

The output would end up being :

hello
There are 0 words of length 0
world
There are 0 words of length 1
how
There are 0 words of length 2
are
There are 3 words of length 3
you
There are 0 words of length 4

View Replies View Related

Unable To Count Each Char In A String

Jun 5, 2014

I am trying to count each char in a string. For example A = 1, B =2, C=3, I am not looking for their binary value. So the word "At" would

AT= (A=1 +T=20)=21.

I know how to do this in C++ because I am able to treat a string like an array.

Java Code: void printFile()
{
int sum=0;
String line;
for(char cr ='A';cr<'Z';cr++)
{
for(int i=0; i<myList.size();i++)

[Code]...

View Replies View Related

Count Number Of Paragraphs In A Document

Mar 28, 2014

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
 public class Paragraph {
public static void main(String[] args) throws FileNotFoundException, IOException

[Code] .....

View Replies View Related

Progress Bars (can't Start And Won't Count Down)

May 5, 2014

I'm trying to get a timer to work to decrease the value of a stats bar (like Hunger Level, Energy Level), but it won't paint the bars in the first place, it won't let me start the timer (the start after t.start() is underlined) and now I'm getting an error that won't let me run it at all without putting public static void main (String [] args), even though adding it anywhere is causing other errors

public void hungerTimer () {
int delay = 300;
int period = 300;
interval = 100;
hungerBar.setValue(100);
hungerBar.repaint();
timer = new Timer();

[Code] .....

View Replies View Related

How To Count Number Of Pages Printed

Jul 20, 2010

i want to create a program in java which counts the number of prints taken from a printer..

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

Using String Methods To Count Words?

Nov 3, 2014

Aeparated by blanks in a string. For simplicity, use strings without punctuation or other white space characters(tabs, newlines etc). Use a JTextArea to allow the user to enter the text and allow the text area to scroll if necessary. when the user clicks a button to count the words , the total number of words counted is displayed in a textbox that cannot be modified by the user.

now my problem is that i am not getting the counted number to display in the un-editable textbox. i also have the problem where the cusrsor is showing in the middle of the input screen instead of at the top.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
public class WordCounter extends JFrame implements ActionListener

[code]....

View Replies View Related

String Array Unique Count?

Jun 9, 2014

I am attempting to count the unique strings (a.k.a flowers) in the array along with a count of any duplicates. An example is embedded in my code below. I've only pasted the part of the program I am having trouble with. I can't figure out what I am doing incorrectly.

private void displayFlowers(String flowerPack[]) {
// TODO: Display only the unique flowers along with a count of any duplicates
/*
* For example it should say

[Code]....

View Replies View Related

Count The Length Of Each Word In A String

Jul 31, 2014

I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:

hello world how are you
There are 0 words of length 0
There are 0 words of length 1
There are 0 words of length 2
There are 3 words of length 3
There are 0 words of length 4
There are 2 words of length 5

ithis is my code so far it sort of does the job but not the way i want it too

import java.util.Scanner;
import java.util.StringTokenizer;
public class Brown_Matthew_13117002{

[Code].....

View Replies View Related







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