Printing First Number In Input

Mar 21, 2014

Write a Java program ExtractNumber.java to read the data via the keyboard, character by character and print the first number (with or without a decimal point) contained in the input. For example, given the input data: Mary works for $43.75 per hour, the program should print 43.75 to the screen.

When i entered the sentence "The lady works for 8 hours at $10.00 per hour"

ERROR: My output was 810.00 instead of simply the first number 8

View Replies


ADVERTISEMENT

Printing Out Value Of Each Number

Jan 10, 2015

I finally got this to work but only by printing out the value of each number to debug as such, was there an easier way to do it that I missed?

public class Statistics { 
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// variables used
double num1;
double num2;
double num3;
double num4;
double num5;
double num6;
Scanner input = new Scanner(System.in);
//receive number and add it separately

[Code] ....

View Replies View Related

Printing Number In Reverse With Subroutine

Feb 21, 2015

The code below is running, but when I enter the numbers it shows up with what is just below.

----jGRASP exec: java ReverseOrder
 
Please enter 5 integers with no spaces in between: 12345
 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:646)
at ReverseOrder.reverseDigits(ReverseOrder.java:30)
at ReverseOrder.main(ReverseOrder.java:15)

[code]....

View Replies View Related

Printing Number Of Prime Numbers In A Range?

Feb 13, 2015

The assignment is to make a program that prints the number of prime numbers in a range. This is what i have so far. The output is a list of 2s. I created the for loop to cycle through the range of 17-53 and nested a while loop within to test each incident of the for loop to check for divisors starting with 2 until the modulus result is 0 resulting in a false for being a prime number. Then the loop should increment to the next i value. The last part is an if statement that i had intended to add counters to the k variable that would keep track of the number of prime numbers.

boolean isPrime = true;
int j = 2;
int k = 1;
for (int i = 17; i <= 53; i++){
{
while (i % j == 0){
isPrime = false;

[Code] .....

View Replies View Related

Printing Decimal Equivalent Of Binary Number

Feb 28, 2014

This problem is from Java . It asks me to convert a binary number in to decimal. How to program this in java? I am not able to find an effective algorithm to convert in to Java.

View Replies View Related

Printing Unfilled Diamond Based On User Input

Jan 31, 2015

I am new to learning Java and I am trying to print an unfilled diamond using asterisks. The height of the diamond has to be based on user input. My code currently prints out a filled diamond based on user input but I cannot figure out how to print an unfilled one. Every time I change one of the loops it messes something else up.

[public static void diamond(){
int i = 0;
int j = 0;
int k = 0;
int height = 0;

[code]...

View Replies View Related

GUI Program - Convert Input Name To Number

Mar 12, 2015

A GUI program that allows user to enter their name and the program will convert it in number using array.

For example if i input "EUNISE" the output should be 521149195

Because the letter equivalent of

a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
k = 11
l = 12
m = 13
n= 14
o = 15
p = 16
q = 17
r= 18
s = 19
t = 20
u = 21
v = 22
w = 23
x = 24
y = 25
z = 26

View Replies View Related

Program To Add Space Between Input Number

Nov 2, 2014

I am writing a program where whatever number I input in my scanner, it will space them...

For example: If I input 456, the output should be 4 5 6... here is my code:

import java.util.Scanner;
public class midterm {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String number = "";

[Code] ....

the current output for this program is:
456 456 456

View Replies View Related

Input Even Number Then It Will Be Outputted Invalid

Dec 7, 2014

import java.io.*;
public class MagicSquare
{
public static void main(String[]args)throws IOException
{
BufferedReader in=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter a Magic Number: ");
int number=Integer.parseInt(in.readLine());

[Code] ....

//where should i enter a even number that will be outputted "invalid"
//and it only should accept odd numbers

View Replies View Related

Counting Number Of Words From User Input

Jan 2, 2015

I tried the following code, OK:

Java Code:

String temp = "hi this sf hello is new what is this";
String[] cmd = temp.split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

However, when i get the input from user , i didnt get the expected result:

Java Code:

System.out.println("Enter the input string to count the words: ");
String[] cmd = new Scanner(System.in).next().trim().split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

Now Result for above code:

Enter the string to count the words:

hi this is new

words are: 1

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

Determining User Input To Be Prime Number

Feb 12, 2015

Any better way to write a program that takes a user number input and the program determines whether or not the number is prime or not. It was suppose to be a number between 0 and 8,000,000.

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

[Code].....

View Replies View Related

Determine If User Input Is Fibonacci Number Or Not

Oct 20, 2014

Part A: While Loop Program

Write a program that detects Fibonacci numbers. Prompt the user to input a positive integer. Upon input, the program will determine if the number is either a Fibonacci number or not. If a Fibonacci number, then the order of the number in the sequence must be output. If not a Fibonacci number, then the Fibonacci numbers above and below it (including their order in the sequence) must be output. Once it finishes, the program will prompt the user for a new number. The program will exit if the user enters a string (such as “quit”) instead of an integer. Use the sample output file, fib-seq-det.txt, to view a sample session

This is my project, I wrote a programs that tells you if the input number is a fibonacci number or not. For some reason it only works for some Fibonacci numbers but not all of them.

import java.util.Scanner;
public class While
{
public static void main(String[] args) {
System.out.println("Welcome to the Fibonacci Sequence Detector
");
Scanner in = new Scanner(System.in);
System.out.print("Please input a number for analysis: ");
int input = in.nextInt();

[Code] ....

View Replies View Related

Reverse Number - Get Input And Result In Same Line

Feb 11, 2015

I'm having trouble with a program, reversing numbers. I got the program itself to work! However, I am having an issue with the output.

import java.util.Scanner;
public class p1a {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int n;
int reverse = 0;

[Code] .....

If I were to input, say 2341, I would get this: The reverse of 0 is 1432

The 0 should have been 2341, or whatever I input. What am I doing wrong?

I tried to move line 21 : (System.out.println("The reverse of " +n+ " is "+reverse);

Above the while loop, but instead get:

The reverse of 2341 is 0

How can I get my input and my result together in the same line?

View Replies View Related

Too Many Number Of Cases Of Input - Design Pattern?

Apr 16, 2015

For example

Select food;

1. hamburger 2. pizza 3.chicken 4.sandwich .. etc

if I choose hamburger there are choices again

1. big mac 2.Quarter Pounder with Cheese 3.Double Cheeseburger ...etc

If I choose big mac there are choices again!!.

choose the drink you want

1.coke 2.orange juice ..etc

situations like this, what design pattern should I use?

View Replies View Related

Program Should Output Depending On Input Number

Apr 15, 2014

The program should has the output depending the number I'll input. (The number should be from 1-9) . This is the program's output should be:

Input number: 4

Output:
# # # 1 # # # # #
# # # 2 # # # # #
# # # 3 # # # # #
1 2 3 4 5 6 7 8 9
# # # 5 # # # # #
# # # 6 # # # # #
# # # 7 # # # # #
# # # 8 # # # # #
# # # 9 # # # # #

(Example 2):
Input numbers: 8

Output:
# # # # # # # 1 #
# # # # # # # 2 #
# # # # # # # 3 #
# # # # # # # 4 #
# # # # # # # 5 #
# # # # # # # 6 #
# # # # # # # 7 #
1 2 3 4 5 6 7 8 9
# # # # # # # 9 #

(Example 3):
Input number: 5

Output:
# # # # 1 # # # #
# # # # 2 # # # #
# # # # 3 # # # #
# # # # 4 # # # #
1 2 3 4 5 6 7 8 9
# # # # 6 # # # #
# # # # 7 # # # #
# # # # 8 # # # #
# # # # 9 # # # #

I'm not asking the code to make this program, I have trouble to understand the algorithm to make this program ...

View Replies View Related

Add All Even Numbers Between 2 And User Input Number Which Is Included In Addition?

Apr 17, 2014

import java.util.*;
 public class SumOfAllEvens { 
public static void main (String[] args) { 
Scanner s = new Scanner (System.in);
 //for (int i=1; i<4; i++){
int usernumber;

[code]....

I'm supposed to use a for loop that runs until it reaches the number input by the user, but I'm not sure how to tell the program to add the user's number along with all of the even numbers in between the user input and 2.

View Replies View Related

Checking If User Input Matches Any Number In Same Column

Feb 14, 2014

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package firstgui;
import javax.swing.*;
import java.awt.event.*;
public class TicTacToe {

[Code] ......

Program is 10x10 board, I need to check if the user's input is a duplicate of any number in that same column, but I can't figure it out. When I tried it, it always check the same box. For example, if I entered 4 in [1][1] (going by 10x10 grid), it automatically checks right after I entered that [1][1] is the same as my input and erases it. My professor wants me to check it with the "CheckWinner" method.

I tried the following when someone told me to pass the reference of the JButton being clicked to ignore it.

private boolean CheckWinner(JButton source, String inplayer) {
//...
if (EventBoard[i][j] != source &&
EventBoard[i][j].getText().equals( inplayer )){
JOptionPane.showMessageDialog(null, "copy");
EventBoard[i][j].setText("");
}

View Replies View Related

Allowing User To Input A Number And Assign This To Int Variable

Jul 4, 2014

I need the user to be able to input a number, and for the program to assign this value to an 'int' variable. I know how to do this with a 'string' variable:

Java Code:

String options = JOptionPane.showInputDialog(null, "In your decision, how many options do you have?
" +" (NOTE: The maximum number of options = 5, and you must enter your answer as a numeral.)"); mh_sh_highlight_all('java');

But I need to know how to do this with an 'int' variable.

View Replies View Related

5 Digit Integer - Print If Input Number Is Palindrome Or Not

Apr 21, 2015

I have to write a program that inputs a 5 digit integer from the keyboard and prints if the input number is a palindrome or not. I got it to work, but when my result prints it says "0 is a palindrome" or "0 is not a palindrome". How can I get rid of the 0 and replace it with the number input by the user?

import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
int number;
int digit;
int temp;

[code]....

View Replies View Related

Rainfall Analysis For The Number Of Days According To User Input

Feb 24, 2015

I am assigned with a project that will as the user for the amount of days they would like data entered, then loop asking them for these numbers. once it reaches the entered amount of days it will display a menu for which the user can enter the number they want on the list and it will display the data. the menu has 6 items. Total, Average, Determine if its a Drought, Flood or normal rainfall, highest of data entered, lowest of data entered, and 6 is to quit the program.

So far i have total and average working properly. Still looking at how to display drought, flood, and normal.

Main problems, i cannot get highest and lowest to display correct data.

Also when entering 6, or an invalid menu choice, neither "goodbye" or "invalid choice, choose again" gets printed. Rather the loop for entering data continues.

import java.util.Scanner;
public class RainAnalysis {
/*
* Project 1 This program will analyze the rainfall for the number of days a user inputs.
*/
 
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int numOfDays = 0;
double rainfallAmount = 0.0;

[Code] ....

Here is a sample output to show what happens when i put in 6 after the menu is displayed.

also i noticed that it does not say the correct day when displaying the lowest and highest rainfall. this is because i put count, i did not know what else to put in order to get the correct day.

how many days do you want to monitor rainfall?5

Enter the rainfall for day 1.01
Enter the rainfall for day 2.1
Enter the rainfall for day 36
Enter the rainfall for day 43
Enter the rainfall for day 51

Rainfall Data Display Menu:
1 - Display total rainfall.
2 - Display rain average for the period.
3 - Display if drought, flood, or normal conditions.
4 - Display the day and amount of the highest rainfall.
5 - Display the day and amount of the lowest rainfall.
6 - Quit the program.

Enter your choice:
4
Day5 has the highest rainfall with 6.0 inches.

Rainfall Data Display Menu:
1 - Display total rainfall.
2 - Display rain average for the period.
3 - Display if drought, flood, or normal conditions.
4 - Display the day and amount of the highest rainfall.
5 - Display the day and amount of the lowest rainfall.
6 - Quit the program.

Enter your choice:
5
Day5 has the lowest rainfall with 0.1 inches.

Rainfall Data Display Menu:
1 - Display total rainfall.
2 - Display rain average for the period.
3 - Display if drought, flood, or normal conditions.
4 - Display the day and amount of the highest rainfall.
5 - Display the day and amount of the lowest rainfall.
6 - Quit the program.

Enter your choice:
6
Enter the rainfall for day 6

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

Java Programming To Input Number And Display All Its Prime Digit?

Jan 9, 2015

I am new to java programming and using bluej for programming and i have tried this question what i have have given in title ... how to find out the errors in the following program:-

class Primedig {
public void getinput(int n) {
int ctr=0;
while(n!=0) {
int r=n%10;
for(int i=1;i<=r;i++)

[code] .....

output:-

For example input is 243 the output comes only 3 not 2& 3 both.

View Replies View Related

CountDigitx - Take Two Integers As Input And Return How Many Time X Occurs In Number N

Apr 29, 2014

Write a recursive Java method countDigitx that takes two integer numbers as input (a number n and digit x) and returns the number of times digits x occurs in number n:

For example if n=23242 and x=2 then the method should return 3 that is the number of times the number 2 found in n.

View Replies View Related

Count Number Of 7s (User Input) - Illegal Start Of Expression

Oct 15, 2014

I'm attempting to create a program that employs a method to count the number of 7s a user puts as an input into the program. I'm just getting started, but I'm getting an error when I try to implement a main method that says "Illegal start of expression"

What adjustments could I make to eliminate this error message?

// Add a main method here.
public static void main(String[] args) {
//Error occurs here
public static int countSevens(int n) {
// 0. Clean-up. Get rid of any minus signs as that is not a digit.
n = Math.abs(n);

[Code] .....

View Replies View Related

JOption Pane - Output Number With Greatest Value From User Input

Nov 7, 2014

Using JOptionPane,ask for 10 numbers from the user.Use an array to store the values of these 10 numbers.Output on the screen the number with the greatest value.

View Replies View Related







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