Debugging Java Code for User Input Decryption

In summary, the program is designed to take a word input from the user and output the letters of that word with the next letter in the alphabet. However, there were some errors in the code, such as not considering uppercase and lowercase letters as the same and an incorrect loop increment. After fixing these issues, the program should work properly.
  • #1
crimsondarkn
13
0
Here's what I wrote , but there's no output.. It is suppose to read a line typed from the user and enter whatever the input was with the letter after the ones inputted. please help

import java.util.Scanner;

class ProblemS2 {

static Scanner sc=new Scanner(System.in);

public static void main(String[] args){

System.out.print("Input the word:");

String code =sc.next();



System.out.println("Time to decipher this...");

char c;


char[] letteroutput = { 'A', 'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };

for(int i=0;i<code.length();i++) {
c = code.charAt(i);



for(int j=0; j <letteroutput.length; i++) {

if(letteroutput[j]==c)
System.out.println(letteroutput[j+1]);
else
break;


}





}

}}
 
Technology news on Phys.org
  • #2
I don't know what is it that you wan't to do but for starters 'A' and 'a' are not the same thefore if you input "java" nothing will happen since 'j' does not match 'J', maybe you can try String code =sc.next().toUpperCase();?
 
  • #3
Let's say I input MOO

the output would be NPP

N is after M
P is after O

I tried with Caps, does the same thing
 
  • #4
for(int j=0; j <letteroutput.length; i++ ) {

Can't be bothered to run through your algo, but this jumped out at me as I was skimming this thread. Should be j++ if you want j to ever change. :P
 
  • #5
Oh I just fixed that but then I found out the real problem... The Else statement I had cause the program to output nothing since it always broke out of my loop. Thx for help
 

1) What is Java debugging and why is it important?

Java debugging is the process of identifying and fixing errors or bugs in a Java program. It is important because it helps ensure that the program runs smoothly and produces the desired results.

2) How do I start debugging a Java program?

The first step is to identify the problem by looking at error messages or using debugging tools. Then, you can set breakpoints in the code and step through it to see where the error occurs. It can also be helpful to print out values or use logging to track the flow of the program.

3) What is a breakpoint and how do I use it?

A breakpoint is a point in the code where the program will pause so that you can inspect the values of variables and track the flow of the program. To set a breakpoint, you can click on the line of code where you want it to be, or use the debugging tools to add it.

4) How do I fix a Java debugging problem?

There are a few steps you can take to fix a debugging problem. First, make sure you understand the problem and have identified the root cause. Then, use debugging tools to step through the code and see where the error occurs. Finally, make necessary changes to the code to fix the problem.

5) Are there any common mistakes or issues to watch out for when debugging Java programs?

Some common mistakes or issues that can make debugging more difficult include incorrect use of variables or methods, logic errors, and not using debugging tools effectively. It's important to pay attention to details and thoroughly test the code to avoid these issues.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
771
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
19
Views
2K
Back
Top