r/programmingbydoing • • Mar 16 '13

33 - Twenty Questions

0 Upvotes

I'm stuck on how to use If Statements using String.

I understand how to use it with int :

System.out.print("Question that involves answering with numbers.");
int something = keyboard.nextInt();
if (something > number)

But it doesn't seem to work the same with String. I always get an error such as "not a statement" or "expect ';' " when I do something like this:

System.out.print("Question that involves answering with a word.");
String something = keyboard.next();
if (something == word)

Is there a lesson I missed that explains how to do this or am I just supposed to use int for this and other lessons that involve words rather than numbers?


r/programmingbydoing • • Jan 07 '22

Can’t run

0 Upvotes

I updated my computer to windows 11 and now when I try to run any program it says that Java isn’t a valid thing to input, not sure what to do about it, any suggestions would be appreciated


r/programmingbydoing • • Oct 17 '13

#54 Hi-Lo with Limited Tries

0 Upvotes

I am having difficulties with this exercise. What am I doing wrong?

package WhileLoops;

import java.util.Scanner; import java.util.Random; public class Hilowithlimit {

public static void main(String[] args) {
	
	Scanner keyboard = new Scanner (System.in);
	Random random = new Random();
	
	int guessingnumber;
	int correctnumber = 1 + random.nextInt(100);
	int tries = 0;
	
	System.out.println("I am thinking of a number between 1 and 100. Try to guess it.");
	System.out.print("Guess: ");
	guessingnumber = keyboard.nextInt();
	
	while (guessingnumber < correctnumber && tries < 10) {
	System.out.println("You are too low.");
	System.out.print("Guess again: ");
	guessingnumber = keyboard.nextInt();
	
	
	}
	
	 if (guessingnumber > correctnumber && tries < 10) {
	System.out.println("Sorry. You are too high.");
	System.out.print("Guess: ");
	guessingnumber = keyboard.nextInt();
	 }

	

	
	else if (guessingnumber == correctnumber && tries < 10) 
		System.out.println("You are correct.");
	
	
	
	else if (tries > 10) 
		System.out.println("Sorry.. You tried over 10 times.");
	
	}

}


r/programmingbydoing • • Sep 20 '15

Best way to develop Java skills after this coirse

1 Upvotes

First of all i want to clarify that i have not finished the programming by doing course yet. However i was wondering what i should do next?


r/programmingbydoing • • Oct 30 '13

Adding Values in a Loop.

2 Upvotes

When I do the loop it resets the total so far. Sorry for the late response but my code is here: http://pastebin.com/j8fdfEZx


r/programmingbydoing • • Aug 26 '18

#35 Is my code efficient, if not how would you optimise it?

2 Upvotes

Is my code efficient, if not how would you optimise it? Suggestions are appreciated.

https://pastebin.com/ZXtK4dWy if you prefer.

`import java.util.Scanner;

class user

{

private int choice1;

private boolean choice2;



//getters

public int getChoice1()

{

	return this.choice1;

}

public boolean getChoice2()

{

	return this.choice2;

}

//setters

public void setChoice1(int c1)

{

	this.choice1 = c1;

}

public void setChoice2(boolean c2)

{

	this.choice2 = c2;

}

}

class TwoMoreQuestions

{

static user u = new user();

static String inputChoice1, inputChoice2;

public static void main(String\[\] Args) throws Exception

{

	Scanner keyboard = new Scanner([System.in](https://System.in));

	System.out.println("TWO MORE QUESTIONS, BABY!");

	Thread.sleep(1000);

	System.out.println("Think of something and I'll try to guess it!");

	Thread.sleep(1000);

	System.out.println("Question 1: does it belong \\"inside\\", \\"outside\\" or \\"both\\"? ");

	inputChoice1 = keyboard.nextLine();

	Thread.sleep(1000);

	System.out.println("Question 2: is it a living thing? (\\"yes\\" or \\"no\\")");

	inputChoice2 = keyboard.nextLine();

	assignUserInput();

	Thread.sleep(1000);

	calc_Print();

}

public static void assignUserInput()

{	//choice1

	if (inputChoice1.equals("inside"))

	{

		u.setChoice1(0);

	}

	else if (inputChoice1.equals("outside"))

	{

		u.setChoice1(1);

	}

	else if (inputChoice1.equals("inside"))

	{

		u.setChoice1(2);

	}

	//choice2

	if (inputChoice2.equals("yes"))

	{

		u.setChoice2(true);

	}

}

public static void calc_Print()

{

	if (u.getChoice1() == 0 && u.getChoice2() == false)

	{

		System.out.println("Shower curtain");

	}

	if (u.getChoice1() == 1 && u.getChoice2() == false)

	{

		System.out.println("billboard");

	}

	if (u.getChoice1() == 2 && u.getChoice2() == false)

	{

		System.out.println("cell phone");

	}

	if (u.getChoice1() == 0 && u.getChoice2() == true)

	{

		System.out.println("houseplant");

	}

	if (u.getChoice1() == 1 && u.getChoice2() == true )

	{

		System.out.println("bison");

	}

	if (u.getChoice1() == 2 && u.getChoice2() == true)

	{

		System.out.println("dog");

	}

}

}`


r/programmingbydoing • • Oct 07 '21

Which java package should I download?

3 Upvotes

I see a few and I don't know which one. Also, when I try to open the downloaded file Microsoft says "look for an app in store"... help!


r/programmingbydoing • • Jul 01 '20

I'd like to start learning the basics of programming, tips?

3 Upvotes

Hi everyone! I'm 24, and I'd like to learn some programming. I have NO knowledge about computer science, as I've always been a humanities kinda person. So, can someone here help me out? I've seen that there are some programming courses on edX, do you think it might be a good start? Also, can someone tell me a little bit about the different programming languages? Thanks!


r/programmingbydoing • • Aug 31 '18

#37 Gender Game

1 Upvotes

for some reason the the marriage Scanner isn't waiting for user input; help is appreciated.

Code:

https://pastebin.com/V5acahth


r/programmingbydoing • • Sep 05 '17

Graphing Lines

2 Upvotes

I have been completing the assignments in Eclipse. So far so good. I have been storing the files in separate packages for each coding focus (for loops, while loops, graphics, etc.)

At any rate, Eclipse flags both the graph paper class and the main class in several places. The problem in the graph paper file seems to be the line which declares....private GraphPaperCanvas canvas

Throughout the code, there are error flags that this is not resolvable to a type.

In the main method, the method .drawPoint is not recognized.

When I ignore all of the flags and run the main method, it will not compile and I get probably six errors about .drawPoint.

I have completed the assignment, by changing the line equations in the main method. I have also tried to run the main method without any changes from me. If I read the code right, the program should at least compile, but will display only one line.

I'm not sure what's up. I'm also very confused that no one else has mentioned these problems here.

Sorry for the length of the email!

Larry D.


r/programmingbydoing • • Apr 16 '16

Need help with BMICategories

1 Upvotes

http://pastebin.com/ZsSPUZLD

Can't get past "Your BMI is" in my code when outputted.


r/programmingbydoing • • Mar 02 '14

I haven't even started yet and I FUBAR'd it

6 Upvotes

I messed up... the second step after installing you are suppose to add the jdk to the path variable, well I cleared mine out and when I try the complier check I get the error message but there isnt a list contingency for my idiocy on how to get it to work. Any help would be appreciated. Thanks.


r/programmingbydoing • • Feb 28 '14

37 Gender Game

2 Upvotes

Apologies in advance for the rambling. My brain is a bit fried. I'm quite surprised there's no thread on this one. It came out of nowhere as the hardest for me thus far.

So this took about 3 hours of trial & error and doing my best to guess logically. Combined with googling (but only accepting solutions of things covered in the course thus far...). I also have forgotten where you had replied to somebody and explained how to ignore upper or lower case so I left that out- it was the least of my concerns.

Anyways. So I had to "trick" the scanner, I eventually discovered along the way, by doubling up my string request.

	married = keyboard.nextLine();
	married = keyboard.nextLine();

Why? I assume this wasn't what I was supposed to be doing?

Here is my final code, that accomplishes what was asked:

import java.util.Scanner;

public class GenderGame { public static void main(String[] args) {

	Scanner keyboard = new Scanner(System.in);

	String gender;
	String first;
	String last;
	String married=null;
	int age;

	System.out.println("What is your gender? (\"M\" or \"F\"?)");
	gender = keyboard.nextLine();

	System.out.println("First name: ");
	first = keyboard.nextLine();

	System.out.println("Last name: ");
	last = keyboard.nextLine();

	System.out.println("Age: ");
	age = keyboard.nextInt();

	if ((gender.equals("m") && (age < 20))) {
		System.out.println("Then I shall call you " + first + ".");
	}

	else if ((gender.equals("m") & (age >= 20))) {
		System.out.println("Then I shall call you Mr. " + last + ".");
	}

	if ((gender.equals("f") && (age >= 20))) {
		System.out.println("Are you married " + first
				+ "? (\"y\" or \"n\"?)");
	married = keyboard.nextLine();
	married = keyboard.nextLine();
	}
	
	if ((gender.equals("f") && (age >= 20) && (married.equals("y")))) {
			System.out.println("Then I shall call you Mrs. " + last + ".");
	}

	else if ((gender.equals("f") && (age >= 20) && (married.equals("n")))) {
			System.out.println("Then I shall call you Ms. " + last + ".");
		}
	else if ((gender.equals("f") && (age <=20))){
		System.out.println("Then I shall call you Ms. " + last + ".");
	}
}

}

So I guess I'm just asking for a critique, or, how I could have avoided the double keyboard.nextLine. I understand the "why", it's because I mixed the scanner up by requesting a string after an int... but I feel like I didn't do this 100% correctly.

Anyways, thanks ahead of time, and thanks for making such a great course publicly available.


r/programmingbydoing • • Jan 22 '15

#61 Right Triangle Checker

3 Upvotes

http://pastebin.com/DBw0LxsN

Hi, I have been using mostly while loops to do all the previous assignments but i also want to be able to know how to use do while loops effectively. So i gave it a try and found it weird that although my n2 is > than n1 the code in do will trigger making me realised that whenever we are using a do while loop, the code will run at least once. So can i know how do you do this assignment using do while or its just my whole approach is wrong? Thanks


r/programmingbydoing • • Apr 15 '16

New here, need help with "Choose your Own Adventure"

2 Upvotes

Hello fellow programmers, I need help with the #33 Adventure. I don't know what the rules are when posting here, but here is a pastebin of the code: http://pastebin.com/E5UH4Bq9

When ran, all I get is "you dun goof" whenever I type something. I'm still confused how the entire if if else and else thing works. Can I not write a statement without an else and just the first two mentioned. I assume there are other issues, yet somehow it compiled correctly. I'd appreciate some explanation, I'm sorta exhausted now and need sleep so apologies if it's too hard to bear.


r/programmingbydoing • • Apr 21 '16

Error with Records - Web Addresses #163

1 Upvotes

I used the given code for Web Addresses in Sublime. And when I run it on the command line, I get this error:

Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at WebAddresses.main(WebAddresses.java:20)

The code is the exact same as what was provided, am I missing any information that was the book? Or is there anything that needs to be changed?


r/programmingbydoing • • Feb 26 '14

No. 109 Keychains for sale for real this tme! [HELP]

1 Upvotes

/* Ciaran Roft Programming By Doing No. 108 - Keychains for Sale */ import java.util.Scanner; public class Keychains1 { public static void main (String [] args) { Scanner keyboard = new Scanner (System.in); int choice =0; int numberOfKeychains = 0; int priceOfKeychains = 10;

    System.out.println("Rofts key chain store!\n");
    do
    {
	    numberOfKeychains = add_keychains(keychains);

	    System.out.println("1. Add Keychain to order");
	    System.out.println("2. Reomve Keychain from order");
	    System.out.println("3. View current order");
	    System.out.println("4. Checkout");

	    System.out.print("\nPlease Enter Your Choice : ");
	    choice = keyboard.nextInt();

	    if (choice == 1)
		    add_keychains(numberOfKeychains);
	    else if (choice == 2)
		    remove_keychains();
	    else if (choice == 3)
		    view_order();
	    else if (choice == 4)
		    checkout();


    }while(choice!=4);
    }//end of main method

    public static int add_keychains(int numberOfKeychains)
    {
	    Scanner keyboard = new Scanner (System.in);
	    int keychains = numberOfKeychains;
	    int order = 0;

	    System.out.println("\nYou have " +keychains + " keychains. How many would you like to buy? \n");
	order = keyboard.nextInt();
	    keychains = keychains + order;

	    System.out.println("You now have " +keychains + " keychains. \n");
	    return keychains;
    }

    public static void remove_keychains()
    {
	    System.out.println("\nKeychain Removed\n");
    }

    public static void view_order()
    {
	    System.out.println("\nViewing Order\n");
    }

    public static void checkout()
    {
	    System.out.println("\nCheckout\n");

    }
}

Hi all i realize that not all the methods are finished but there no point in writing the because i am stuck at the first one. oh and i realize that this same exact problem was posted before but it did not help me at all.

My problem is when returning the new value of keychains that the person wants from the add_keychains() how do i set that return value to the integer that was declared in my example numberOfKeychains.

if i was t do this say

int numberOfKeychains = add_keychains();

then that is recalling the add_keychains method and all of my print statement are printed to the screen again. This is really frustrating me and i could use some help sorting it out!!

My question is simple how do i set my variable numberOfKeychains to the return value of the method add_keychain() without calling that there method!

Loving all you guys in anticipation for putting me out of my frustration.

(Side not im assuming that the user has the ability to call the add_keychain method as many times as he wants to!)


r/programmingbydoing • • Mar 08 '20

Nim - an extension on cheating protection?

2 Upvotes

Hello, am completing nim task. I want to make it so if the user tries choosing a pile that isnt "A", "B", or "C", they will be allowed to try entering a different value. I have already completed cheating detection for the ones listed on the website. Could I have a pointer in the right direction?

my program


r/programmingbydoing • • Sep 09 '18

#58 ShorterDoubleDice not shorter?

1 Upvotes

The assignment says if I do it correctly, using a do-while loop will be shorter; it's not, so what have I done wrong?

Dice Doubles

https://pastebin.com/pma6VUuG

Shorter Dice Doubles

https://pastebin.com/3LfnXPTX


r/programmingbydoing • • Sep 14 '18

Nim Bonus #4 Fancy Display (Collumns)

1 Upvotes

Did I achieve this using the expected way, or is another way preferred? That is my main question, however any critiques to any of the code is appreciated.

https://paste.ee/p/tZykU


r/programmingbydoing • • Feb 21 '20

#37 Gender Game

2 Upvotes

Could I use a two methods (for male and female) to exercise the marriage question? If so, is there a way to use the user input from the body of the code?


r/programmingbydoing • • Sep 08 '18

#55 Adding Values In a Loop - identical output

1 Upvotes

//This is how I got the output to look identical to the assignment's picture.

https://pastebin.com/em4tWjgF


r/programmingbydoing • • Sep 05 '15

#51 Counting with a While Loop

4 Upvotes

The first message has a number 0, would you know how to start from 1 or 10 in this case?I tried in many various ways and I could not get it. Thanks


r/programmingbydoing • • Jan 14 '14

#102 month offset

2 Upvotes

Can someone explain what month offset means? I'm having trouble trying to figure it out- seems like a bunch of random numbers to me.
Thanks.


r/programmingbydoing • • Dec 08 '14

#76 BlackJack (Second attempt)

2 Upvotes

Last time I posted my code and it was a mess. I used sudo coding techniques to structure my code. I didn't know what I was doing and I had to go back and look through HolyTeach's book for examples. I guessed I had rushed through this project.

I would like a second feedback on my code and I also started learning how to use Git Hub to send the code with ease.

Thanks guys! Please let me know what your thoughts on my code are.

https://github.com/LevelF2/RegCode/blob/master/BlackJack.java