Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package JavaApplication8;

import java.util.Scanner;
public class JavaApplication8 {

    public static void main(String[] args) {


    Scanner scan =new Scanner(System.in);
    System.out.println("Enter an interger between 1000 and 9999:");
            int fourdigitnum=scan.nextInt();

if (fourdigitnum>=1000 &&fourdigitnum<=9999)
{
    System.out.println("Enter an interger between 0 and 9:");
            int onedigitnum=scan.nextInt();
        if(onedigitnum>=0 && onedigitnum<=9)
        {
            //old 4 digits
            int digit1,digit2,digit3,digit4;
            //new 4 digits
          int digit11,digit22,digit33,digit44;

            int temp;
            digit4=fourdigitnum%10;
            temp=fourdigitnum/10;
            digit3=temp%10;
            temp=temp/10;
            digit2=temp%10;
            temp=temp/10;
            digit1=temp%10;


            //finding 1st new digit
            if (0==0){

            if (digit1<onedigitnum)

             digit11=digit1+1;
            else if (digit1>onedigitnum)
                digit11=digit1%onedigitnum;

            //finding 2nd new digit
             if (digit2<onedigitnum)

            digit22=digit2+1;
            else if (digit2>onedigitnum)
                digit22=digit2%onedigitnum;

             //finding 3rd new digit
              if (digit3<onedigitnum)

            digit33=digit3+1;
            else if (digit3>onedigitnum)
                digit33=digit3%onedigitnum;

              //finding 4th new digit
               if (digit4<onedigitnum)

            digit44=digit4+1;
            else if (digit4>onedigitnum)
                digit44=digit4%onedigitnum;
            }

            System.out.println("The output is: "+digit11+""+digit22+""+digit33+""+digit44);
            //MY PROBLEM IS HERE.. im keep getting variable digit11 digit22.. etc.. have not been initialized

        }

        else
            System.out.println("Error, this is not a 1-digit integer");

}
    else
    System.out.println("Error, this is not a 4-digits integer");


}
    
}


What I have tried:

i tried to to declare the variables outside of the if /else statment .. or use the print outside of if/else.. but its not really changing it and i tried to use if (condition) do something else.. instead of (if... else if) but it caused me other problems.. so what to do?


i think my problem is that the system.out.println at the end.. cannot read the value resulting from the if/else statment ..and i dont relly know whats causing this and how to fix it
Posted
Updated 24-Mar-18 11:57am
v2

The compiler is correct. For instance:
Java
int digit11, digit22, digit33, digit44;
 //..


 if ( digit1 < onedigitnum)
   digit11 = digit1 + 1;
 else if ( digit1 > onedigitnum)
   digit11 = digit1 % onedigitnum;

 //..

your code miss the inizialization of digit11 every time (digit1 == onedigitnum) holds, because your if - else if statements don't handle such a scenario.
 
Share this answer
 
Quote:
and i dont relly know whats causing this and how to fix it

Look at your code: what is the value of digit11 when (digit1 == onedigitnum) ?
Java
if (digit1<onedigitnum)
    digit11=digit1+1;
else if (digit1>onedigitnum)
    digit11=digit1%onedigitnum;

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900