Debugging Java Code for User Input Decryption

  • Context: Java 
  • Thread starter Thread starter crimsondarkn
  • Start date Start date
  • Tags Tags
    Debugging Java
Click For Summary

Discussion Overview

The discussion revolves around debugging a Java program intended to decrypt user input by shifting each letter to the next one in the alphabet. Participants explore issues related to case sensitivity, loop control, and logical flow within the code.

Discussion Character

  • Technical explanation
  • Debugging
  • Exploratory

Main Points Raised

  • One participant shares their Java code but notes that it produces no output when run, seeking assistance.
  • Another participant points out that the case sensitivity of characters ('A' vs. 'a') could be causing issues, suggesting the use of `toUpperCase()` to standardize input.
  • A participant provides an example input ("MOO") and describes the expected output ("NPP"), illustrating the intended functionality of the program.
  • Another participant identifies a potential error in the loop control, suggesting that the increment should be `j++` instead of `i++` to allow the inner loop to function correctly.
  • The original poster acknowledges fixing the loop issue but discovers that an `else` statement was causing the program to exit the loop prematurely, preventing any output.

Areas of Agreement / Disagreement

Participants express various viewpoints on the issues present in the code, with no consensus reached on a single solution. Multiple competing views on the cause of the problem and potential fixes are presented.

Contextual Notes

Limitations include the dependence on character case for matching, potential logical errors in loop control, and the unresolved impact of the `else` statement on program execution.

crimsondarkn
Messages
13
Reaction score
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
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();?
 
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
 
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
 
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
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K