Splitting Up Main Method / Class?

Jan 29, 2014

I've written a program just for the sake of it (to learn) and it seem's like theres quite a lot in the main method that perhaps could be split up into seperate classes. I'm not too sure where should start with this though, or what my thought process should have been as I was writing the program.

import java.util.Scanner;
 public class Loops {
 public static void main(String[] args) { 
int answer = 16; 
Scanner scan = new Scanner(System.in); 
// Question
 System.out.println("What is 4 x 4 ?"); 

[code]...

--- Update ---

here's a version without code comments as they might make it harder to read here -

import java.util.Scanner;
 public class Loops {
 public static void main(String[] args) {
 int answer = 16;
 Scanner scan = new Scanner(System.in);
 System.out.println("What is 4 x 4 ?");
 int userAnswerInt = 0;

[code]...

View Replies


ADVERTISEMENT

Error Passing Value Of A Variable From One Class To Main Method Of Another Class

Jan 8, 2014

I've 3 classes.

1. Circle
2. GetInputFromUser
3. testCircle

package ABC;
public class Circle {
private double radius;
public double getRadius() {
return radius;

[Code] .....

In the testCircle class, in the line: getRadius = ui1.GetInput();

It's showing the error: The method GetInput(float) in the type GetInputFromUser is not applicable for the arguments ()

And when I do: getRadius = ui1.GetInput(rad);

It's showing the error: rad cannot be resolved

View Replies View Related

How To Call A Method That Exist Within A Class Into Main Method

Feb 13, 2014

I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?

public class locker { 
public static void main(String[] args) {
CombinationLock();

[code]....

View Replies View Related

How To Import Class Into Class With Main Method

Mar 3, 2014

Im writing a simple program to understand classes and objects. Basically what I have is a file called Program.java where I have my main method.I have another file called Person.java which I want to use to create Person objects. That person can have a name, email adress, phone number, etc.I put both these files in the same folder.in Program.java my first statement is:

Java Code: import Person.java mh_sh_highlight_all('java');

My problem is that when I compile Program.java i get an error message saying that the package Person.java does not exist.So my question is, when you create a class that you want to use for objects, how do you import that class into your class with the main method so that you can use instances of your other class?

View Replies View Related

Calling Method Of A Class From Main Class?

Aug 31, 2014

// Add range to Vehicle.
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon

// Display the range.
void range() {
System.out.println("Range is " + fuelcap * mpg);

[Code] ....

I'm compiling it in Eclipse and this continues to show in the console display

Minivan can carry 7. Exception in thread "main" java.lang.NoSuchMethodError: Vehicle.range()V
at AddMeth.main(AddMeth.java:34)

View Replies View Related

Access A Method Of Class From Main

Feb 9, 2014

This time I have to make a Black Jack game ( I guess this is a classic) I have created Three classes for this BlackJack( Main), Card, and Player.

What I am trying to do is put the Give one card to the player and remove it from the deck into a separate procedure because I will be doing this several times during the game.

This is the code I have so far Under the class BlackJack.

Java Code:

package black.jack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

[Code] .....

View Replies View Related

Create Method In Main Class And Use It On Object

Feb 26, 2015

I've been writing classes over and over for school. So I create a class outside of my main class. I create a new constructor and then create objects from my main class. I hope that makes sense. So i use methods in that class to work with the object. So I have an object name I've created <dot> method name. So I can create objects and then use methods from the class, but I'm wondering can I create a method in my main class and use it on that object? I don't understand how to do that.

View Replies View Related

How To Call String From Main Method In Different Class

Dec 9, 2014

The following code is located in my main class called InjectionFix. I have another class where I need to call or initiate the string in the code below. How can I achieve this. I tried to create an Object of the class but I cant do this because my other class doesnt contain a main method.How can I get the other class to initiate the code below which is loacted in my main class.
 
public static String escapeDN(String name) {
  StringBuilder sb = new StringBuilder();
  // space or # character at the beginning of a string
  if ((name.length() > 0) &&
        ((name.charAt(0) == ' ') ||
             (name.charAt(0) == '#'))) {

[Code] .....

View Replies View Related

Is It Correct To Declare A Class Inside Main Method

Jan 21, 2014

I saw an example where an (inner)class is declared inside the main method, this is correct or not and why/when it's reasonable to use?so smth like this

public class myClass()
{
public static void myMethod(myInnerClass obj)
{
if (obj.method())

[code]....

View Replies View Related

Binary Search Tree - Calling FindSmallest Method In Main Class

Nov 3, 2014

How should I call my findSmallest method in the main class.. Here is the code:

public class Tester {
public static void main(String[] args){
try {
BinaryTree<Integer> myTree2 = new BinaryTree<Integer>();
 
myTree2.insert(5);
myTree2.insert(2);
myTree2.insert(7);

[Code] ....

So the question is what kind of parameter I should pass in myTree2.findSmallest()??? Here is my findSmallest method..

public E findSmallest(Node<E> parent){
if(parent.left.left == null){
E returnValue = parent.left.data;
parent.left = parent.left.right;
return returnValue;
} else {
return findSmallest(parent.left);
}
}

View Replies View Related

How To Make A Method Splitting A String

Jan 14, 2014

How do I make a method that can seperate parts of a String. The String is [name,aid]... example [John,5],

I want to remove [ , and ] from the String... How can I do this?

This is what i've done this far:

static public String splitta(String tf)
{
String part1 = "";
String part2 = "";
String part11 = "";
String part22 = "";
String part111 = "";
String part222 = "";
String[] parts = new String[2];

[Code] ....

This dosn't work, it wont compile... It states the following:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at testprojekt.Validering.splitta(Validering.java:130)

View Replies View Related

Splitting A String Which Is Double Hours Method

May 13, 2014

I am really struggling to make the Split String method work with another method which is double hours.

HtmlElement span = new HtmlElement(SPAN_OPEN, SPAN_CLOSE);
span.setValue("Drive Time: ");
td.addNestedElement(span);
HtmlElement span2 = new HtmlElement(SPAN_OPEN, SPAN_CLOSE);
span2.addAttribute("class", "drive_time");
String time=String.valueOf(showSet.getTransferHours());
/*Split the Strings into Hours and Minutes*/

[code]...

View Replies View Related

Splitting Values From A File With String Tokenizer And Storing It In Another Class

Aug 26, 2014

I'm new to java. I have a Product class with getters and setters.

E.g. setProdType & getProdType

I want to store the values from a file into that

StringTokenizer token = new StringTokenizer(line,"**");
while(token.hasMoreElements()) {
int p.setProdType = Integer.parseInt(token.nextElement().toString());
}

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?

View Replies View Related

Writing A Method That Returns Words Without Four Letters Back To Main Method

May 27, 2014

I have to write a method called censor that gets an array of strings from the user as an argument and then returns an array of strings that contains all of the original strings in the same order except those with length of 4. For example if cat, dog, moose, lazy, with was entered, then it would return the exact same thing except for the words with and lazy.

Currently, my code so far just prints [Ljava.lang.String;@38cfdf and im stuck.

import java.util.Scanner;
 public class censorProgram {
public static void main (String args[]){
Scanner input = new Scanner (System.in);
System.out.println ("How many words would you like to enter?");
int lineOfText = input.nextInt();
 
[Code] ....

I have to make new string array in the method and return words without four letters in the main method

View Replies View Related

Declaring Class In Main Class - Constructor Cannot Applied To Given Types

Aug 1, 2014

So i declared a class in main class but it seems there's error when i compile:

constructor xx in class xx cannot applied to given types

This is my java class:

public class trainer extends person{
String classType;
public trainer(String name, String gender, String address, int id, String classType) {
super(name,gender,address,id);
this.classType=classType;

[Code] ....

And this is the way i declared in main class:

trainer tr = new trainer();

And what i want to do is:

tr.toString();

View Replies View Related

How To Enter In Diameter In Main Class For Sphere Class

Apr 1, 2015

public class Sphere {
public double diameter;
public double volume;
public double area;
public double fourThirds = 4/3;
public Sphere(double someDiameter){
someDiameter = diameter;

[Code] ....

I am trying to get this code so that I only enter the diameter once in the sphere object1 = new Sphere(4); but I can't get it to work right. I can get the diameter to work with the calculate volume and area methods but that's it.

View Replies View Related

Main Method Separated From Other Method

Jan 13, 2014

Here's a class called Fibonacci

Java Code:

public class Fibonacci {
public static int fib(int n) {
if (n < 2) {return n;}
else {return fib(n-1)+fib(n-2);}

[code]....

it contains one method called fib() and one main method.If I would want to have the main method in another class than fib(), how would I write the two classes? Only cutting the main method from this class to another one doesn't work.My question is, is the reason it doesn't work because I then would have to have a constructor in the Fibonacci class, and create a Fibonacci object first which I then use the method on?

View Replies View Related

Using ComboBox In Main Class From Another Class

Oct 30, 2014

Need to use a ComboBox from another class (Beginning Java forum at JavaRanch)

When my file is saved it has the values off the main gui's comboBox in variable 'env' which I then write to file using. I just need to access the comboBox in my second class so I can use it in main as I'm using the wrong one atm- if that makes sense?

View Replies View Related

Using A Variable From Main Class In Another Class

May 31, 2014

I have a program with 4 classes, all of them in the same package, one of them is the Main class, and in that class I declared a variable named "port" of type int. One of the 3 another ones is the class Connection class, which it requires the port variable. I want to use this variable in the Connection class. How can I do it?Both classes are shown below:

Main.java
package server;
/* Imports */
/* Another variables */
int port; /* <-- IS THIS ONE */

[code]....

View Replies View Related

Pass A Value From Main Class To Another Class

Feb 5, 2015

THIS IS MY MAIN CLASS:

Java Code:

import java.io.FileNotFoundException;
public class PrimaClasse {
public static void main(String[] args) throws FileNotFoundException {
SecondaClasse oggettoSeconda = new SecondaClasse();
oggettoSeconda.controlloNomi();

[code]....

Now it's working and from the main class i can controll the second class BUT. i want that is the main class that ask the user name, and i want pass that value to the second class. what code i must change?why eclipse wants me insert this import java.io.FileNotFoundException; and this throws FileNotFoundException for not give me an error?

View Replies View Related

No Main Method?

Jul 23, 2014

I'm trying to make a TBG (Text Based Game) in Java for Android. Now, when I try to run it, it says No Main Method to run this program. The compiler also checks the program and tell me there are no errors.

View Replies View Related

Why Is Main Method Static

Oct 1, 2014

in java why main method is always static in nature...

View Replies View Related

Calling A Method From Main

Jan 26, 2014

The idea is to create a program to add plants and retrieve plants from a Nursery. I created a class with the properties of the plants and also there is the class an Array list to keep track of the plants entered ( they will have to be printed later) (I am not too sure if adding a public class there is the best option.

The program will allow the user to pick and action and from there the code will do something. I don't want to have all the code inside 'main' The problem is in line 114.This is what I have so far.

Java Code:

package plant.nursery;
import java.util.ArrayList;
import java.util.Scanner;
/**Class to create a plant for a nursery.
public class PlantNursery

[code]....

View Replies View Related

Define Main Method

Apr 26, 2014

this is my program error occured run time,but compile sucessfully

{error msg-Error: Main method not found in class helloworldapp.HelloWorldApp, please define the main method as:
public static void main(String[] args)}
class HelloWorldApp
{
private int empno;
private float salary;
void setHelloWorldApp()
{
empno=12;
salary=1200;

[code]....

View Replies View Related

Calling A Method To Main

Nov 17, 2014

I'm trying to call a method to my main method, but I can't seem to get it to work and it keeps resulting in a compile error.

import java.io.*;
import java.awt.Point;
import java.text.DecimalFormat;
import java.util.Random;
public class Chase {
public static void main(String args[]){

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved