Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Exercise i have been stuck on for several days now

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Exercise i have been stuck on for several days now

    Hello java community,
    i am new to the forum and i got an exercise like a week ago.
    the exercise is:
    Write a program that recieves an positive integer from the user and print the biggest diggit for that integer.

    the problem i face with is that i have to use "For" loop in order to solve it which get me stucked.

    the program i wrote is:


    import java.util.Scanner;
    public class name{

    public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    System.out.print("Enter positive integer:");
    int num=kb.nextInt();

    for (int maxDigit = 0; num % 10 >= maxDigit ; maxDigit = num )
    {
    System.out.println("The biggest digit in "+num+ " is " +maxDigit);

    }

    kb.close();

    }
    }

    that's after alot of changes and i don't know what to do from now.
    Thanks!

  2. #2
    Junior Member
    Join Date
    Dec 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise i have been stuck on for several days now

    I believe the issue is your for loop condition.

    Example: user enters 7
    First iteration, 7 % 10 >= 0, maxDigit = 7
    Second iteration, 7 % 10 >= 7, maxDigit = 7 (you enter an infinite loop)

  3. #3
    Junior Member
    Join Date
    Jan 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise i have been stuck on for several days now

    String n = String.valueOf(new Random().nextInt(262144));
    log.info("The number is: {}", n);
    int max = Integer.valueOf(String.valueOf(n.charAt(0)));
    for(int i=0; i<n.length(); i++){
    int c = Integer.valueOf(String.valueOf(n.charAt(i)));
    if(c > max){
    max = c;
    }
    }
    log.info("Max: {}", max);

  4. #4
    Junior Member
    Join Date
    Jan 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise i have been stuck on for several days now

    public class FindBiggestDigit {
        public static void main(String[] args) {
            boolean check=true;
            while ( check ){
     
            Scanner entry=new Scanner(System.in);
            System.out.println();
            System.out.println("Enter negative integer to exit");
            System.out.print("Enter a positive integer :");
            int enteredNumber = entry.nextInt();
     
            if ( enteredNumber < 0 )
                break;
     
            int showNumber = enteredNumber;
     
            int biggestDigit=0;
     
            int newDigit = 0 ;
     
            while ( enteredNumber != 0){
                newDigit = enteredNumber % 10;
                if(newDigit > biggestDigit){
                    biggestDigit=newDigit;
                }
                enteredNumber/=10;
            }
     
     
            System.out.println("The biggest digit of "+showNumber+" is "+biggestDigit);
            }
        }
    }
    Last edited by undencem; January 9th, 2020 at 06:35 PM.

Similar Threads

  1. Days Remaining Total Way Off, Displays Days Since Instead. Please Help??
    By kjc21793 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 2nd, 2014, 07:02 PM
  2. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  3. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  4. Stuck, and i need this to be solved within 2 days.
    By nicor4 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 31st, 2013, 09:47 AM
  5. how do i get only the workig days for a certain month
    By anonimus83 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 11th, 2010, 11:13 AM