Click here to Skip to main content
15,498,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The code for the first class :

Java
import java.util.Scanner;           //importing scanner for input and output
public class CPUcopy {                  
    Scanner sc = new Scanner(System.in);
    String a,b;       
    String[] f = new String[10];
    String[] g = new String[10];
    int i;                   //declaring static variable i
    
    public void hw() {              //creating public constructor hw for selection of CPU by user

        for(i=0;i<3;i++); {
            System.out.println("Available CPU brands are :\n1.Intel \n2.AMD");
            System.out.print("Enter the CPU brand - ");
            a = sc.next();
            switch(a) {                 //switch case for selecting CPU brand
                case "1":
                case "INTEL":
                case "intel":
                case "Intel": {
                    f[i] = "Intel";
                    System.out.println("Available CPU models are :");
                    System.out.println("1. i3 \n2. i5\n3. i7\n4. i9");
                    System.out.print("Select CPU model - ");
                    b = sc.next();
                    switch(b) {
                        case "1":
                        case "i3": g[i] = "i3";       
                        break;
                    
                        case "2":
                        case "i5": g[i] = "i5";
                        break;
                    
                        case "3":
                        case "i7": g[i] = "i7";
                        break;
                    
                        case "4":
                        case "i9": g[i] = "i9";
                        break;
                    }
                }
                break;

                case "2":
                case "AMD":
                case "Amd":
                case "amd": {
                    f[i] = "AMD";
                    System.out.println("Available CPU models are :");
                    System.out.println("1. Ryzen 3 \n2. Ryzen 5\n3. Ryzen 7\n4. Ryzen 9");
                    System.out.print("Select CPU model - ");
                    b = sc.nextLine();
                    b = sc.nextLine();
                    switch(b) {
                        case "1":
                        case "ryzen 3":
                        case "Ryzen 3": g[i] = "Ryzen 3";
                        break;
                    
                        case "2":
                        case "ryzen 5":
                        case "Ryzen 5": g[i] = "Ryzen 5";   
                        break;
                    
                        case "3":
                        case "ryzen 7":
                        case "Ryzen 7": g[i] = "Ryzen 7";
                        break;
                    
                        case "4":
                        case "ryzen 9":
                        case "Ryzen 9": g[i] = "Ryzen 9";
                        break;
                    }
                }
                break;
            }
        } 
        
        System.out.println("Output");
        for(i=0;i<3;i++){
            System.out.println(f[i]+" "+g[i]);
        }
    }
}


The code to run above mentioned program:

Java
public class main2 {
    public static void main(String[] args){
        CPUcopy c = new CPUcopy();
        c.hw();            
    }
}


I keep getting null output whenever I run the program, although there are no errors.

What I have tried:

I have tried creating a similar program from scratch, and it works there.

Java
import java.util.Scanner;
public class arraytest {
    Scanner sc = new Scanner(System.in);
    static int i,a;
    int[] arr = new int[10];
    String[] f = new String[10];
    String[] g = new String[10];
    public void test() {
        
        for(i=0;i<3;i++) {
            System.out.println("enter CPU......");
            System.out.println("1. Intel    2. AMD");
            a = sc.nextInt();
            switch(a) {
                case 1: {
                    f[i] = "Intel";
                    System.out.println("enter CPU model.......");
                    System.out.println("1. i3    2. i5  3. i7   4. i9");
                    arr[i] = sc.nextInt();
                    switch(arr[i]) {
                        case 1: g[i] = "i3";
                        break;
                        case 2: g[i] = "i5";
                        break;
                        case 3: g[i] = "i7";
                        break;
                        case 4: g[i] = "i9";
                        break;
                    }
                }
                break;
                case 2: {
                    f[i] = "AMD";
                    System.out.println("enter CPU model.......");
                    System.out.println("1. R3    2. R5  3. R7   4. R9");
                    arr[i] = sc.nextInt();
                    switch(arr[i]) {
                        case 1: g[i] = "Ryzen 3";
                        break;
                        case 2: g[i] = "Ryzen 5";
                        break;
                        case 3: g[i] = "Ryzen 7";
                        break;
                        case 4: g[i] = "Ryzen 9";
                        break;
                    }
                }break;
            }
        }
        System.out.println("output:");
        for(i=0;i<3;i++) {
            System.out.println(f[i]+" "+g[i]);
        }
    }
}


The Main class for this program is similar to the problematic one.
Posted
Updated 10-Sep-21 2:34am
Comments
Richard MacCutchan 10-Sep-21 4:05am    
Why not use the debugger to see what values are being processed in your switch statements? Or, just print out every token that you are trying to process.

Quote:
I keep getting null output whenever I run the program, although there are no errors.

There is different kind of errors and they unravel at different time.
Syntax errors unravel at compile time.
Typo errors give crashes at run time.
Logical errors give unexpected results at run time.

Not getting expected result is a sign of error. There is a tool to help you fixing that error, it is a debugger.

Your code do not behave the way you expect, or 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 code 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[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
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
 
There are a few issues with your code, the first one being that the for statement at line 11, which has a semi-colon at the end, meaning that the loop does nothing. You also have a close parenthesis after the case "1" block, which means that case "2" is orphaned. I have also modified both switch statements to convert the input to lower case to avoid multiple cases for the same word:
Java
import java.util.Scanner;           //importing scanner for input and output

public class CPUcopy {                  
    Scanner sc = new Scanner(System.in);
    String a,b;       
    String[] f = new String[10];
    String[] g = new String[10];
    int i;                   //declaring static variable i
    
    public void hw() {              //creating public constructor hw for selection of CPU by user

        for(i = 0; i < 3; i++) {
            System.out.println("Available CPU brands are :\n1.Intel \n2.AMD");
            System.out.print("Enter the CPU brand - ");
            a = sc.next();
            switch(a.toLowerCase()) {                 //switch case for selecting CPU brand
                case "1":
                case "intel":
                    f[i] = "Intel";
                    System.out.printf("f[%d] : %s%n", i, f[i]);
                    System.out.println("Available CPU models are :");
                    System.out.println("1. i3 \n2. i5\n3. i7\n4. i9");
                    System.out.print("Select CPU model - ");
                    b = sc.next();
                    switch(b) {
                        case "1":
                        case "i3": g[i] = "i3";       
                        break;
                    
                        case "2":
                        case "i5": g[i] = "i5";
                        break;
                    
                        case "3":
                        case "i7": g[i] = "i7";
                        break;
                    
                        case "4":
                        case "i9": g[i] = "i9";
                        break;
                    }
                break;

                case "2":
                case "amd": {
                    f[i] = "AMD";
                    System.out.println("Available CPU models are :");
                    System.out.println("1. Ryzen 3 \n2. Ryzen 5\n3. Ryzen 7\n4. Ryzen 9");
                    System.out.print("Select CPU model - ");
                    b = sc.nextLine();
                    b = sc.nextLine();
                    switch(b.toLowerCase()) {
                        case "1":
                        case "ryzen 3": g[i] = "Ryzen 3";
                        break;
                    
                        case "2":
                        case "ryzen 5": g[i] = "Ryzen 5";   
                        break;
                    
                        case "3":
                        case "ryzen 7": g[i] = "Ryzen 7";
                        break;
                    
                        case "4":
                        case "ryzen 9": g[i] = "Ryzen 9";
                        break;
                    }
                }
                break;
            }
        } 
        
        System.out.println("Output");
        for(i=0;i<3;i++){
            System.out.println(f[i]+" "+g[i]);
        }
    }
    public static void main(String[] args){
        CPUcopy c = new CPUcopy();
        c.hw();            
    }
}
 
Share this answer
 
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
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