Print Range Of Integers From X To Y With Increment Of 5

May 6, 2015

Working on problem in my book in which I have to print a range of integers from x to y with an increment of 5. I thought I had the right idea when writing out this code, but apparently, it only gives a few of the numbers in the range, not all, what I am doing wrong?

import java.util.Scanner;
public class Ranges
{
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter a value for x and y: ");
int x = input.nextInt();

[Code]...

import java.util.Scanner;
public class Ranges
{
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter a value for x and y: ");
int x = input.nextInt();

[Code]...

View Replies


ADVERTISEMENT

If / Else Statement - Sum Of Integers Inside And Outside Of Range

Feb 13, 2014

Write a program that asks the user for the low and high integer in a range of integers. The program then asks the user for integers to be added up. The program computes two sums:

The sum of integers that are in the range (inclusive) and the sum of integers that are outside of the range. The user signals the end of input with a 0.

In-range Adder
Low end of range:
20
High end of range:
50
Enter data:
21
Enter data:
60
Enter data:
49
Enter data:
30
Enter data:
91
Enter data:
0
Sum of in range values: 100
Sum of out of range values: 151

Here is my code:

import java.util.Scanner;
class addRange
{
public static void main ( String[] args )
{
Scanner scan = new Scanner( System.in );
System.out.println("The In-Range integer is; ");
int inR = scan.nextInt();

[Code] ....

I'm getting an error on the line with the first else if saying nextNum might not have been initialized. but it's initialized on the line directly above that....

View Replies View Related

Switch Statement That Decides What Range To Print Based On Letter Grade Input

Nov 1, 2014

Alright so I wrote a switch statement that decides what range to print based on the letter grade input.

import java.util.Scanner;
public class SwitchPractice
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);

[code]...

It works fine, but once it the user enters a letter grade and then displays the value, it does not prompt the user for another letter grade so that it can perform the output again. Also if I wanted to display an error message to the user if they enter an invalid letter grade how would I do that. I tried using a while loop and if statement in the switch statement but that didn't work.

View Replies View Related

Creating Method To Print Array Of Integers

May 7, 2014

I am trying to create a method that takes an array of integers and prints it out using System.out.print. I'm having trouble creating the right way to print it out since I cannot find a way to convert the int array to a string to print it out.
 
public static String printArray(int[] num){
for (int i=0; i<num.length;i++){
String msg = num[i];
}
return System.out.print(msg + " ");
}

View Replies View Related

Unable To Print Repeating Integers In Array

Aug 5, 2012

I have a problem where i have to write some code to read through an int array and print out the different integers and how many times they occur. For example:

if the array contained the numbers 1,4,2,3,5,4,4,7,5,4,3,6,8,6,4,

i would need the print to appear something like this

integer: 1, times: 1
integer: 4, times: 5
integer: 2, times: 1

etc

as the array is read sequentialy from element 0 to the end. the program should find the integer value in the first element (increment a counter by 1) and then search all other elements to see if the integer reoccurs (counter++ for each time it reappears). obviously the counter would be the 'times' value in the second column.

My problem is i can get this accomplished yet i cant get the program to recognise when it has already registered an integer in a previous element and skip to the next element looking for a new integer. SO for each element in the array it prints the integer and then searches the entire array for the integer again and increments the counter accordingly. so in essence there is a line printed for each integer the number of times it actually occurs.

I simply wish to have my code to find an integer and the number of times it occurs. and then disregard the elements it has registered the previous integer in.

Also, keep in mind that i am NOT permitted to use any new data structures/arrays for storing/remembering values. I can only use int and double variables throughout.

what i have so far is this: with the array being customerID[] :

Java Code:

int num;
int counter;
for(int count = 0; count < customerID.length; count++)
{
counter = 0;
num = customerID[count];
for(int count2 = 0; count2 < customerID.length; count2++)

[Code]...

The output for the int array 1,1,3,0,3,2,0,4,1,3, looks something like this:

Integer: 1, Times: 3
Integer: 1, Times: 3
Integer: 3, Times: 3
Integer: 0, Times: 2
Integer: 3, Times: 3
Integer: 2, Times: 1

[Code]...

and as you can see lines are repeated for each time the same integer occurs in a new element. i just need my code to skip an element if the integer has been previously encounterd and recorded. i.e i need it to look more like this :

Integer: 1, Times: 3
Integer: 3, Times: 3
Integer: 0, Times: 2
Integer: 2, Times: 1
Integer: 4, Times: 1

with the first encountered integer being checked throughout the array and then moving to the next integer/element which is not the same as any previous.

View Replies View Related

Long Type Output File - Print Prime Numbers Between Given Range Of Numbers

Aug 22, 2014

I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000

public class primenumber {
public static void main(String[] args) {
long start = 5000000;
long end = 10000000;
System.out.println("List of prime numbers between " + start + " and " + end);
for (long i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);

[Code] ....

View Replies View Related

Print Maximum And Minimum Values Of Integers From User Input

Apr 27, 2015

So I'm learning java from a website and I was tasked with creating a simple program which allows the user to enter a series of integers, then finally when they decide to input a non-integer the program will print the maximum and minimum values of the integers they entered. So for example if they entered 5, 4, 3 and 2 then enter a non-integer the program would output 5 (maximum value), then 2 on a new line (minimum value).

Here is my code:

import java.util.Scanner;
public class MaxMinPrinter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

[Code] ....

And this is what the output looks like:

Actual output
-------------------------------------------
Enter an integer: 5
- 10
-
- Enter an integer: -4
- 8
- -6
-
- Enter an integer: 11
- -1
-
- Enter an integer: q
- -1
- -6

When it's supposed to look like this:

Expected output
-------------------------------------------
Enter an integer: 5
Enter an integer: 10
Enter an integer: -4
Enter an integer: 8
Enter an integer: -6
Enter an integer: 11
Enter an integer: -1
Enter an integer: q
11
-6

View Replies View Related

Separating Integers Into Individual Digits And Print Separately By Three Spaces Each

Mar 11, 2014

Write an application that inputs one number consisting of five digits from the user, separates the number into in individual digits and prints the digits separated from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each.

For example, if the user types in the number 42339,

the program should input 4 2 3 3 9.

Here is my code so far but I am stuck.

mport java.util.Scanner;
public class Seperating {
public static void main ( String[] args) {
Scanner input = new Scanner(System.in);
int number = 0 ;
System.out.printf("%d" , number );
System.out.print("Enter integer");
number = input.nextInt();
}
}

but I am not getting the result I wanted what am I doing wrong

View Replies View Related

Convert Array Of Integers To Array Of Characters And Then Print It Out

Feb 13, 2014

I have double checked this code over and over and I just can't find the problem.

What I'm trying to do is take a file and input it into an 2D array.

Ultimately, I should convert the array of integers to an array of characters, then print it out. The file contains a set of ASCII values.

After printing it out, I should then create methods to manipulate the image produced.

Using 2D arrays is a requirement for this exercise.

I think that somehow I'm overcomplicating this and the solution is a lot more simple than I think, but I can't think of what to change.

The error I am getting is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
at main.main(main.java:17)

Java Code:

import java.util.*;
import java.io.*;
public class main {
public static void main(String[] args)
throws FileNotFoundException {
String[][] data = new String[22][40];

[Code] .....

View Replies View Related

Increment Counter By Recursion

Mar 22, 2015

I have a question related to the code below, that I do not understand. The aim is to count all files and subdirectories in an ArrayList full of files and subdirectories. So I have to count every file and every subdirectory.

The code concerning counting files is clear to me - every time d is of the type file I have to increment n by one. However I thought that I have to do the same thing in case d is a directory, so I would have written the same code for directories.

So what does "n += ((Directory) d).countAllFiles();" mean? In my understanding the method countAllFiles() is applied again on the object Directory ( as Directory is the class that contains this method), but how is n incremented by this? I thought n should be incremented by one as we did with files.

public int countAllFiles() {
int n = 0;
for(SystemFile d : content) {
if(d instanceof File) {
n++;

[Code] ....

View Replies View Related

Increment Array Values

Jul 5, 2014

I'm attempting to increment the values by 1 in an array of objects but I'm not able to increment with the increment operator.

for(int i=1;i<a.length;i++){
a[i].getHour(); hour = hour++;
a[i].getMin(); miinute = minute++;
a[i].getSec(); sec = sec++;
}

It just loops the value of hour without incrementing.

View Replies View Related

For Loop To Display And Increment?

Mar 1, 2015

I'm trying to display an columns and rows. The first column has integers and the rest are doubles. The initial calculations are correct, but I cannot figure out how to total last column, which is interest paid, and then start the iteration over.

int paymentNumber;
//print out the colum labels
System.out.printf("%7s %9s %10s %8s %15s %n", "Payment", "Interest"
, "Principal", "Balance", "Interest Paid");
System.out.print(" "); //put three blank spaces first
//Test For Loop Output:
for ( paymentNumber = 1; paymentNumber <= period*12; paymentNumber++) {

[Code] .....

Here's the output: It displays the payment number correctly. Am I supposed to write a for loop for each column?

run:
Please enter the amount of the loan:
1000
Please enter the interest rate:
1
Please enter the loan period in years.
1
Your Monthly Payment is:$83.79

[Code] ....

View Replies View Related

How To Call Increment Method

May 26, 2015

I have the following:

public class HistoricalMoment{

private String eventName;
private ClockDisplay timeOfEvent;
public static final int MIDNIGHT_HOUR = 00;
public static final int MINUTE_ZERO = 00;
}

How can I create a method called public void addMinuteToTimeOfEvent() which calls the timeOfEvent's increment() method to add one minute to the timeOfEvent?

View Replies View Related

How Does Post Increment Work

Mar 5, 2014

I have a code issue and I have a below question for the same.Assuming data[] is of type byte[], is the below statement in java -

while((data[p++] & 0x80) == 0x80);

same as -

while ( (data[p] & 0x80) == 0x80 ) { p++; }

I tried with do { p++; } while ( (data[p] & 0x80) == 0x80 ); That also doesn't seem to be the same thing. I am not putting code around this but essentially if I make this change for not using data[p++] code stops working!

View Replies View Related

Increment / Decrement Buttons

Nov 17, 2014

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
public class IncrementDecrementPanel extends JPanel {

[Code] ....

View Replies View Related

How To Increment URLs In String

Apr 13, 2013

I need open a url every time I want to refresh system with an update. Each update has a number so my urls are url1, url2, url3, ... I know how to put it in a loop to increment the urls in a String. I do not know how to make it open and close the url in a browser.

public class Test {
public static void main( String args[]) {  
for (int i = 201; i < 300; i++){
System.out.println("http://url=" + i);

[Code]....

View Replies View Related

How To Automatically Increment ID And Then Display Only One ID In Report

Jun 7, 2014

The program shall assign a new employee ID to the employee and display it on the screen.

The employee ID shall be generated by adding 1 to the largest employee ID already in the employee.txt data file.

You can see I've tried variations of identification++ but have not been successful. I've also tried to get the last or the highest identification in the arrayList but have not done that right.

public static void addEmployee() {
String firstName = Validator.getString(
sc, "Enter First Name: ");
String lastName = Validator.getString(
sc, "Enter Last Name: ");
int identification = 0;

[Code] ....

I also can display all of the employees their identifications and their punches but I cannot narrow it down to searching one employee and displaying information for just the one.

-If the selected value is ‘I’, prompt the user to enter the employee’s ID number.
-If ‘I’ is selected the display shall show the employee’s name and ID, list out each day worked in chronological order, the number of hours worked that day and a total number of hours worked in the two week period.

The report shall prompt the user to re-enter an employee ID of it does not exist in the employee file.

private static void displayReports() {
System.out.println("Report");
Scanner sc = new Scanner(System.in);
String action = " ";
while (!action.equalsIgnoreCase("d"))

[Code] ....

View Replies View Related

Increment Array And Got Unexpected Result

Aug 16, 2014

Java Code:

char array[] = new char[10];
int i = 0;
array[i++] = (char)('A' + i);
System.out.println("char for array is: " + array[0]); mh_sh_highlight_all('java');

However, the result for array[0] is B. Not A. from my understanding, the array[0] should be 'A'.

View Replies View Related

GUI Bars - Increment Height By About 10 Pixels

Nov 12, 2014

So I'm trying to make a bar graph, each bar will increment the height by about 10 pixels. To do this, I'm making an array of bars. For some reason, using fillrect I cannot not increase the size more than 10 by 10, if I do, it will only show the 10 by 10. I want the bars to increase in height by 10 each time.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AssortedAssortmentsOfSorts extends JFrame

[Code] .....

View Replies View Related

Increment Array And Got Unexpected Result

Aug 16, 2014

char array[] = new char[10];
int i = 0;
array[i++] = (char)('A' + i);
System.out.println("char for array is: " + array[0]);

However, the result for array[0] is B. Not A. from my understanding, the array[0] should be 'A'.

View Replies View Related

JSP :: Increment Variable From JavaScript - Function Is Undefined

Nov 13, 2014

I've a simple logic Code to increment a variable from javascript and Send it back to jsp. I just writing simple code to explain the logic. My code will have scritplets. I know usage of scriptlets is inside jsp is oldest but for the sake of logic.

<!--Jsp Page -->
<%
int i=0;
<input type="button" onclick="fun()"/>
%>
<!-- Script as Follows-->
<script>
function fun(){
<%i++;5>
}
</script>

But My value of i is not incrementing its showing that function is undefined

View Replies View Related

Increment Variable Each Time It Goes Through For Enhanced Loop

May 28, 2014

I am trying to increment a variable named counterVariable each time it goes through a for enhanced loop. It is stuck on 0.

Java Code:

valuesProcessor:
for(String[] row : values) {
for(String col : row) {
int counterVariable = 0;
if(col == null)
break valuesProcessor;

[Code] .....

View Replies View Related

Which Axis To Increment / Decrement With Inclusion Of Speed Variable

Jan 30, 2015

if I move object A to one area of the screen I want object B to to move to object A's location but I also want object B to move at a fixed speed (movement variable). How do I go about doing this? Both the x and y coordinates of object B would need to know the coordinates of object A to calculate the distance between the two and to determine how much of which axis to increment/decrement (if that makes sense?) with the inclusion of the speed variable.In other words I'm just trying to create a homing object.

View Replies View Related

For Loop Program - Increment Hours By 12 And Double Population By 2 Each Time

Oct 3, 2014

So I need to make a for loop for this problem: A certain type of bacteria doubles its population every twelve hours. If you start with a population of 1000, how many hours will it take for the population to exceed 1,000,000? Output needs to be in table format, such as:

Hours: - Population:
0 ------- 1000
12 ----- 2000
24 ----- 4000

I've created the code, but don't understand how to increment hours by 12 and double the population by 2 each time.

public class Population
{
public static void main (String[] args) {
int hours = 0;
int population;

[Code] ....

View Replies View Related

Amount Of Spell User Uses - How To Increment Initial Randomized Output

Feb 16, 2015

I created my own class and it basically randomizes the amount of 'spells' [magic] a user uses (range 1-5). I can't figure out how to increment the initial randomized output to make something like this :

User X : uses 2 spell.
User Y : uses 3 spells.

User X : uses 3 spells. (Taking the randomized 2 and increment by 1)
User Y : uses 4 spells. (Taking the randomized 3 and increment by 1)

My current setup looks like this : First one is the class I created and the second one is just how the output would look like. (seperate by +++++...)

My goal is to create a METHOD to increment it.

public class Spell {
// Max number of spells
private final int MAX_FACE_VALUE = 5;
// Current face value
private int faceValue;
// New instance of Spell
public Spell() {
faceValue = 1;

[Code] ....

+++++++++++++++++++++++++++++++++

public class SpellOutput {
public static void main(String[] args) {
//Object creation
Spell spellCasterOne = new Spell();
Spell spellCasterTwo = new Spell();
Spell spellCasterThree = new Spell();

[code] ....

View Replies View Related

When Remove Increment Operator Program Shows Invalid Statement

Feb 19, 2015

class Brain56
{
public static int[] get()
{
return null;
}
public static void main(String...a)
{
int k=1;
get()[k]++;
}
}

when i remove the increment operator(get()[k]) program shows invalid statement may i know the reason behind it?

View Replies View Related







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