Program To Find If Numbers Are Consecutive Or Not
Feb 23, 2014
im trying to do a program to find if numbers are consecutive or not! if they are consecutive i need a true as a return and a false when they are not... i did this program and im sure i did something wrong because i keep only true returns ..
Example: (3,5,7) (1,2,2) (7,7,9) should return a false!
Java Code: import java.util.*;
public class Consecutive{
public static void main (String [] args){
Scanner console= new Scanner(System.in);
System.out.println("Enter three numbers");
String numbers = console.nextLine();
System.out.println( "The numbers (" + numbers + ") is '" + consecutive( numbers ) + "'" );
}//end of main
[code]...
View Replies
ADVERTISEMENT
Feb 21, 2014
I need to find if the number entered by the user are consecutive or not!
Ex:this should return a true!! This should return false!
(1,2,3) (3,5,7)
(3,2,4) (1,2,2)
(-10,-8,-9) (7,7,9)
My program seems to work ok when i enter number in order like 1,2,3 = true , and all the numbers for false seem to be working as well! my problem is when i enter number like 3,2,4 that are not in order but still are consecutive!! I thought that another if statement would be the solution but i have tray several different ones and still can't make it work !!!
Java Code:
import java.util.*;
public class Consecutive{
public static void main (String [] args){
Scanner console= new Scanner(System.in);
System.out.println("Enter three numbers");
String numbers = console.nextLine();
[[Code] .....
View Replies
View Related
Feb 26, 2015
I wanted to know if I was off to the right start. I am trying to write a program using the for loop the calculate the product of the consecutive numbers 4 through 8 but so for I am getting 3 values output and I only want 1 value at the print out.
The code I am using outputs the numbers too large. I am trying to see where I went wrong.
for ( int i = 4 ; i <= 8; i++)
{
int j = i++;
int k = j++;
int l = k++;
int m = l++;
System.out.println( + (i*j*k*l*m) );
}
View Replies
View Related
Aug 16, 2014
I have the following program. In a nutshell, I creates an array of 3 consecutive ints and the user has to guess what those numbers are, knowing that they are between 0 and 7, this is from Head First Java. In the book, the code has a bug that is made on purpose and they challenge you to fix it. As you can see bellow, every time a user's guess matches a int in the array, the NumOfHits is increased by one. When the NumOfHits is 3 (the length of the array) the game finishes.
The bug is that if you guess 1 and 1 is in the array, if you type in 1, 3 times, I will increase the NumOfHits 3 times and end the game as if you had won. To solve the bug, you need to find a way to tell the program that if the user already guessed that number, it should no longer be taken into account and we shouldn't increase the NumOfHits if the same number is provided.
I "fixed" this by searching for the index of the number that matches an int of the array, and changing that index's value to 100, since the user knows that the numbers are between 0 and 7, they will not submit 100.
The result? If the user submits 2 and 2 is in the array, the value of its indexed is replaced by 100, so that if the user submits 2 again it doesn't count as a match.
Please see the comments in the code
import java.io.*;
import java.util.Arrays;
class DotCom{
int NumOfHits = 0;
int[] LocationCells;
public void setLocationCells(int[] locs){
LocationCells = locs;
[Code] .....
The code works, but for some reason, some times I get a ArrayIndexOutOfBoundsException run time error and it appears to be somehow random. This is what I saw on the command line earlier
C:Userspablo.alvarez>java launchDotCom
Enter a number: 3
missed
Enter a number: 4
hit
Enter a number: 5
hit
Enter a number: 7
missed
Enter a number: 5
missed
Enter a number: 4
missed
Enter a number: 3
missed
Enter a number: 4
missed
Enter a number: 5
missed
Enter a number: 6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at DotCom.checkYourSelf(launchDotCom.java:16)
at DotComGame.startGame(launchDotCom.java:59)
at launchDotCom.main(launchDotCom.java:72)
As you can see here, 3 returned 'missed' but 4 and 5 returned hit. Since the numbers are in sequence and are only 3, it makes sense that the last number is 6 (4,5,6). You will then notice that when I submitted 3 and 4 again it returned 'missed' as expected, because the numbers were already matched earlier, but then when I submitted 6, it returned the error seen above.
Sometimes this doesn't happen at all, sometimes it happens at the 2nd guess or third, it appears to be somehow random. I'm familiar with the error message, but I don't see where I'm using an index that doesn't exist in the array. The only time I'm referencing an array index is on
LocationCells[index] = 100;
but the index variable will only hold a value if the int submitted by the user matches on of the values in one of the indexes, so how is it that I'm going over the number of available indexes?
View Replies
View Related
Jan 23, 2015
I have to write a program that find the sum of two numbers 62 and 99 and stores them in a variable named total. However, I have one error that I just can't get rid of and can't tell what it is. I'm using jGrasp and here's what it says:
Programming Challenge #5.java:14: error: class SumofTwoNumbs is public, should be declared in a file named SumofTwoNumbs.java
public class SumofTwoNumbs {
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
and here: is my code:
import java.util.Scanner;
/*
* 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.
*/
/**
// This program has variable of several of the integer types.
public class SumofTwoNumbs {
public static void main(String[] args) {
[code]....
View Replies
View Related
Oct 10, 2014
I want to find the prime palindrome numbers less that a given number by my program. Here is my code, I am trying to find the method to solve the errors when I compile it. It said variable a might not have been initialized in line 41,62,86.
import java.util.Scanner;
public class Lab5{
public static void main (String[] args){
System.out.println("Please input a number");
Scanner Input=new Scanner(System.in);
int num = Input.nextInt();
[Code]...
View Replies
View Related
May 23, 2014
So, if you are a football/soccer fan, you know that in every FIFA World Cup you have 4 nations. From those 4, 2 will qualify to the Knockout stages.
Let's say, after all the group fixtures are played, the standings are as follows :
1. England - 6 points
2. Spain - 4 points
3. Brasil - 3 points
4. Germany - 1 point
I want to know how I can select ONLY England and Spain for the Knockout stage of the FIFA World Cup (The class I am working on).
View Replies
View Related
Jun 17, 2014
Writing a Java program to find sum of numbers in a random string.
(Input=abc235^%$q!12&3 output=250)
If a number has - sign then u should consider it as a negative number.(Input=abc-12t%^&$dcf22 Output=10)
View Replies
View Related
Aug 23, 2014
Write a program to find maximum between three numbers using ternary operator.
View Replies
View Related
Feb 23, 2014
The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.
Here is what I have so far:
double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 };
ArrayList<Double> d = new ArrayList<Double>();
ArrayList<Double> e = new ArrayList<Double>();
for ( double x : q ) {
d.add( x );
e.add ( new Double ( x ) );
[Code] .....
Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.
View Replies
View Related
Oct 3, 2013
I should mention here that number of elements can be from 1....10'000. And it has to be done as O(N)
I attempted at the problem like so :
public static int adjacencies(int[] A) {
int count = 0;
boolean found = false;
for(int i = 0; i < A.length-1; i++) {
if (A[i] == A[i+1]) {
[Code] .....
To me it looks right and it seems to work.
View Replies
View Related
Jan 16, 2015
I am doing a sieve code to find prime numbers.
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter the maximum number ");
int maxNumber = scanner.nextInt();
int[]numberList = createList (maxNumber);
[code]....
I have a problem with the output. It only get as far as marking the multiples of 2. It does not mark the multiples of 3 and so on.Example output: If the range is from 2 to 15 the output is :2, 3, 0, 5, 0, 7, 0, 9, 0, 11, 0, 13, 0, 15 note: 0 means its marked.As you can see it only marks multiples of 2 but it does not mark multiples of 3 above. I think the problem lies in the for loop inside my main method, because the index does not increment.
View Replies
View Related
Jul 10, 2014
I need to find the largest value in a scanned file.I've gotten the count, sum, average, evens, and odds myself.The code above the while loop is not mine and my professor said I may no edit it or other wise mess with it. I also may not use arrays.Also I've realized that the largest/smallest are recording the value of count. I've tried the following:
Scanner infile = new Scanner ( new FileReader(args[0]) );
int count=0,sum=0, largest=Integer.MIN_VALUE,smallest=Integer.MAX_VALUE, evens=0, odds=0;
double average=0.0;
[code]....
View Replies
View Related
Feb 5, 2014
I am trying to find 3 largest numbers in an array using a single For loop but my following code is still showing randomly sorted numbers.
public class largest3 {
static int m, n, o;
static int array[] = new int[100];
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
array[i] = (int) (Math.random() * 100);
[Code] ....
View Replies
View Related
Mar 23, 2015
import java.util.Scanner;
public class AvgLrgSml{
public static void main(String[]args){
System.out.print("Hello there. Please enter any three numbers.");
Scanner keyboard = new Scanner(System.in);
double num1 = keyboard.nextDouble();
double num2 = keyboard.nextDouble();
[Code]...
View Replies
View Related
Aug 12, 2014
If you aren't familiar with the euler problem 67, you are given an array that is a set of numbers in a triangle. Like this
3
7 4
2 4 6
8 5 9 3
and you have to find the maximum path, which for this one is
(3)
(7) 4
2 (4) 6
8 5 (9) 3
I have solved this problem iteratively with the code below
depth = depth-2;
while (depth >=0) {
for (int j = 0; j <= depth; j++) {
values[depth][j] += Math.max(values[depth+1][j], values[depth+1][j+1]);
}
depth -= 1;
}
depth is a variable for the row in the triangle. My problem is that i need the solution to be recursive and i am having trouble doing this. So far i have
public static int findMax(int[][] array,int depth) {
if (depth==0)
return array[0][0];
else if
}
View Replies
View Related
Dec 2, 2014
I am trying to write a code that asks the user to input ten numbers and then finds and displays the smallest number out of the ones given. I am supposed to implement arrays into the program to do this. But the problem I have run into is that when I compile the code in jgrasp, I am given several error messages and I am not quite sure what I have done wrong. I'm assuming it is either a syntax or a logical error on my part but reading over the code I do not understand what is causing these errors.
This is the most current draft of my code:
import java.util.Scanner;
public class Exercise7_9 {
public static void main(String[] args) {
double[] numbers = new double[10];
//Enter ten double numbers: Scanner(System.in)
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter ten numbers: ");
[Code] ....
/* Sample Run:
Enter ten numbers: 1.9, 2.5, 3.7, 2, 1.5, 6, 3, 4, 5, 2
*/
And these are the exact error messages:
----jGRASP exec: javac -g Exercise7_9.java
Exercise7_9.java:35: error: '.class' expected
if (double m > list[i]) {
^
Exercise7_9.java:35: error: illegal start of expression
if (double m > list[i]) {
[Code] .....
View Replies
View Related
Jun 20, 2014
the prime numbers from 1 to 2500 can be obtained as follows. From a list of the numbers of 1 to 2500,cross out al multiples of 2 (but not 2 itself). Then, find the next number (n, say) that is not crossed out and cross out all multiples of n (but not including n).
Repeat this last step provided that n has not exceeded 50 (the square root of 2500). The numbers remaining in the list (except 1) are prime. Write a program which uses this method to print all primes from 1 to 2500. Store your output in a file called primes.out.
View Replies
View Related
Mar 24, 2014
I don't remember where but I faintly recall one of the oracle docs examples (dealing with GUI basics I think) having something similar to
button.add(lbl).addIcon().addActionListener(al);
but only in the aspect of using .method.method.method
The reason I'm asking is because I want to shorten this
int j = 0;
for(int i = 0; i <= 6; i+=3){
pnl[j+2].add(lbl[i+5]);
pnl[j+2].add(lbl[i+6]);
pnl[j+2].add(drop[j]);
pnl[j+2].add(lbl[i+7]);
pnl[j+2].add(btn[j]);
j++;
}
Into something like this
int j = 0;
for(int i = 0; i <= 6; i+=3){
pnl[j+2].add(lbl[i+5])
.add(lbl[i+6])
.add(drop[j])
.add(lbl[i+7])
.add(btn[j]);
j++;
}
I might be way off though... but I'm just wondering under what circumstances would this be possible
When I try it in Eclipse a little notification appears that says...
"The method add(PopupMenu) in the type Component is not applicable for the arguments (JLabel)"
When I google 'Component' I see it appears to have something to do with Serialization (which I'll need to look into)
View Replies
View Related
Feb 13, 2014
I have the following code that supposed to perfrom sorting on the linked list using method sort in order usind node concept of Linked List but seems to give inlogic results. the following code tests only the values lower than the first value but i can't manage to sort the data higher than the first entered value;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
public class ListNode<T extends Comparable> {
ListNode<T> nextNode;
T data;
public ListNode(T item)
[Code] .....
View Replies
View Related
Nov 9, 2014
if I want to add a number of X consecutive values in an array and get the minimal total value along with the indexes that caused this result, how can I do that? for example:
X = 2
array[4] = (5,2,8,6,7)
start adding every 2 consecutive values as following:
array[0]+array[1] = 5+2 = 7 //minimal value
array[1]+array[2] = 2+8=10
array[2]+array[3] = 8+6= 14
array[3]+array[4] = 6+7=13
output:
minimal value = 7
indexes of minimal values are: 0 and 1
View Replies
View Related
Nov 9, 2014
Here is my problem: if I want to add a number of X consecutive values in an array and get the minimal total value along with the indexes that caused this result, how can I do that?
For example:
X = 2
array[4] = (5,2,8,6,7)
start adding every 2 consecutive values as following:
array[0]+array[1] = 5+2 = 7 //minimal value
array[1]+array[2] = 2+8=10
array[2]+array[3] = 8+6= 14
array[3]+array[4] = 6+7=13
output:
minimal value = 7
indexes of minimal values are: 0 and 1
View Replies
View Related
Jun 26, 2014
How to find if JVM is 32 or 64 bit from Java program....
View Replies
View Related
Feb 5, 2014
I have made a program which finds the number of occurrence of the words. It is as follows :
public class B {
public static void main(String[] args) {
Map<String, Integer> mp = new LinkedHashMap<String, Integer>();
String s = "This is me tarun ohri This";
Scanner p = new Scanner(s);
int i = 0;
[Code] .....
Output is coming {This=0, is=1, me=1, tarun=1, ohri=1}
View Replies
View Related
Apr 25, 2015
I wrote a program that find adjacent value for -1 .ex:
input
1
3 3
1 2 2
1 1 -1
5 5 0
output
1
code:
/*
* 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.
*/
package javaapplication1;
import java.util.Scanner;
[Code] ....
View Replies
View Related
Jan 23, 2015
I have to write a program the calculates the MPG and display's the result on the screen. However, I have three errors I just can't seem to figure out. jGrasp points to the following error, but doesn't tell me what it is:
Programming Challenge #9.java:34: error: cannot find symbol
gallons = keyboard.nextInt();
^
symbol: variable keyboard
location: class MPG
3 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
My code is as follows:
import java.util.Scanner;
/*
* 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.
*/
// This program has variable of several of the integer types.
public class MPG {
public static void main(String[] args) {
[Code] ....
View Replies
View Related