8 Replies - 258 Views - Last Post: 31 January 2018 - 05:29 AM Rate Topic: -----

#1 Lyzone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-January 18

Cant find a problem with my code, need help.

Posted 30 January 2018 - 04:58 AM

Hi. So I've started java within the past week and I'm just practicing constructors right now, but I can't figure out what is wrong with this constructor.
package findCpu;

class whatIsCpu {
	int numberOfCores;
	int numberOfThreads;
	String name;
	double freq;
	String cache;
	int uniqueID;
	
	public whatIsCpu(String name, int numberOfCores, int numberOfThreads, double freq, String cache, int uniqueID) {
		this.name = name;
		this.numberOfCores = numberOfCores;
		this.numberOfThreads = numberOfThreads;
		this.freq = freq;
		this.cache = cache;
		this.uniqueID = uniqueID;
		
		System.out.println("Info:\nCores: " + numberOfCores + "\nThreads: " + numberOfThreads + "\nFrequency: " + freq + "\nCache: " + cache);
		if(uniqueID == 91826) {
			System.out.println(" ");
			System.out.println("CPU DETECTED!");
			System.out.println("CPU = 'i5 6600k'!");
			System.out.println(" ");
		}
		if(uniqueID == 471235) {
			System.out.println(" ");
			System.out.println("CPU DETECTED!");
			System.out.println("CPU = 'Xeon E5-2697V4'!");
			System.out.println(" ");
		}
		if(uniqueID != 471235 || uniqueID != 91826) {
			System.out.println("CPU not found. Are you sure you specified correctly?");
			System.out.println(" ");
		}
	}
}

public class FindingCpu {
	public static void main(String[] args) {
		new whatIsCpu("i5 6600k", 4, 4, 3.50, "6 MB SmartCache", 91826);
		new whatIsCpu("Xeon E5-2697V4", 18, 36, 3.60, "45 MB SmartCache", 471235);
		
	}
	
}


When I run it, it outputs the 'CPU DETECTED' bit and also says 'CPU not found' even though I told it to only output 'CPU not found' if the uniqueID is not (!=) 471235 or 91826, the unique IDs of the only cpus I put in there. there's obviously a flaw in my code but I can't find where, and it'd be great if I could know for future reference and ease. I've moved the if(uniqueID != 471235, etc) to the top of the constructor, below the this.<whatever>, I've also put it inside of ones brackets and all that does is say it once instead of twice. Anyone help? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Cant find a problem with my code, need help.

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3542
  • View blog
  • Posts: 16,145
  • Joined: 20-September 08

Re: Cant find a problem with my code, need help.

Posted 30 January 2018 - 05:23 AM

Quote

 if(uniqueID != 471235 || uniqueID != 91826) {

should be
 if(uniqueID != 471235 && uniqueID != 91826) {

You need also to learn the difference between if and else..if (Hint: you need to use the latter)
Was This Post Helpful? 0
  • +
  • -

#3 Lyzone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-January 18

Re: Cant find a problem with my code, need help.

Posted 30 January 2018 - 07:04 AM

Thanks! I’ve seen if else being used but I’ve never really used it myself. Once I get home from school I’ll check it out.

This post has been edited by andrewsw: 30 January 2018 - 10:47 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7164
  • View blog
  • Posts: 14,933
  • Joined: 16-October 07

Re: Cant find a problem with my code, need help.

Posted 30 January 2018 - 07:42 AM

Or, actually, chain your logic:
if(uniqueID == 91826) { // your first test
    // your code
} else if(uniqueID == 471235) { 
    // note that if you got here, the prior test failed
    // your code
} else {
    // if you got here, all prior tests failed
    // which means, implicitly uniqueID != 471235 && uniqueID != 91826
    // your code
}



Keep in mind, as a programmer, you want to avoid unnecessary tests and particularly doing the same test twice. This should guide your reasoning and won't fail you.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 Lyzone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-January 18

Re: Cant find a problem with my code, need help.

Posted 30 January 2018 - 08:51 AM

Thanks, didn’t know I could do that. So what that means is if the uniqueid matches the first one,it says the first ones message, if it doesn’t, it continues to the second one, and if it doesn’t match that one the. It goes to the error message aka the third else.. it’s coming together, this website is teaching me a lot.

This post has been edited by andrewsw: 30 January 2018 - 10:46 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7164
  • View blog
  • Posts: 14,933
  • Joined: 16-October 07

Re: Cant find a problem with my code, need help.

Posted 30 January 2018 - 10:41 AM

Yep, you got it.

This format is common and preferred:
if (conditionA) {
} else if (conditionB) {
} else if (conditionC) {
} else {
}



However, technically, in Java, else-if isn't a special case and could be nested:
if (conditionA) {
} else 
    if (conditionB) {
    } else
        if (conditionC) {
        } else {
        }



The else-if pattern is so common that some languages implement an entirely new statement for it, like elif or elseif, to encourage better blocking.

Also, note, that syntax formatting is one of those things that folks get very attached to. Doubtless you'll find a coder who insists the latter version is preferred or offers some other formatting. It ultimately doesn't matter as long as you're consistent. Java happened to adopt a C standard I prefer, anyway, so I'm happy to go with it. More here: http://www.oracle.co...ons-142311.html
Was This Post Helpful? 0
  • +
  • -

#7 Lyzone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-January 18

Re: Cant find a problem with my code, need help.

Posted 30 January 2018 - 04:33 PM

Hey, so I understood what you said and I'm going to use it from now on, but why didn't 3 if statements work? Or do they just not work?

This post has been edited by ndc85430: 31 January 2018 - 02:45 AM
Reason for edit:: Removed quote of previous post. Just press "Reply" please.

Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3542
  • View blog
  • Posts: 16,145
  • Joined: 20-September 08

Re: Cant find a problem with my code, need help.

Posted 31 January 2018 - 03:48 AM

Quote

but why didn't 3 if statements work?

That wasn't the main problem. I told you what the principal problem was in my first reply.

This post has been edited by g00se: 31 January 2018 - 05:37 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7164
  • View blog
  • Posts: 14,933
  • Joined: 16-October 07

Re: Cant find a problem with my code, need help.

Posted 31 January 2018 - 05:29 AM

View PostLyzone, on 30 January 2018 - 06:33 PM, said:

but why didn't 3 if statements work?


As pointed out by g00se, uniqueID != 471235 && uniqueID != 91826, not uniqueID != 471235 || uniqueID != 91826. Perhaps you don't see that? Step back a moment.

Lets just define some simple test and walk through it:
t1(x) = x!=1 || x!=2
t2(x) = x!=1 && x!=2

t1(1) = false || true = true
t2(1) = false && true = false

t1(2) = true || false = true
t2(2) = true && false = false

t1(3) = true || true = true
t2(3) = true && true = true



So, just to be clear, n!=x || n!=y, where x!=y will always be true.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1