5 days old beginner java experiment. :)

+2 Jim Dupont · January 15, 2015
Hi there :)
I may come acros as a total beginner and well thats because i am.
i dont like to click "next" on a tutorial video before i get the feeling of; "oke i got this" ( i think )
While not at home i dig into Java for dummies. All in all i have about 5 days experience of coding. 
Yay! Almost ready to code the world. 

Now to come to my point, i have been trying to make a very simple "stop smoking" program, with my current
knowledge.

A program that when you open it asks you the question, "how many days has it been?"
To which u enter the number of days and you get a health fact + little motivation text. 
 I have been trying different ways, different methods. While i do not have any error it just wont answer me.

here is the result of my little experiment, as u can see, it has been tempered with in order to achieve the result i desire.

import java.util.Scanner;

class Jimece{

 public static void main(String args[]){
  
  Scanner myScanner = new Scanner(System.in);
  
  int Firstday;
  int Secondday = 2;
     int Tirthday = 3;
  int Forthday = 4;
  int Fiftday = 5;
  
  System.out.print ("How many days since you kicked the habbit?");
  
  Firstday = myScanner.nextInt();
  Secondday = myScanner.nextInt();
  Tirthday = myScanner.nextInt();
  Forthday = myScanner.nextInt();
  Fiftday = myScanner.nextInt();
  
  Firstday = 1;
  System.out.print("Well done with your ");
  System.out.print(Firstday);
  System.out.println(" keep it up!");
  
 }
}

Maybe im trying to run ahead of myself :) 
Maybe i should force myself to click next on the video tutorials, watch all and then watch all again.

any pointers, advice is all welcome. 
I thank you!

Post a Reply

Replies

Oldest  Newest  Rating
0 Tim Herreijgers · January 21, 2015
You are doing the same thing twice:



System.out.print("Congratulations with your ");
System.out.print(days);
System.out.println(" day(s), keep it up!");


What you can do to clean your code up is this:


import java.util.Scanner;
class Current{
  public static void main(String args[]){
     Scanner myScanner = new Scanner(System.in);


     System.out.println("How many days since you kicked the habbit?");

     int days;
     days = myScanner.nextInt();


     System.out.print("Congratulations with your ");
     System.out.print(days);
     System.out.println(" day(s), keep it up!");

     switch (days){
     case 1:
     System.out.println("Today your blood presure have returned to normal levels.");
     break;
     case 2:
     System.out.println("Today your smell and taste abilities have returned to normal.");
     break;
  }
}


Because you have to print the same thing in both cases you can print it before the switch. It isn't needed but it looks better :p 
0 Jim Dupont · January 21, 2015
Awesome guys ^^
Once again thank you! 
I think my coin finally got rolling and i got my "Ah yes!" moment :) 

This is what i turned out to be now with your help.

import java.util.Scanner;
class Current{
public static void main(String args[]){
Scanner myScanner = new Scanner(System.in);


System.out.println("How many days since you kicked the habbit?");

int days;
days = myScanner.nextInt();



switch (days){
case 1:
System.out.print("Congratulations with your ");
System.out.print(days);
System.out.println(" day(s), keep it up!");
System.out.println("Today your blood presure have returned to normal levels.");
break;
case 2:
System.out.print("Congratulations with your ");
System.out.print(days);
System.out.println(" day(s), keep it up!");
System.out.println("Today your smell and taste abilities have returned to normal.");


}


}
}

Is there anything that should not be there or that should be there ? 
Or am i right to say that i kinda got it finally ;) "Proud moment"

Thanks!!
0 Troy Jones · January 16, 2015
    
switch (days) {
case 1:
System.out.println("Yay, one day!");
break;
case 2:
System.out.println("Yay, two day!");
break;
}
//so on and so forth
0 Jim Dupont · January 15, 2015
A super mega big thanks guys !! :)

This definitely got me back on track, i loved the tips and the example of how it can be done.

The reason btw behind all the variables which i was trying to force into the code, is that i was trying to figure out a way
to get a different text attached to a different number. :) 

Once again a big thanks! ^^
0 Nicholas Eason · January 15, 2015
I think what you may be looking for is the Switch statement :)

Here's the javadoc
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

A few tips:

Java convention for variable names is that the first letter is lowercase, then every "word" that comes after starts with an uppercase letter

i.e. iLikePizza

Second, you don't need to initialize the variables if you're not going to use their value.

For example, you have int secondDay = 2; then you change it to myScanner.nextInt();

Third, set myScanner.nextInt() to 1 variable, it makes it cleaner and doesn't require 5 user inputs for the same number.

I don't wanna tell you how to do it though because then what would you learn? :) Good luck
  • 1

Java / Android Development

107,307 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator