Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp

Reed Kinney

Greenhorn
+ Follow
since Mar 11, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Reed Kinney

The class name is already created when I create the project through netbeans, it just uses whatever I name the project. The loop is there so they can't enter a number greater than 50 or less than 1. We have to use Option Pane for this project which is annoying cause we haven't used it since the start of the semester which is also why I think i'm having troubles with this project. Okay, I'll check out that page!
3 days ago
I'm writing a program for class that and i'm having troubles with the third step which is "Ask the user for the name of an output file. The program should create the file." I've created output files that I've chosen but I don't know how to incorporate one that the user creates. So far I was thinking of creating a string called filename which but it's not working and I don't think it will cause it's not a .txt. I've looked around the internet but can't really find something similar. This is what I have so far
3 days ago
I almost have it I think, we had another lecture on it and i'm feeling a little more confident about loops. I'm so close, I just can't get the program to print in multiples of 5 because in my for loop I need to assign int i to 1 otherwise my program won't work. Ideally I think i'd want it set to 0 so it goes up by 5. I think I may have something wrong with my for loops though, I kinda just copied what we did in class and hoped for the best lol. I also don't know if I can fix this by adding something in an else statement, like if the number isn't divisible by 5 add a number to i so that it is.  
3 days ago
I'm so confused lol this is beyond frustrating jeez, I was taught that when I have a line like 22-23 that since I have a scanner in the line then whatever the user inputs is stored in whatever I name the variable. I thought this was true since it worked in previous programs? I updated my code a little bit, am I close to getting this right? I feel like I just don't know what to name the initialization of the loop and how to design it so whatever the user inputs the output is in multiples of 5. I have the modulus set up properly I think but I don't know what to add to it in the 'else' statement. For example, if the user enters a 7 I would write +=3 in my else statement but if the user enters any number besides one that's -3 a multiple of 5 then my program doesn't do what's asked. package pkg4.pkg2;
import java.util.Scanner;

/**
*
* @author bambo
*/
public class Main {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
         Scanner keyboard = new Scanner(System.in);

         
         System.out.println("Please enter a number greater than 5");
         int n = keyboard.nextInt();
     
         if (n<=5)
         {
             System.out.println("bad number, enter a number greater than 5");
             keyboard.nextInt();
         }
       
       
         for(; n>0; n+=5)
         {
           
            if (n%5==0)
            {
                System.out.println(n);
            }
            else {
               
            }
     

         
       
   }
   }            
   }
1 week ago
Line 23 stores the user's input in the integer 'n'.
1 week ago
I don't understand what the initialization part of the for loop would be, the user chooses 'n' so do I just leave the first part of the for loop blank? If I assign a value to 'n' in the first part of the for loop then it contradicts the whole program and it doesn't work.
1 week ago
I've only had a brief lecture on it in my class and for some reason i'm having trouble understanding how to implement it in this program.
1 week ago
I almost got it now! I figured out the modulus, the only thing now is that my program keeps printing till 100 and doesn't stop at what 'n' is. Lastly, it starts at 10 instead of 1. I feel like my variable 'j' doesn't belong but if I assign n to 0 than the program doesn't run.

package pkg4.pkg2;
import java.util.Scanner;

/**
*
* @author bambo
*/
public class Main {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
         Scanner keyboard = new Scanner(System.in);

         
         System.out.println("Please enter a number greater than 5");
         int n = keyboard.nextInt();
         int j;
         if (n<=5)
         {
             System.out.println("bad number, enter a number greater than 5");
             keyboard.nextInt();
         }
       
       
          for(j=0; n>5; n+=5)
         {
             if (n %5 ==0)
             {
             System.out.println(n);
             }
             else
             {
                 n=5;
             }
         while (n>=100)
             {
                 System.exit(0);
             }

         
       
   }
   }            
   }
1 week ago
I added a variable J to the mix, it works now but I still can't get it to print in multiples of 5. I feel like using % isn't usefu because itl can only really work if you know how much you need to add but I don't cause the user decides. I'm probably wrong though lol programming is frustrating
1 week ago
This is what I need to do: Write a program that asks the user for a number greater than 5 and prints all values between 1 and n that are multiples of 5

This is my code so far, i'm having trouble printing out only multiples of 5. I don't think using a modulus is the right way but i'm not sure. Any help would be awesome!



   
1 week ago