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


ADVERTISEMENT

User To Input Integer And Then Outputs Both Individual Digits Of Number And Sum Of Digits

Feb 2, 2015

Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.

First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.

import javax.swing.JOptionPane;
public class DigitsAndSum {
public static void main (String[] args) {
String str1;
String str2;
int int1 = 0;
int int2 = 0;

[Code] ....

View Replies View Related

Prompt User To Input Integer And Then Output Both Digits Of Number And Their Sum

Jun 7, 2015

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. 
 
Now I have a code for spacing out the integers, and I have a separate code for adding the digits. But I am not sure how to merge them together. Both of them work independently
 
Spacing code:
import java.util.*;
public class SumoftheIntegers
{
static Scanner console=new Scanner(System.in);
public static void main(String []args) {
int num1, test, rem;
int counter = 0;

[Code]...

Now the sum of the integers code:
 
import java.util.Scanner;
public class sum
{
    public static void main(String[] args)
    {
        // Create a Scanner
        Scanner input = new Scanner(System.in);
// Enter amount
        System.out.print("Enter an integer: ");
        int integer = input.nextInt();

[Code]...

View Replies View Related

Multiplying Last 2 Digits In A Year From User Input

May 2, 2014

I Have been playing with this for while, and I can not seem to get my head around, how I can do this. I need to get the last two digits of a year that a user will enter. For example, a date like July 2 , 2014, I want to get the last two digits of year 2014 which are "14" and divide it by 4. This will give me 3.5; how ever I will disregard the ".5" and just keep the 14. I have no problem doing the division, my biggest this is how to just get the last two digits of a year. This is what I have so far, it is basically a template of how I want my program; I just need getting the last 2 digits of a year.

public class DateCalc
{
public static void main (String[] args)
{
String month;
int day;
int year;

[Code] ....

" This will give me 3.5; how ever I will disregard the ".5" and just keep the 14 ". I meant that I just want to keep the 3 from the 3.5 that will be generated when you divide 14 by 4.

View Replies View Related

Read From User Input (integer) And Reduce It By Multiplying Its Non-zero Digits

Nov 15, 2014

Write a program that reads from the user an integer and reduce it by multiplying its non-zero digits. The result of the multiplication is a number which is to be reduced as the initial one. This process continues until an integer of one single digit is obtained. For example:

64734502 (6 * 4 * 7 * 3 * 4 * 5 * 2) 20160 (2 * 1 * 6) 12 (1 * 2) 2

Your program should display the number obtained in every iteration.

Sample run1
Enter an integer: 64734502
After iteration 1: 20160
After iteration 2: 12
After iteration 3: 2

Sample run2
Enter an integer: 97737999
After iteration 1: 6751269
After iteration 2: 22680
After iteration 3: 192
After iteration 4: 18
After iteration 5: 8

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

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

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

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

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

Reading Input With Scanner And Checking If User Entered Empty String?

Apr 7, 2015

The project is a program that allows the user to enter students and enter grades for each student. One of the requirements is that if there is already a grade stored for the student that it will display the previous grade. IF the user then enters a new grade the new grade will be stored. IF the user simply presses enter (enters an empty string) nothing is done. I have everything working except for the requirement of doing nothing if the user enters an empty string. If I just press enter at this point I get a NumberFormatException.

The below code is a method "setTestGrades" from the student class. This method iterates through each student object stored in an array list and then checks if there is a previous grade (Requirement# unset grades have to default to -1) before allowing the user to set a new grade.

public void setTestGrades(int testNumber) { //Sets the grade for the specified test number for each student in the arraylist.
testNumber -= 1;
Scanner input = new Scanner(System.in);
for (int i = 0; i < studentList.size(); i++) {
System.out.println("Please enter the grade for Test #" + (testNumber + 1) + " For Student " + studentList.get(i).getStudentName());

[code]....

View Replies View Related

Program That Takes User Odd Number And Print Prime Numbers Lower Than User Input

Nov 4, 2014

I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.

public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;

public void getUserNum()

[code]...

View Replies View Related

GUI To Display Search Results Based On User Input Server Name

Jul 18, 2014

I am using a GUI to display search results based on user's input server name. I am using MS Access as DB and I have the DSN set up correctly.But the search results are displayed for the first typed value in text filed. I am getting same result set every time though i input different server names .

import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
public class SearchResult implements ActionListener{

[Code]...

View Replies View Related

Java Assignment To Get User Input And Display ZIP Codes And Populations

Oct 7, 2014

URL....So the problem is that when I type in "PA" it will display about 24 Zips and Populations before it stops. The problem is in the ZIPs file. It goes down the list and then takes the Zip from the Zips file to the Zips in the Population file and displays the Population. It will go to population 513 and stop. Reason being, there is no ZIP code in the Population file to display a population. The loop then stops. How can I get the program to skip over the zip code when there is no corresponding ZIP code in the other file and continue showing the other Pops..Here's what I currently have completed:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Population {
//Declaring global variables.
Scanner fileScannerZip, fileScannerPop, inputFile;
private String lineZip, linePop;
int invalidZip;

[code]...

View Replies View Related

Display User Input In Lower Case - Why No Output In Second TextField

Jun 2, 2014

I've done a lot of hardwork for this assignment but I don't know what's why the second textfield which is for output is blank.

All I want is to get the input from user in textfield a and display it in textfield b in lower case.

public void KeyPressed (KeyEvent ke) {
String letter2="";
if(ke.getKeyCode()==KeyEvent.VK_A)
{
letter2=letter2+"a";
btextfield.setText(letter2);

[Code] .....

View Replies View Related

Ask User To Enter Number From 1 To 10 And Program Will Display Roman Numeral

Feb 6, 2014

I am trying to write a program that asks the user to enter a number from 1 through 10 and then the program will display the roman numeral for that number.

I am also adding a error message in which i haven't yet because im still trying to figure out how to the program will do the roman numeral.'

I have used the if and else if. but when i input a number it just repeats the number back to me.

The program cimpiles but it doesn't do what i want. here is what i have so far. how can i get the program to display the roman numeral after the number is entered.

import javax.swing.JOptionPane;
public class Romannumeral
{
public static void main(String[] args)
{
double number;
 
[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

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

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

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

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

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







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