5 Replies - 328 Views - Last Post: 03 December 2012 - 02:05 PM Rate Topic: -----

#1 Thenewb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-December 12

Accessing variables in main from a custom class?

Posted 03 December 2012 - 01:04 PM

I was wondering if it is possible to access a variable that is in my main "content" from a custom class to be able to execute the method? I want to be able to take the variable "content" and write it to a text file called results and i cant seem to figure out how to do that. Any help or suggestion is really appreciated. If there is an easier way to do this, could you please let me know and maybe provide an example or a place with an example of using your method. If you have any questions about what i am asking, just let me know and ill try to explain it better because i can see how this may be hard to understand. I am using the methos in class 2 to be able to open, write to, and close the file, and in my main i execute the method, but i want the variable "content" that is in my main to be in my class.

Thank you for any help provided.

~thenewb

package finalproject;
import java.io.*;
import java.util.Formatter;
import java.util.Random;
import java.util.Scanner;

public class FinalProject {
    @SuppressWarnings("empty-statement")
  public static void main(String[] args) throws IOException, IOException {
           
           String PC;
           String Xbox360;
           String Wii;
           String N64; 
           String NES;
           String SEGA;   // declaring variables
           int game; //games for PC
           int game2; // games for xbox360
           int game3;//games for Wii
           int game4;//games for N64
           int game5;//games for NES
           int game6;//games for SEGA
           int content = 0; 
           String GameSystem = "0"; 
           String Filename; // file name
           
          
           
        Scanner input = new Scanner(System.in);// searching for input
        FinalProjectClass nameObject = new FinalProjectClass ();//using method from other class
        
        System.out.println("Enter Your Name Here:  ");//asking user for name
        String name = input.nextLine();
        
        nameObject.name(name);//displaying the name using method
        
       
       System.out.println("What Game System are you playing? ");//asking what game system the user is playing
       System.out.println("Enter the Number corresponding to your system, Use this list to help you: "
               +"PC = 1 "
               +"Xbox360 = 2 "
               +"Wii = 3 "
               +"N64 = 4 "
               +"NES  = 5 "
               +"SEGA = 6 ");
       
       Scanner keyboard = new Scanner(System.in);
       GameSystem = keyboard.nextLine();
       int SystemChoice= Integer.parseInt(GameSystem);
       
        Random generator = new Random ();
           switch (SystemChoice){                    // Using switch to allow system to file through the different variables
           case 1: 
                    
                    
                    
                    game = generator.nextInt(25) + 1;
                    content = game;
                   
                   switch(game){ // selects game based on number entered and tells the user the name of the game.
                       
                       case 1 :
                           System.out.println(" You should Play the Game: Minecraft ");
                           break;
                       case 2:
                           System.out.println(" You should Play the Game: League Of Legends ");
                           break;
                       case 3:
                           System.out.println(" You should Play the Game: The Sims 3 ");
                           break;
                       case 4:
                           System.out.println(" You should Play the Game: CrossFire ");
                           break;
                       case 5:
                           System.out.println(" You should Play the Game: Left 4 Dead 2 ");
                           break;
                       case 6:
                           System.out.println(" You should Play the Game: Borderlands 2 ");
                           break;
                       case 7:
                           System.out.println(" You should Play the Game: Empire Total War ");
                           break;
                       case 9: 
                           System.out.println(" You should Play the Game: EverLong ");
                           break;
                       case 10: 
                           System.out.println(" You should Play the Game: Starcraft II ");
                           break;
                       case 11:
                           System.out.println(" You should Play the Game: Call of Duty: Modern Warfare 2 ");
                           break;
                       case 12:
                           System.out.println(" You should Play the Game: Call of Duty Modern Warfare ");
                           break;
                       case 13:
                           System.out.println(" You should Play the Game: Call of Duty Black Ops 2 ");
                           break;
                       case 14:
                           System.out.println(" You should Play the Game: Guild Wars 2 ");
                           break;
                       case 15:
                           System.out.println(" You should Play the Game: Prototype ");
                           break;
                       case 16:
                           System.out.println(" You should Play the Game: Need For Speed: Most Wanted ");
                           break;
                       case 17:
                           System.out.println(" You should Play the Game: Skyrim ");
                           break;
                       case 18:
                           System.out.println(" You should Play the Game: MoonBase Alpha ");
                           break;
                       case 19:
                           System.out.println(" You should Play the Game: World Of Tanks ");
                           break;
                       case 20:
                           System.out.println(" You should Play the Game: Sim City 3000 ");
                           break;
                       case 21:
                           System.out.println(" You should Play the Game: Bloodline Champions ");
                           break;
                       case 22:
                           System.out.println(" You should Play the Game: Zoo Tycoon ");
                           break;
                       case 23: 
                           System.out.println(" You should Play the Game: Spelunky ");
                           break;
                       case 24:
                           System.out.println(" You should Play the Game: Assasins Creed ");
                           break;
                       case 25:
                           System.out.println(" You should Play the Game: World Of Warcraft ");
                           break;}
              break;
        case 2: 
                    
                    
                    game2 = generator.nextInt(21) + 1;
                    content = game2;
                   
                   switch(game2){
                       case 1:
                           System.out.println(" You should Play the Game: Mass Effect ");
                           break;
                       case 2:
                           System.out.println(" You should Play the Game: Call of Duty 2 ");
                           break;
                       case 3:
                           System.out.println(" You should Play the Game: RockBand ");
                           break;
                       case 4:
                           System.out.println(" You should Play the Game: Need for Speed: Hot Pursuit ");
                           break;
                       case 5:
                           System.out.println(" You should Play the Game: Kameo ");
                           break;
                       case 6:
                           System.out.println(" You should Play the Game: Ghost Recon");
                           break;
                       case 7:
                           System.out.println(" You should Play the Game: Crackdown ");
                           break;
                       case 9: 
                           System.out.println(" You should Play the Game: Crackdown 2 ");
                           break;
                       case 10: 
                           System.out.println(" You should Play the Game: Oblivion");
                           break;
                       case 11:
                           System.out.println(" You should Play the Game: Call of Duty: Modern Warfare 2 ");
                           break;
                       case 12:
                           System.out.println(" You should Play the Game: Call of Duty Modern Warfare ");
                           break;
                       case 13:
                           System.out.println(" You should Play the Game: Call of Duty Black Ops 2 ");
                           break;
                       case 14:
                           System.out.println(" You should Play the Game: Halo 3 ");
                           break;
                       case 15:
                           System.out.println(" You should Play the Game: Gears of War ");
                           break;
                       case 16:
                           System.out.println(" You should Play the Game: Need For Speed: Most Wanted ");
                           break;
                       case 17:
                           System.out.println(" You should Play the Game: Skyrim ");
                           break;
                       case 18:
                           System.out.println(" You should Play the Game: Madden NFL '10 ");
                           break;
                       case 19:
                           System.out.println(" You should Play the Game: NCAA Football '08");
                           break;
                       case 20:
                           System.out.println(" You should Play the Game: NBA Live '08 ");
                           break;
                       case 21:
                           System.out.println(" You should Play the Game: BattleField 3 ");
                           break;}
                       
                 break;
        case 3:
                    
                    game3 = generator.nextInt(19) + 1;
                    content = game3;
                   
                   switch(game3){
                       case 1:
                           System.out.println(" You should Play the Game: Carnival Games ");
                           break;
                       case 2:
                           System.out.println(" You should Play the Game: Super Smash Bros. Brawl ");
                           break;
                       case 3:
                           System.out.println(" You should Play the Game: Super Swing Golf ");
                           break;
                       case 4:
                           System.out.println(" You should Play the Game: Super Mario Galaxy ");
                           break;
                       case 5:
                           System.out.println(" You should Play the Game: Super Mario Galaxy 2  ");
                           break;
                       case 6:
                           System.out.println(" You should Play the Game: MarioKart Wii");
                           break;
                       case 7:
                           System.out.println(" You should Play the Game: Family Game Night ");
                           break;
                       case 9: 
                           System.out.println(" You should Play the Game: Animal Crossing: City Folk ");
                           break;
                       case 10: 
                           System.out.println(" You should Play the Game: Mario and Sonic at the Olympic Games");
                           break;
                       case 11:
                           System.out.println(" You should Play the Game: Mario and Sonic at the Olympic Winter Games ");
                           break;
                       case 12:
                           System.out.println(" You should Play the Game: NBA Live '09 All-Play ");
                           break;
                       case 13:
                           System.out.println(" You should Play the Game: WiiPlay ");
                           break;
                       case 14:
                           System.out.println(" You should Play the Game: WiiSports");
                           break;
                       case 15:
                           System.out.println(" You should Play the Game: The Legend of Zelda: Twilight Princess ");
                           break;
                       case 16:
                           System.out.println(" You should Play the Game: The Legend of Zelda: Skyward Sword ");
                           break;
                       case 17:
                           System.out.println(" You should Play the Game: Super Mario Bros. Wii ");
                           break;
                       case 18:
                           System.out.println(" You should Play the Game: Donkey Kong Country Returns");
                           break;
                       case 19:
                           System.out.println(" You should Play the Game: Links Crossbow Training");
                           break;}
                break;
      
        case 4: 
                    
                   
                    game4 = generator.nextInt(32) + 1;
                    content = game4;
                   
                   switch(game4){
                       case 1:
                           System.out.println(" You should Play the Game: Harvest Moon 64 ");
                           break;
                       case 2:
                           System.out.println(" You should Play the Game: Super Smash Bros.");
                           break;
                       case 3:
                           System.out.println(" You should Play the Game: Mario Party 2 ");
                           break;
                       case 4:
                           System.out.println(" You should Play the Game: Quest 64");              
                           break;
                       case 5:
                           System.out.println(" You should Play the Game: GoldenEye 007  ");
                           break;
                       case 6:
                           System.out.println(" You should Play the Game: Mariokart 64" );
                           break;
                       case 7:
                           System.out.println(" You should Play the Game: Tonic Trouble ");
                           break;
                       case 9: 
                           System.out.println(" You should Play the Game: The Legend of Zelda: Ocarina Of Time ");
                           break;
                       case 10: 
                           System.out.println(" You should Play the Game: The Legend of Zelda: Majora's Mask ");
                           break;
                       case 11:
                           System.out.println(" You should Play the Game: Mario Tennis ");
                           break;
                       case 12:
                           System.out.println(" You should Play the Game: Goemons Great Adventure ");
                           break;
                       case 13:
                           System.out.println(" You should Play the Game: Rugrats Scavenger Hunt ");
                           break;
                       case 14:
                           System.out.println(" You should Play the Game: Rainbox Six" );
                           break;
                       case 15:
                           System.out.println(" You should Play the Game: Tony Hawks Pro Skater ");
                           break;
                       case 16:
                           System.out.println(" You should Play the Game: Pokemon Stadium ");
                           break;
                       case 17:
                           System.out.println(" You should Play the Game: Pokemon Stadium 2 ");
                           break;
                       case 18:
                           System.out.println(" You should Play the Game: Diddy Kong Racing");
                           break;
                       case 19:
                           System.out.println(" You should Play the Game: A Bugs Life ");
                           break;
                       case 20:
                           System.out.println(" You should Play the Game: Mega Man 64" );
                           break;
                       case 21:
                           System.out.println(" You should Play the Game: Yoshi's Story");
                           break;
                       case 22:
                           System.out.println(" You should Play the Game: Glover ");
                           break;
                       case 23:
                           System.out.println(" You should Play the Game: Ready 2 Rumble Boxing ");
                           break;
                       case 24:
                           System.out.println(" You should Play the Game: Wipeout 64");
                           break;
                       case 25:
                           System.out.println(" You should Play the Game: Extreme XG2 ");
                           break;
                       case 27:
                           System.out.println(" You should Play the Game: Banjo-Kazooie " );
                           break;
                       case 28:
                           System.out.println(" You should Play the Game: Super Mario 64");
                           break;
                       case 29:
                           System.out.println(" You should Play the Game: Starfox 64 ");
                           break;
                       case 30:
                           System.out.println(" You should Play the Game: Kirby 64 The Crystal Shards ");
                           break;
                       case 31:
                           System.out.println(" You should Play the Game: Pokemon Puzzle League 64");
                           break;
                       case 32:
                           System.out.println(" You should Play the Game: Dr. Mario 64 ");
                           break;}
                 break;
        case 5:
            
                    
                    game5 = generator.nextInt(10) + 1;
                    content = game5;
                   
                   switch(game5){
                       case 1:
                           System.out.println(" You should Play the Game: Troy Aikman Football ");
                           break;
                       case 2:
                           System.out.println(" You should Play the Game: Sonic The Hedgehog ");
                           break;
                       case 3:
                           System.out.println(" You should Play the Game: College Slam ");
                           break;
                       case 4:
                           System.out.println(" You should Play the Game: Double Dribble ");
                           break;
                       case 5:
                           System.out.println(" You should Play the Game: Techmo Super Hockey ");
                           break;
                       case 6:
                           System.out.println(" You should Play the Game: Batman Forever ");
                           break;
                       case 7:
                           System.out.println(" You should Play the Game: Spider-Man ");
                           break;
                       case 9: 
                           System.out.println(" You should Play the Game: Triple Play ");
                           break;
                       case 10: 
                           System.out.println(" You should Play the Game: Zoop ");
                           break;}
                       
                       break;
           
        case 6:

                   
                    game6 = generator.nextInt(10) + 1;
                    content = game6;
                   
                   switch(game6){
                       case 1:
                           System.out.println(" You should Play the Game: StarTropics ");
                           break;
                       case 2:
                           System.out.println(" You should Play the Game: ExciteBike ");
                           break;
                       case 3:
                           System.out.println(" You should Play the Game: Prince of Persia ");
                           break;
                       case 4:
                           System.out.println(" You should Play the Game: Super Mario Bros. 2" );
                           break;
                       case 5:
                           System.out.println(" You should Play the Game: Golf ");
                           break;
                       case 6:
                           System.out.println(" You should Play the Game: Jordan v.s Bird oneon one ");
                           break;
                       case 7:
                           System.out.println(" You should Play the Game:  Super Mario Bros./ Duck Hunt");
                           break;
                       case 9: 
                           System.out.println(" You should Play the Game: Rad Racer ");
                           break;
                       case 10: 
                           System.out.println(" You should Play the Game: Teenage Mutant Ninja Turtles II: The Arcade Game ");
                           break;}
             break;}
           
           
           System.out.println("Thank you for using the video game selector and we hope you enjoy the video game that we have selected for you. "
                   + "Have a Nice Day ");
               
   
           
           
           createfile g = new createfile();
           g.openFile();
           g.addRecords();
           g.closeFile();        
    
           }}                       
                           
                           
                           



Class 1
public class FinalProjectClass {
 public void name(String name){
        System.out.println("Hello" +  name);
}

public static void main(String[] args) throws IOException{



Class 2
package finalproject;
import java.io.*;
import java.lang.*;
import java.util.*;
public class createfile {
    
    private Formatter x;
    
    public void openFile(){
    try{
        x = new Formatter ("Results.txt");
       }
        catch(Exception e){
            System.out.println("you have an error");
        }
    
    }
    public void addRecords(){
        x.format("content");
    }
public void closeFile(){
    x.close();
}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Accessing variables in main from a custom class?

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Accessing variables in main from a custom class?

Posted 03 December 2012 - 01:17 PM

At which line in main() method are you having problems. Dont expect anyone to browse your whole code. Post only relevant shit or point to the exact point.
Was This Post Helpful? -1
  • +
  • -

#3 Thenewb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-December 12

Re: Accessing variables in main from a custom class?

Posted 03 December 2012 - 01:44 PM

View Postdarek9576, on 03 December 2012 - 01:17 PM, said:

At which line in main() method are you having problems. Dont expect anyone to browse your whole code. Post only relevant shit or point to the exact point.

On lines: 58, 139, 209, 270, 368,and 405 on the main i have the randomly generated number being set equal to the variable content and i want that to equal the same variable content that is on line 19 in class 2, but i do not know how to do that or even if it is possible. i basically want the take the variable content and put it on a file and keep it there to record the data being made from the program running. Thanks for the reply

~thenewb
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Accessing variables in main from a custom class?

Posted 03 December 2012 - 01:53 PM

Your design is false to start with.. sorry
A main() method is just to lauch an application and should only have a few (3-10) lines of code
As Java is an Object Oriented language the main is just used to create an Object and then use it's methods. Other classes can then access the instance variable of that object

class MyClass {

     public static void main(String[] args) {
       int a, b, c;
       ....
       .... 
    
       OtherClass o = new OtherClass();
       ....


should be
class MyClass {
     int a, b, c;

     void start() {
        ...
        ...
        OtherClass oc = new OtherClass(this);

     }

     public static void main(String[] args) {
        MyClass mc = new MyClass();
        mc.start();
     }  // that's it !!! main() finishes here
}


Now if OtherClass received as parameter the MyClass object to its constructor it can call back getter of MyClass like getContent() to access that variable
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,084
  • Joined: 27-December 08

Re: Accessing variables in main from a custom class?

Posted 03 December 2012 - 02:02 PM

View Postdarek9576, on 03 December 2012 - 03:17 PM, said:

At which line in main() method are you having problems. Dont expect anyone to browse your whole code. Post only relevant shit or point to the exact point.

I think the code posted was relevant. Let's keep things a little more professional as well, please.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Accessing variables in main from a custom class?

Posted 03 December 2012 - 02:05 PM

Anyhow 400 lines is way too much for a method so you should split your code in different methods which is easier in the context of a OO oriented approach
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1