Jump to content

Java programming help!

Watashi

My code continues to read in the "done" value that I punch in, and it returns it. Here's my code

public static String console2 (String PrereqString)
{
Scanner console = new Scanner(System.in);
int number = 1;
String prereqs = "";
while(!prereqs.equals("done")){
   System.out.print("List pre-requisite#" + number + "(enter done, if finished with prerequisites):");
   prereqs = console.nextLine();
   number = number + 1;     
   PrereqString = PrereqString + " " + prereqs;
}

return PrereqString;
}

Link to comment
Share on other sites

Link to post
Share on other sites

So is it just outputting the value that you set for "done"?

There are 10 types of people in this world. Those that understand binary and those that don't.

Current Rig (Dominator II): 8GB Corsair Vengeance LPX DDR4 3133 C15, AMD Ryzen 3 1200 at 4GHz, Coolermaster MasterLiquid Lite 120, ASRock B450M Pro4, AMD R9 280X, 120GB TCSunBow SSD, 3TB Seagate ST3000DM001-9YN166 HSD, Corsair CX750M Grey Label, Windows 10 Pro, 2x CoolerMaster MasterFan Pro 120, Thermaltake Versa H18 Tempered Glass.

 

Previous Rig (Black Magic): 8GB DDR3 1600, AMD FX6300 OC'd to 4.5GHz, Zalman CNPS5X Performa, Asus M5A78L-M PLUS /USB3, GTX 950 SC (former, it blew my PCIe lane so now on mobo graphics which is Radeon HD 3000 Series), 1TB Samsung Spinpoint F3 7200RPM HDD, 3TB Seagate ST3000DM001-9YN166 HDD (secondary), Corsair CX750M, Windows 8.1 Pro, 2x 120mm Red LED fans, Deepcool SMARTER case

 

My secondary rig (The Oldie): 4GB DDR2 800, Intel Core 2 Duo E8400 @ 3GHz, Stock Dell Cooler, Foxconn 0RY007, AMD Radeon HD 5450, 250GB Samsung Spinpoint 7200RPM HDD, Antec HCG 400M 400W Semi Modular PSU, Windows 8.1 Pro, 80mm Cooler Master fan, Dell Inspiron 530 Case modded for better cable management. UPDATE: SPECS UPGRADED DUE TO CASEMOD, 8GB DDR2 800, AMD Phenom X4 9650, Zalman CNPS5X Performa, Biostar GF8200C M2+, AMD Radeon HD 7450 GDDR5 edition, Samsung Spinpoint 250GB 7200RPM HDD, Antec HCG 400M 400W Semi Modular PSU, Windows 8.1 Pro, 80mm Cooler Master fan, Dell Inspiron 530 Case modded for better cable management and support for non Dell boards.

 

Retired/Dead Rigs: The OG (retired) (First ever PC I used at 3 years old back in 2005) Current Specs: 2GB DDR2, Pentium M 770 @ 2.13GHz, 60GB IDE laptop HDD, ZorinOS 12 Ultimate x86. Originally 512mb DDR2, Pentium M 740 @ 1.73GHzm 60GB IDE laptop HDD and single boot XP Pro. The Craptop (dead), 2gb DDR3, Celeron n2840 @ 2.1GHz, 50GB eMMC chip, Windows 10 Pro. Nightrider (dead and cannibalized for Dominator II): Ryzen 3 1200, Gigabyte A320M HD2, 8GB DDR4, XFX Ghost Core Radeon HD 7770, 1TB Samsung Spinpoint F3 (2010), 3TB Seagate Barracuda, Corsair CX750M Green, Deepcool SMARTER, Windows 10 Home.

Link to comment
Share on other sites

Link to post
Share on other sites

could you be more specific about what your desired behavior is and what you are actually getting? Is it that you're seeing "done" in the returned string when you don't want it to be?

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

public static String console2 (String PrereqString)
{
Scanner console = new Scanner(System.in);
int number = 1;
String prereqs = "";
while(!prereqs.equals("done")){
   System.out.print("List pre-requisite#" + number + "(enter done, if finished with prerequisites):");
   prereqs = console.nextLine();
   number = number + 1;     
   PrereqString = PrereqString + " " + prereqs;
}

return PrereqString;
}

You should use code blocks to make the code more visible.

 

I don't understand what this code should be doing, but I see a problem with this line:

PrereqString = PrereqString + " " + prereqs;

Variables need to have a lower case name. preReqs or prereqs. In Java, Class names are capitalized, so this whole line syntactically is incorrect. 

 

I'd suggest the following:

public static String console2(String preReq) {
    Scanner console = new Scanner(System.in);
    
    int n = 1;
    String input = "";

    while (!input.equals("done")) {
        System.out.println("...");
        input = console.nextLine();
        n++;

        preReq = preReq + " " + input;
    }

    return preReq;
}

 

I don't understand the code, but this code works and stops when done is entered:

import java.util.Scanner;

class Testst {

    public static String console2(String preReq) {
        Scanner console = new Scanner(System.in);
        
        int n = 1;
        String input = "";

        while (!input.equals("done")) {
            System.out.println("...");
            input = console.nextLine();
            n++;

            preReq = preReq + " " + input;
        }

        return preReq;
    }

    public static void main(String[] args) {
        console2("test");
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

managed to double post. oopsie.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, DtrollMC said:

Variables need to have a lower case name. preReqs or prereqs. In Java, Class names are capitalized, so this whole line syntactically is incorrect. 

this is good practice according to the standard java style guidelines, but it is not a language requirement. Not to mention certain best naming practices (like constant variables should be entirely capitalized) break the rule as you described.

 

To be clear I'm not disagreeing that variables should almost always be lower case, I'm just trying to point out that its a standards issue not an actual syntactical req 

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, reniat said:

this is good practice according to the standard java style guidelines, but it is not a language requirement. Not to mention certain best naming practices like constant variables should be entirely capitalized break the rule as you described.

 

To be clear I'm not disagreeing that variables should almost always be lower case, I'm just trying to point out that its a standards issue not an actual syntactical req 

I would argue constants are a different case, as you wouldn't have a class with all caps. The best naming practices can go a long way in telling others what something in your code does. Always good to preach the best practices :)

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, DtrollMC said:

Always good to preach the best practices

definitely don't disagree with that. I just want to make it clear that they should do that for legibility reasons, not just because they think their code will have errors if they don't adhere to them.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Coding style are up to the organizations and individuals. My professor actually requires the students to underscore all global variables.

 

Different companies/organizations may demand you adhere to their own coding styles. It is not set in stone. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

id suggest using breakpoints to look why the comparison is failing you/ what else is wrong. using this is gonna help you more in the long run than someone else fixing your code

MSI GX660 + i7 920XM @ 2.8GHz + GTX 970M + Samsung SSD 830 256GB

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, wasab said:

My professor actually requires the students to underscore all global variables.

He shouldn't. You know there's a thing called naming conventions, and teachers should teach according to them. (If you're not talking about constants, which can be named LIKE_THIS)

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, LoidSemus said:

He shouldn't. You know there's a thing called naming conventions, and teachers should teach according to them. (If you're not talking about constants, which can be named LIKE_THIS)

_privateVar

this is what i meant by underscore. He is only professor part time. His full time job is actually senior software engineer and that is the style of his company. There is no single rule for naming. Organization can easily create their own naming rules so its members can easily comprehend one another's code which is the purpose of having these rules in the first place. 

 

you realize the rule but you failed to understand why they are there in the first place. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×