I want to write a program that reads an integer number and shows the user the Max digits of that number. What is the problem with my code ?
I have tried some codes here but eclipse doesn't allow !
what is the correct form ? How should I change it to correct form ?
import java.util.Scanner;
public class TestMaxDigits {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long n;
long max = 0;
if(n%10>max){
max = n%10 ;
System.out.println("max ="+max) ;
}
}
}
You should use hasNextInt() and nextInt() (or hasNextLong() and nextLong() ) method from Scanner class to read integer number form user input.
Try with this code:
public static void main( String[] args )
{
Scanner input = new Scanner( System.in );
while( input.hasNextInt() )
{
int n = input.nextInt();
long max = 0;
if( n % 10 > max )
{
max = n % 10;
System.out.println( "max =" + max );
}
}
}
You should describe more what exactly you want to do.
It's not working because logically your code is buggy..
public class TestMaxDigits {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long n;// you never read any input form the keyboard
long max = 0;
if(n%10>max){//this check once for the condition and if found true
//then assign the value to max in next step.
max = n%10 ;
System.out.println("max ="+max) ;
//but since number may have more than one digit which you never checked in your logic
}
}
}
Here is one Way to do it
public class Test {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Input number");
int input=keyboard.nextInt();
keyboard.close();
int max=0;
if(input>=0){
while(input!=0){
if(max<input%10)
max=input%10;
input=input/10;
}
}else{
System.out.println("Invalid Input");
}
System.out.println(max);
}
}
Related
My concern is only with why the modulo (%) isn't working. Please don't comment on the code on it's entirety. My code is as displayed below.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Enter a number");
int num = scanner.nextInt();
int count = 0;
while(num!=0) {
num = num/10;
count++;
}
System.out.println("Total digits = " + count );
int rem = num % 10;
System.out.println(rem);
}}
The output is
Also quick note: the output for "rem" is 0 only when "num" is passed as an operand. If a number replaces the "num" then the code works just fine
There is a wrong logic used for reversing the number. You can try below code.
public class ReverseNumberExample1
{
public static void main(String[] args)
{
int number = 987654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println("The reverse of the given number is: " + reverse);
}
}
2 weeks in. I need to break the while loop with a negative integer but I've been unable to implement it successfully. This is my original code before I editted left and right to no avail. The values in Print are outside the loop.
public class AssignmentQuestion2 {
public static void main(String[] args) {
//write your code here
ArrayList<Integer> rainfall = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
System.out.println("Please input rainfall(Input negative value to exit)");
while (in.hasNextInt()) {
if (in.hasNextInt()) {
rainfall.add(in.nextInt()); //how to input into array
System.out.println(calculateAverage(rainfall)); //average
System.out.println(findSmallest(rainfall)); //smallest input
System.out.println(findLargest(rainfall)); //largest input
} else {
in.close();
}
}
}
You can break a loop using the (aptly named) break statement:
int r;
while ((rainfall != null)) {
if (in.hasNextInt()) {
r = in.nextInt();
if (r < 0) {
break;
}
rainfall.add(r); //how to input into array
System.out.println(calculateAverage(rainfall)); //average
System.out.println(findSmallest(rainfall)); //smallest input
System.out.println(findLargest(rainfall)); //largest input
}
}
For java practice, i am trying to create a program that reads integers from the keyboard until a negative one is entered.
and it prints the maximum and minimum of the integer ignoring the negative.
Is there a way to have continuous input in the same program once it runs? I have to keep running the program each time to enter a number.
Any help would be appreciated
public class CS {
public static void main(String []args) {
Scanner keys = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = keys.nextInt();
while(true)
{
if(n>0)
{
System.out.println("Enter again: ");
n = keys.nextInt();
}
else
{
System.out.println("Number is negative! System Shutdown!");
System.exit(1);
}
}
}
}
Here is a part of my code - It works, but i think there is an easier way of doing what i want but not sure how!
import java.util.Scanner;
public class ABC {
public static void main(String []args) {
int num;
Scanner scanner = new Scanner(System.in);
System.out.println("Feed me with numbers!");
while((num = scanner.nextInt()) > 0) {
System.out.println("Keep Going!");
}
{
System.out.println("Number is negative! System Shutdown!");
System.exit(1);
}
}
}
You could do something like:
Scanner input = new Scanner(System.in);
int num;
while((num = input.nextInt()) >= 0) {
//do something
}
This will make num equal to the next integer, and check if it is greater than 0. If it's negative, it will fall out of the loop.
A simple loop can solve your problem.
Scanner s = new Scanner(System.in);
int num = 1;
while(num>0)
{
num = s.nextInt();
//Do whatever you want with the number
}
The above loop will run until a negative number is met.
I hope this helps you
import java.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
I am in a CS1 class learning the basics of Java and have a quick question, on this code could anyone tell me how i could get it to keep count of how many integers were typed in?
Thank you
above your while loop, declare:
int count = 0;
then in your while loop use
count++;
This will start you at 0 and every time it increments the count
You could add a counter to the while loop.
int counter = 0;
while (chopper.hasNextInt()) {
counter++;
System.out.println(chopper.nextInt());
}
System.out.println(counter);
In the cases that you have integer numbers, double numbers and you only need count the integer numbers, you can use:
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
int counter = 0;
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
String myCurrentArg = chopper.nextInt();
if(isInteger(myCurrentArg) ){
counter++;
}
}
System.out.println("The number of integer arguments are: " + counter);
}
public static boolean isInteger(String s) {
return isInteger(s,10);
}
}
I'm having some troubles with the beginning of this program.
It's supposed to take a number and determine whether or not it is perfect.
Currently I have it asking how many numbers to check, whenever I input any positive number, it asks again.
AND, whenever I input a negative number for the second question, the program ends and doesn't prompt again.
Any ideas on how to fix this?
import java.util.Scanner;
public class aermel_Perfect
{
public static void main ( String args [] )
{
int gN = getNum();
int gP = getPerfect();
}
public static int getNum() //Get amount of numbers to check
{
Scanner input = new Scanner ( System.in );
System.out.print( "How many numbers would you like to test? " );
int count = input.nextInt();
int perfect = 1;
boolean vN = validateNum(count, perfect);
return count;
}
public static boolean validateNum( int count, int perfect ) //Check if number is valid
{
if (( count <= 0) || ( perfect <= 0))
{
System.out.print( "Non-positive numbers are not allowed.\n");
}
else
{
return true;
}
return false;
}
public static int getPerfect() //Gets the numbers to test
{
Scanner input = new Scanner ( System.in );
int perfect = -1;
int count = getNum();
System.out.print("Please enter a perfect number: " );
perfect = input.nextInt();
boolean vN = validateNum(perfect, count);
return perfect;
}
}
Use a while loop in your get methods e.g. to repeatedly ask for input until a valid number is entered.
public static int getNum() //Get amount of numbers to check
{
Scanner input = new Scanner ( System.in );
System.out.print( "How many numbers would you like to test? " );
int count = input.nextInt();
int perfect = 1;
boolean vN = validateNum(count, perfect);
while(!vN ){
System.out.println("Invalid input. Try again");
count = input.nextInt();
vN = validateNum(count, perfect);
}
return count;
}
Simiilarly, update the getPerfect method and remove int count = getNum(); statement from this method.
EDIT: To repeatedly ask for perfect number count times, update your main method as below:
public static void main ( String args [] )
{
int gN = getNum();
for(int indx=0; indx <gN; indx++){
int gP = getPerfect();
//use your gP numbers in the way you want
}
}
EDIT1: To call How many numbers would you like to test? " tow times, I think you can simply call your getNum() method two times in the main method as below:
public static void main ( String args [] )
{
int gN = getNum();//first call
gN = getNum(); //second call
int gP = getPerfect();
}
int count = getNum();
System.out.print("Please enter a perfect number: " );
perfect = input.nextInt();
You get two numbers here.