Find If Number Entered By User Consecutive Or Not

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


ADVERTISEMENT

Find ASCII Number Of Names Entered

Sep 12, 2014

I am having a problem with SD array .i have to find the ASCII no. of names entered.

import java .io.*;
class ASCII
{
public static void main()throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);

[Code] .....

View Replies View Related

Math On Three Numbers - Get Average / Sum And Number Entered By User

Feb 25, 2015

Java program : For this assignment you are going to write code for the following class:

MathOnThreeNumbers

Here are the specifications of class MathOnThreeNumbers:

Methods of class MathOnThreeNumbers:

1. inputThreeNumbers
2. getAverage
3. getSum
4. getNumberOne
5. getNumberTwo
6. getNumberThree

Constructor of class MathOnThreeNumbers

write a constructor that initializes the first, second, and three numbers to 1, 2, and 3 respectively.

Specs for the methods methods:

1.
name: inputThreeNumbers
accessibilty: public
arguments: none
what it does: asks the user for three numbers of type double

2.
name: getAverage
accessibilty: public
arguments: none
what it does: returns average of the three numbers

3.
name: getSum
accessibilty: public
arguments: none
what it does: returns sum of the three numbers

4.
name: getNumberOne
accessibilty: public
arguments: none
what it does: returns the first number entered by the user

5.
name: getNumberTwo
accessibilty: public
arguments: none
what it does: returns the second number entered by the user

6.
name: getNumberThree
accessibilty: public
arguments: none
what it does: returns the third number entered by the user

Here is an example of how the class MathOnThreeNumbers works. The following code produces the output displayed after the code.

MathOnThreeNumbers mm = new MathOnThreeNumbers();
System.out.println("first: " + mm.getNumberOne());
System.out.println("second: " + mm.getNumberTwo());
System.out.println("third: " + mm.getNumberThree());
mm.inputThreeNumbers();

[Code] .....

View Replies View Related

Take User Input And Display Sum Of Digits Of Entered Number

Nov 6, 2014

I have returned with yet another problem but I think this one might be a bit easier to fix.

So the whole MO of this program is to take user input, and display the sum of the digits of the number they entered. I am supposed to do this by utilizing methods. So far this is what I have and when I compile it tells me that it "cannot find symbol", which I don't understand as I define what "m" is in the for loop. The other error is that it says:

"Exercise6_2.java:22: error: incompatible types: possible lossy conversion from long to int
return result;
^

I don't understand why it's giving me this error nor do I understand why result seems to inherently be an int. (Also the public static int sumDigits(long n) method was given to me by the book so that is what I am supposed to use).

import java.util.Scanner;
public class Exercise6_2 {
public static void main(String[] args) { 
Scanner input = new Scanner(System.in);
System.out.println("Enter a integer");
long n = input.nextLong();

[Code]...

View Replies View Related

Simple For Loop - Determine Factorial Of A Number Entered By User

Jun 28, 2014

I'm trying to learn Java and my current project is to write a short program to determine the factorial of a number entered by the user. I haven't looked. There may be a method that will do it, but I want to use a for loop specifically.

What I have compiles just fine. I'm actually pretty thrilled just with that. Here is what I have:

class factorial {
public static void main( String[] args) {
Scanner scan = new Scanner(System.in );
int num;
int product = 1;

[Code] ....

View Replies View Related

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 View Related

Creates Array Of 3 Consecutive Ints (0 - 7) User Has To Guess What Those Numbers Are

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

Adding Number Of X Consecutive Values In Array And Get Minimal Total Value

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

Prompt User For Integer To Make Sure Length Entered By User Is A Power Of 2

Oct 8, 2014

Basically I need to make a program prompts the user for an integer, check to make sure the length entered by the user is a power of 2 and at least 2. Then I need to convert from base e to base 2 then I need to print the tick marks of a ruler based of the value of the length.

I got the whole converting thing working and the check for a power of 2, that wasn't an issue because it didn't require any recursion. I just don't know how to print out the proper tick mark values.

The way it is supposed to work is that it needs to look like this. Say the user enters 8;

012131210
012345678

Or if the user enters 16;

01213121412131210
01234567890123456

The bottom row is pretty much just the index value, that I print fine.

The top row though is supposed to be the length of the ticks on a ruler, with the middle most value being the value of the conversion from base e to base 2 from above. I can get that printed and what I get just looks like this.

For 8;

000030000
012345678

For 16;

00000000400000000
01234567890123456

As you can see I can get the middle value and the index values below but I don't know how to use recursion to get the right numbers.

Here's my current code.

import java.util.*;
public class TickMarks {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
boolean looping = true;
while(looping == true){
System.out.print("Please enter a length: ");

[Code]...

Now the methods isPowerOfTwo() and printLength() both work fine. However, I'm not sure how to get the left and right side working.

My thoughts were to split the length in half and get a left and right side. I gave both of them arrays so I can keep track of the values. Now as you've seen above I get all zeros and I know it's because that's the default value in an array, I just don't know how to get the proper values.

View Replies View Related

How To Initialize To First Number Entered

Aug 10, 2014

initialize the min and max to be the first numbers entered, need to improve the code, because if the number entered is < 0 there is an error. but i dont understand how i would initialize to the first number entered, my understanding is tha if the user enters a 5 , then i should initialize to value 5, but i cant anticipate what the user will enter. I could initializa to null, but that still is not the first number entered.

int min = 0;
int max = 0;
int option = JOptionPane.YES_OPTION;
while ( option == JOptionPane.YES_OPTION){

[code]....

View Replies View Related

Validate Date When Entered By User

Oct 13, 2014

Below is my current program for my java class. The assignment is to validate a date when entered by user.

import java.util.*;
public class Date{
public static void main (String arg[]){
int month = 0;
int day = 0;
int year = 0;

[Code] ......

My issue with my program is the output for the day.

If I enter 30 for the date I get this: Date is invalid
Date is invalid
Date is invalid
Date is invalid
Date is invalid

I wanted to know how what is wrong and why it is doing this.

View Replies View Related

Identifying Hashes That User Has Entered

Jun 18, 2014

Before continuing a task, any more accurate way to identify hashes? Currently, I am using regex + length of hash to give the user possible hashes.

Identifier class:

package votek;
* Purpose: Class is used to run checks on possible hashes that the user has entered.
*/
public class Identifier {

//Hash entered by user is stored here
private String hash;
//String that will contain all possible hashes to share with user
private String results = "Possible Hashes: ";
//Method that runs the other hash methods checks and prints out the results.

[Code] ...

View Replies View Related

If Statements - Each Number Entered In Greater Than Zero

May 9, 2014

write a program that will ask the user to enter five numbers.using If statements if each number entered in greater than zero

import.java.util.Scanner;
public class Java3 {
public static void main(String[] args) {
function addNumbers(n1, n2, n3, n4, n5){
var finalNumber = 0;

[Code] ....

addNumbers(1, 2, 3, 4, 5,);

View Replies View Related

JSP :: How To Format Entered Number On Textbox

Apr 24, 2014

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber type="currency" value="12345.6789"/></p>
<input type = "text"/>

This one is working if i set value in fmt but what i want to happen is when i enter a number in textbox it will be fommated on the format that i want, so i think i need to an of og text and set it into value of fmt? <y plan is when I enter a number in textbox it will automatically formatted ....

View Replies View Related

Swing/AWT/SWT :: GUI To Add A List Of Numbers Entered By The User

May 9, 2014

I have a GUI with a textArea for the user to input numbers, a button which should "listen" for those numbers, and then a textField to display the sum. I have my code working for user input of one number; but I'm at a loss as to what sort of loop I need to create to get it to read each line of input. I wasn't sure whether to put this in the beginner forum or here, because I am definitely a beginner.

So my code thus far that works with one input:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

[Code] ....

View Replies View Related

Using While Loop For Printing User Entered Name From Console

Jul 2, 2014

Ask user to enter a name alphabet by alphabet, and print d whole word as output,,,,,, use while loop?Since i am new to JAVA..i have no clue on how to begin?

View Replies View Related

Creating Playlist From Songs That User Has Entered

May 3, 2015

I'm trying to implement a playlist class that creates a playlist from songs that the user has entered. Basically, it'll have the following requirements:

There can be a maximum of 2 playlists.
Each playlist holds a maximum of 3 songs.
Can't be over 12 mins.
Can't be over 10 MB.
Can't use arrays (a real pain).

I'm really struggling with how to even go about this. I'll post majority of my code below, I know a lot of it can be condensed/improved but I'm just trying to focus on functionality at the moment.

Interface class:

public class Interface
{
Scanner console = new Scanner(System.in);
private SongDatabase database = new SongDatabase();
private Playlist playlist = new Playlist();
private int songCount=0;

[Code] .....

In short, the aspects that are relevant to the playlist functionality are the addSongtoPlaylist() method in Interface class, the getSong() method in SongDatabase class and the Playlist class.

View Replies View Related

Allow Input To Loop Until 0 Is Entered For Product Number

Sep 15, 2014

What I am trying to do here is allow input to loop until 0 is entered for the product number. When 0 is entered, it should then dump the total for each individual product. I've tried it about a dozen different ways and have yet to be able to get the loop to function as intended. The way I have the code below, the loop will not function at all (where as before it looped, but never finished).

import java.util.Scanner;
public class Sales {
public static void main(String[] args) {
double total1=0.0;
double total2=0.0;
double total3=0.0;
double total4=0.0;
double total5=0.0;
int product;

[Code] ......

View Replies View Related

Draw V-shape Of Height N Entered By User With Stars

Apr 12, 2014

Am stuck in a problem that asks to write a program using nested for loops to draw a V-shape of height "n" entered by the user with stars .

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

Continuously Get User Input Until Empty String Entered

Jul 26, 2014

I have the code below that just keeps getting the user's name and displaying it until the user enter's an empty string. Well, to simulate that, I just hit the keyboard instead of entering any name but for some reasons I am not seeing in my code, the programme just keeps looping.
 
System.out.println("Enter your name :
");
Scanner st = new Scanner(System.in);
while(st.hasNext()){
System.out.println("Enter your name :
");
String name = st.nextLine();
System.out.println(name);
if(name==" ") break;
}
System.out.println("you are out of the while loop now!!");

View Replies View Related

Perform Mathematical Calculation Between Two Numbers Entered By User

Feb 16, 2015

I need to write a Java program to perform a mathematical calculation between two numbers entered by the user. User has to choose the mathematical operation and input it and then when the user enters 2 numbers, he gets the answer. When user enters any other character other than *, /, + and - he should be able to exit. Perform calculation between numbers should be until user decided to exit from the program. My code is below, calculation part goes well, but can't get it exit when user enter any other character.. How to fix it?

import java.io.*; 
public class q11

public static void main(String[]args)throws IOException
{
InputStreamReader ISR=new InputStreamReader(System.in);
BufferedReader BR=new BufferedReader(ISR);
while(true){
System.out.println("Enter..");
System.out.println("* : For multiplication");

[Code]...

View Replies View Related

How To Make Button Clear The Data That User Entered

Mar 25, 2015

package com.example.imc;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

[Code] ....

View Replies View Related

Read Integer From Keyboard Until Negative Number Is Entered

Apr 8, 2014

Basically i have a question ask me to write a program that read integer from keyboard until a negative number is entered, and then print out the max and min out of the number i have entered.

This is what i have done already which works, but i need to ignore the negative number when print out max and min...which i dont know how to do so. when i compile this program now it will always give me the negative number i enter at the end as minimum. which i need to ignore

Also if negative number is entered at once the program should print a error message say max and min are undefined.

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int large = Integer.MIN_VALUE;
int small = Integer.MAX_VALUE;
while(true) {
System.out.print("Enter a integer: ");
int n = in.nextInt();

[Code] ....

View Replies View Related

Not Reading All Lines While Comparing User Entered Name To The One That Exists In File?

Jan 21, 2014

Logic her eis when user selects option1, it asks for user to enter name and as soon as user enters name, it should compar name to existing names in txt file. I have user while loop and for loop but for some reason, it doesnt compare properly as there seems to be some mistake in looping and it just read first line or you can say single line rather than comparing it with all lines in txt file. i have attached votes.txt file with this. Also, if user doesnt exists infile, it will ask user to enter vote as yes or no and add it to file and then count total number of Yes and No votes from file and compare them.

my votes.txt file looks as below with two columns namely (name and vote).

Hiren No
samir yes
bob no
rikul no
master yes
patrick no
summer yes
bhanja no

package samples;

import homework.EmployeeA;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

[code]....

View Replies View Related

Store Name SD Array And Display Room And Floor Number Whose Name Is Entered

Sep 12, 2014

I have to store the name SD array and display the room and floor no. whose name is entered but i am having a problem with the logic part. The answer is coming to be wrong

import java .io.*;
class hotel
{
public static void main()throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
String ar[][] = new String[5][10];

[Code] ....

View Replies View Related







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