Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 674 675 [676] 677 678 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 890452 times)

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10125 on: October 19, 2016, 04:44:23 pm »

Variable names are completely arbitrary and don't, like... matter. You could name it literally any valid name and it would do the exact same thing.

I'm also pretty sure that you don't need a new scanner to scan new types and that whatever method is probably overloaded correctly in any given instance to take whatever. I don't actually know anything about what a scanner is or what it does, though, I'm just going based off of previous OO experience.
« Last Edit: October 19, 2016, 04:45:54 pm by Putnam »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10126 on: October 19, 2016, 04:49:40 pm »

I'm mention this because in previous programs he made a separate scanner for each input statement he wanted.

He named each of the scanners after the thing he was going to input. He's still using that naming convention, in fact when BiggerFish mentioned the duplicate-scanners issue in his latest program, he doubled-down on that naming convention from before (naming the scanner after the specific thing you're asking for). So I was skeptical as to whether he actually gets it, and want him to show a demo of a program taking two completely different inputs to be sure he gets it.

We keep having the same discussion about the same niggles with 3ma75. This is round three for the scanners-naming thing, not to mention all the other incidents where he isn't paying attention to people's detailed posts. I'm running out of patience for the pattern of behavior, which is why I am drawing a line on the naming thing.

No, I'm not going to give a fuck about resizing your input boxes when you haven't actually engaged properly with what people have said. 3man75, you're still acting like you have no clue what I'm talking about. I was very clear.
« Last Edit: October 19, 2016, 04:54:44 pm by Reelya »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10127 on: October 19, 2016, 05:07:40 pm »

Scanners are there to take input from your keyboard. Kinda like Cin >> for C++

Names are indeed arbitrary but the point of contestion I think right now is why did I name it 'playerName' instead of something like 'input' or whatever to make it less likely to confuse me or a maintenance programmer down the road.

Basically instead of: Scanner playerName = new Scanner(System.in);

I should have made: Scanner inputDevice = new Scanner(system.in); or Scanner genericInput *rest of code*.


I'm mention this because in previous programs he made a separate scanner for each input statement he wanted.

He named each of the scanners after the thing he was going to input. He's still using that naming convention, so I was skeptical as to whether he actually gets it.

I hate my reputation too. I hate my inability to recall important tidbits after a week of not working on something over a weekend. Whether I'm getting better I can't say but to give you some insight about the job I posted..I had to leave.

Not because they didn't like me but because I was a student assistant and I had to also go to school for 6 credits per semester. FAFSA screwed me and money is tight so I wasn't able to sign up. Which forced me to drop that job.

Beleive it or not I was actually learning alot but I was still wet behind the ears. Printing issues? I got that in the bag and a metal box. stage set up? Check. Spanish speaking proffesors having issues with word? I know how to explain it for dum dums like myself.

New concepts? Not so much. But if I got better in that tech job I can do better in programming with time. Also the name of my department was network and media. Meaning I had to do media which was setting up for not so tech stuff and become a stagehand/roadie for whatever the event was. No more on this due to privacy and what not.

I actually felt like I left on a good note though so that's good. Not that it matters because yeah I'm a piece of shit of a worker and i'm 22 with alot of pre-reqs that I still have to take to transfer to a uni for my bachelors in C.science.

Now that that's out of the way reelya..

I still do like naming conventions for naming things on what i'm going to use them for. Yes, it's inefficient and I know that but I don't know how to name it without it being so damn generic that I don't know WHAT to use it for. Maybe i'm overthinking things and I should just generically named things like 'inputDevice' and just trace the code for hours in the future when my programs become bigger and more complex.

Kay, so how do I make that code box bigger for people to read? An again, I AM NOT GOING TO WORK ON THIS PROJECT ANY LONGER! I just want some advice on how I should build bigger things in the future. I don't have a system for organizing projects with classes or larger things. I don't know how to make things scalelable (the ability to continuously build on code without having to double back and rewrite your original code).




Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10128 on: October 19, 2016, 05:15:57 pm »

It's not a naming problem, it's the redundant second definition. This:

Code: [Select]
Scanner inputSystem = new Scanner(System.in);
      Scanner playerName = inputSystem;

might as well be this:

Code: [Select]
Scanner playerName = new Scanner(System.in);
Also, it'd be better named something like, say, playerNameScanner or playerNameInputSystem, people won't give you crap for long variable names.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10129 on: October 19, 2016, 05:21:11 pm »

Naming can be an issue for maintainability, but I mainly wanted to see evidence that you've acknowledged the core issue people brought up. I saw the renaming to "playerName" as not entirely conducive to believing you "got it".

As for remembering how to do things. Make program "templates" e.g. small programs/classes/functions that do that one thing as elegantly and generally as possible, in the least amount of code. Adhere to really clear naming conventions. Refer to those templates when you make new programs, classes or functions.

You're not expected to memorize boilerplate code. That's not how people operate. We have computers for that. The skill is to be able to see which chunk of boilerplate solution to apply where, then rename the "generic" names to specific names as needed (keep the generic names if it makes sense). Basically if you're retyping things from memory and making mistakes, just don't retype it, cut and paste from a "perfect" version which you keep as a reference.
« Last Edit: October 19, 2016, 05:22:47 pm by Reelya »
Logged

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10130 on: October 19, 2016, 05:27:04 pm »

What 3man75 needs is a minimal example. I recently started learning Java and I used to have the same issue. It is possible to create one Scanner object and use that throughout the entire class to parse both strings and integers. Here is how I do that:

Code: [Select]
import java.util.Scanner;

public class InputManager
{
private Scanner input;

public InputManager()
{
// Create a single scanner for this class (private Scanner input;) and initialize it.
// You can create a new Scanner for each method, but it is easier to reuse this one.
input = new Scanner(System.in);
}

public void run()
{
System.out.println("Type a number to add one. Type a string to make it uppercase. Type 'exit' to quit.");

// Keep looping the program over and over until the user inputs 'exit'
while(true)
{
// Get whatever the user entered as a String. It *might* be a number
String command = input.nextLine();

// Exit the program for this special case
if(command.equals("exit"))
System.exit(1);

// This is a try-catch block. It is how Java does error handling.
// If something inside the try{} fails to work, it will send out an exception.
// If an exception is sent out, the program will skip to the catch{} part.
// If nothing fails and everything goes OK, the catch{} block will be ignored.
try
{
// Our command is a string. Try to turn it into an integer.
// If this part fails, the program will immediately go into the catch{} block.
int number = Integer.parseInt(command);

// If the user input something like '123' then the previous line worked.
// Since we turned the command into an integer, we can now add to it and print it.
System.out.println("Your input plus one: " + (number + 1));
}
catch(NumberFormatException e)
{
// If the program fails to turn the command into an integer, it will go here.
System.out.println("Your input converted to uppercase: " + command.toUpperCase());
}
}
}
}

If you want to test this code, you can run it in main like this:

Code: [Select]
public class InputTester
{
public static void main(String[] args)
{
InputManager test = new InputManager();
test.run();
}
}

It's been a long time since I wrote a batch file, but I think you can just make a text file, put the commands you want on different lines, then name the file ending in .bat to make it a batch file.

If you want to pass different files, write a Python script that takes two filenames as command line arguments, then creates a command string using those filenames. From there you can use os.system(string command) to run it in the Windows shell.

Hey, that sounds like a great idea! I looked into it and apparently Python has a subprocess module that lets you do just that. I just tested it out and successfully ran the command line program from a script. This is much better than the alternative of learning something new like Powershell. ^^;
« Last Edit: October 19, 2016, 05:40:03 pm by DragonDePlatino »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10131 on: October 19, 2016, 05:42:58 pm »

Naming can be an issue for maintainability, but I mainly wanted to see evidence that you've acknowledged the core issue people brought up. I saw the renaming to "playerName" as not entirely conducive to believing you "got it".

As for remembering how to do things. Make program "templates" e.g. small programs/classes/functions that do that one thing as elegantly and generally as possible, in the least amount of code. Adhere to really clear naming conventions. Refer to those templates when you make new programs, classes or functions.

You're not expected to memorize boilerplate code. That's not how people operate. We have computers for that. The skill is to be able to see which chunk of boilerplate solution to apply where, then rename the "generic" names to specific names as needed (keep the generic names if it makes sense).

First issue. One too many scanners. Fixed.

playerName was never changed either because I forgot to. Want me to rename it? fine Scanner playerInpurDevice = new Scanner (System.in);

Done. I wish it was that simply said. Or maybe I lost the meaning in your paragraph again. I believe in bullet points more than anything when reading but hey this is free insightful help right?

So now that's over with I say this: Thanks.

This will be the last time you see me and i'm done. I'll go somewhere else or maybe a MOOC to learn.

What 3man75 needs is a minimal example. I recently started learning Java and I used to have the same issue. It is possible to create one Scanner object and use that throughout the entire class to parse both strings and integers. Here is how I do that:

Code: [Select]
import java.util.Scanner;

public class InputManager
{
private Scanner input;

public InputManager()
{
// Create a single scanner for this class (private Scanner input;) and initialize it.
// You can create a new Scanner for each method, but it is easier to reuse this one.
input = new Scanner(System.in);
}

public void run()
{
System.out.println("Type a number to add one. Type a string to make it uppercase. Type 'exit' to quit.");

// Keep looping the program over and over until the user inputs 'exit'
while(true)
{
// Get whatever the user entered as a String. It *might* be a number
String command = input.nextLine();

// Exit the program for this special case
if(command.equals("exit"))
System.exit(1);

// This is a try-catch block. It is how Java does error handling.
// If something inside the try{} fails to work, it will send out an exception.
// If an exception is sent out, the program will skip to the catch{} part.
// If nothing fails and everything goes OK, the catch{} block will be ignored.
try
{
// Our command is a string. Try to turn it into an integer.
// If this part fails, the program will immediately go into the catch{} block.
int number = Integer.parseInt(command);

// If the user input something like '123' then the previous line worked.
// Since we turned the command into an integer, we can now add to it and print it.
System.out.println("Your input plus one: " + (number + 1));
}
catch(NumberFormatException e)
{
// If the program fails to turn the command into an integer, it will go here.
System.out.println("Your input converted to uppercase: " + command.toUpperCase());
}
}
}
}

If you want to test this code, you can run it in main like this:

Code: [Select]
public class InputTester
{
public static void main(String[] args)
{
InputManager test = new InputManager();
test.run();
}
}

~snip~

Ninja'd: Just why did you make a private scanner int all the way in the bottom? I've never seen that before.
Logged

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10132 on: October 19, 2016, 05:57:04 pm »

Ninja'd: Just why did you make a private scanner int all the way in the bottom? I've never seen that before.

The "private Scanner input;" is an instance variable. When you create an InputManager object, it automatically opens that Scanner in the InputManager() constructor. Then, the InputManager object can reuse that single Scanner in all of its methods. The alternative is to create a new Scanner every time you need one, which is wasteful and verbose.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10133 on: October 19, 2016, 06:13:21 pm »

Just a note about scanners in general since people are wondering about it: Scanners aren't there to get terminal input in particular, they are simply a generic wrapper you can wrap around any old input stream to then read text data from it using convenient methods such as nextLine() or nextFloat(). The actual terminal input stream is the standard input stream System.in, and it's only connected to the terminal because that's what it connects to if you run it from one without specifying any other standard input.
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10134 on: October 19, 2016, 06:23:11 pm »

Variable names are completely arbitrary and don't, like... matter. You could name it literally any valid name and it would do the exact same thing.

I'm also pretty sure that you don't need a new scanner to scan new types and that whatever method is probably overloaded correctly in any given instance to take whatever. I don't actually know anything about what a scanner is or what it does, though, I'm just going based off of previous OO experience.
Java's Scanner has a set of methods around Java's primitive types and two for 'read till next space' and 'read till next line break'.

Just a note about scanners in general since people are wondering about it: Scanners aren't there to get terminal input in particular, they are simply a generic wrapper you can wrap around any old input stream to then read text data from it using convenient methods such as nextLine() or nextFloat(). The actual terminal input stream is the standard input stream System.in, and it's only connected to the terminal because that's what it connects to if you run it from one without specifying any other standard input.
Yeah.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Emma

  • Bay Watcher
  • Romace plots aren't actually that bad
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10135 on: October 20, 2016, 11:19:42 pm »

Does anyone know why:
Code: [Select]
f = open('filename', 'w')
f.write('Text')
f.close()
Doesn't work when filename is an html file? Do I have to specify it as 'filename.html' or something? This is in Python, bythe way.
Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10136 on: October 20, 2016, 11:49:48 pm »

Possibly. Also, I think you need to type the full path to the file, unless that file is in the same directory as the script.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10137 on: October 21, 2016, 12:17:14 am »

you always need the file extension there, no exceptions.
Logged

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10138 on: October 21, 2016, 01:02:09 am »

Hi! Can anyone point me in the right direction for solving this problem? I'm running Windows and I want to execute a program on a bunch of files. I can issue a command to the program like so:

Code: [Select]
ImageResizer /load "image.png" /resize auto "XBR 2x(hbounds=const, vbounds=const, thresholds=1)" /save "image_2x.png"
Basically, I want to run this program on all the images in a folder (and subfolders), loading in [IMAGENAME].png and saving as [IMAGENAME]_2x.png. The resize command is always the same. How do I do this programmatically? I've heard of things called shell scripts and batch files but I don't know where to start. I'm also somewhat competent at C, C++, Java and Python if there are easier solutions in those languages.

After some googling and testing:
Code: (resizer.bat) [Select]
FOR /R %%I IN (*.png) DO ImageResizer /load "%%I" /resize auto "XBR 2x(hbounds=const, vbounds=const, thresholds=1)" /save "%%~dpnI_2x.png"FOR = This is a function that iterates over all files in the current directory
/R = Recursive (include sub-folders)
%%I = Variable to refer to each result. Represents the full drive+path+name+extension of the file.
(*.png) = The filter
%%~dpnI = This is just the drive+path+name of the file. Extension (.png) is omitted.

If you want to paste the command directly into the terminal (instead of using a .bat file,) then only use a single % in place of %%. I tested only with the copy command, but it should probably work for ImageResizer.
« Last Edit: October 21, 2016, 03:40:37 pm by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Emma

  • Bay Watcher
  • Romace plots aren't actually that bad
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10139 on: October 21, 2016, 03:03:23 am »

you always need the file extension there, no exceptions.
Yep, figured that out just after I posted. I don't know how I fucked up that badly but I did.
Logged
Pages: 1 ... 674 675 [676] 677 678 ... 796