This is super simplified code of what I was doing, but the results are the same. I can comment out in.close() in class 1 and it will fix the error. But then I'm left with an open Scanner for the rest of the project. And changing the variable names are not a fix.
class1:
package scannerDebug;
import java.util.Scanner;
public class Class1 {
private String name_;
public Class1(String name) {
name_ = name; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Your name: ");
Class1 ex1 = new Class1(in.next());
System.out.println("eex1" + ex1.name_);
in.close();
}
}
class2
package scannerDebug;
import java.util.Scanner;
public class Class2 {
private String name_;
public Class2(String name) {
name_ = name; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Your name: ");
Class2 ex2 = new Class2(in.next());
System.out.println("ex2" + ex2.name_);
in.close();
}
}
Driver class
package scannerDebug;
public class driver {
public static void main(String[] args) {
Class1.main(args);
Class2.main(args);
}
}
Closing a Scanner also closes the underlying stream. To solve this, simply use a single Scanner in your driver class and use it in both Class1 and Class2:
public class driver {
private static final SCANNER = new Scanner(System.in);
public static void main(String[] args) {
Class1.main(args);
Class2.main(args);
}
public static Scanner getScanner() {
return SCANNER;
}
}
public Class2(String name) {
name_ = name;
}
public static void main(String[] args) {
System.out.print("Your name: ");
Class2 ex2 = new Class2(driver.getScanner().next());
System.out.println("ex2" + ex2.name_);
}
I recommend you follow proper conventions and change your variable/class names.
You could also pass the Scanner to each class via their respective constructors, but I thought you might want to run Class1 or Class2 without running driver first.
Related
I'm trying to instantiate the Scanner using the Singleton design, but when I try to get my main method to instantiate the Scanner, I get an error saying that "Singleton.getInstance() can't be resolved to a type.
package exercise;
import java.util.Scanner;
public class NestedLoop {
public static void main(String[] args) {
stars();
numbs();
}
public static void stars() {
Singleton scanner=new Singleton.getInstance();
int input=scanner.intScan();
for(int row=0;row<input;row++) {
for(int column=0;column<6;column++) {
System.out.print("*");
}
System.out.println();
}
System.out.println(System.identityHashCode(scanner));
}
public static void numbs() {
Singleton scanner=new Singleton.getInstance();
int input=scanner.intScan();
for(int row=0;row<input;row++) {
for(int column=0;column<=row;column++) {
System.out.printf("%d ",column+1);
}
System.out.println();
}
scanner.closeScan();
System.out.println(System.identityHashCode(scanner));
}
}
As for the Singleton class:
package exercise;
import java.util.Scanner;
public class Singleton {
private static Singleton callScan=null;
private Scanner input=new Scanner(System.in);
private Singleton(){};
public static Singleton getInstance() {
if(callScan==null) {
callScan=new Singleton();
}
return callScan;
}
public int intScan() {
int val= input.nextInt();
return val;
}
public void closeScan() {
input.close();
}
}
Kind of stumped! Any help would be greatly appreciated!
Replace
Singleton scanner = new Singleton.getInstance();
with
Singleton scanner = Singleton.getInstance(); // Singleton.getInstance() is already an instance.
Does anyone know why this method will not let me call it. I'm trying to make sure I can call a method with no parameters before I start writing my code. Is there some package or something I need or can someone explain what's going on. Thanks in advance. I'm getting an java: illegal start of expression and red line under () next to validate.
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javamethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
public static void validate() {
System.out.print("Hi World");
}
}
}
The method validate may not be defined inside the main method.
Instead, do it like this:
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
}
public static void validate() {
System.out.print("Hi World");
}
You have your curly brace order mixed up; and in doing so, have declared a new method within the main method.
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javamethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
public static void validate() {
System.out.print("Hi World");
}
}
}
You should declare your methods at the class level.
Change your code to this:
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javaMethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
}
public static void validate() {
System.out.print("Hi World");
}
}
I'm writing a program for my class and i'm getting compiling errors and I'm not sure why. I think it has something to do with my Test method.
public class Test1 {
public static void main(String[] args) {
String test;
public void Test(String s){
text = s;
}
Test test = new Test("ABC");
System.out.println(test);
}
}
public class Test1 {
String text;
public Test1(){
}
public Test1(String s){
this.text = s;
}
public static void main(String[] args) {
Test1 test = new Test1("ABC");
System.out.println(test.text);
}
}
Try this..
Here is example to find how many variables used in my class. But I need to find how many methods are using my variable with in a class.
Test class having four methods but sample1 variable is used in test1() and test3() methods. I want output as test1(),test2() are used sample1 variable
import java.lang.reflect.Field;
public class Test {
private int sample1;
private int sample2;
private int sample3;
public void test1()
{
System.out.println(sample1);
}
public void test2()
{
System.out.println(sample2);
}
public void test3()
{
System.out.println(sample1);
}
public void test4()
{
System.out.println(sample3);
}
public static void main(String[] args) {
Test t = new Test();
Field f[] =Test.class.getDeclaredFields();
for (int i = 0; i < f.length; i++)
{
System.out.println("Variable Name is : " + f[i].getName());
}
}
}
Can this help?
import java.lang.reflect.Field;
import java.util.*;
public class Main {
private int sample1;
private int sample2;
private int sample3;
private ArrayList<String> whoUseSameple1= new ArrayList<String>();
int getSample1(String methodname){
whoUseSameple1.add(methodname);
return sample1;
}
public void test1(){
System.out.println(getSample1(new Object(){}.getClass().getEnclosingMethod().getName()));
}
public void test2(){
System.out.println(sample2);
}
public void test3(){
System.out.println(getSample1(new Object(){}.getClass().getEnclosingMethod().getName()));
}
public void test4(){
System.out.println(sample3);
}
public static void main(String[] args) {
Main t = new Main();
t.test1();
t.test2();
t.test3();
t.test4();
for (String s:t.whoUseSameple1){
System.out.print(s+" ");
}
System.out.print("are usd Sample1 Variable\n");
// Field f[] =Main.class.getDeclaredFields();
// for (int i = 0; i < f.length; i++)
// {
// System.out.println("Variable Name is : " + f[i].getName());
// }
}
}
I have few classes in my project.the main class should me SMSMain.But when i run the project it gives no main class found error.I tried to set in in properties(Netbeans) but it doesn't find any.where am I going wrong?here is my code:
package
na.edu.pon.oop210s.s12012.s211045888.sms;
/**
* Student Number: <211045888>
* Date: 3/11/12 7:47 pM
* Exercise: <Exercise 4>
* Created using: <netbeans>
*/
public class sms {
class Student
{
int studentID;
String studentName,course;
public void setName(String studentName){
this.studentName = studentName;
}
public void setNewId(int studentID){
this.studentID = studentID;
}
public void setCourse(String course){
this.course = course;
}
public String toString(){
return studentID +" "+studentName.toString();
}
class lecturer{
int staffID;
String staffName,taughtCourses;
public void setName(String staffName){
this.staffName = staffName;
}
public void setNewId(int staffID){
this.staffID = staffID;
}
public String toString(){
return staffID +" "+staffName.toString();
}
class course{
String code,description;
double units;
course(String code, String CD){
this.code=code.toUpperCase();
description=CD.toUpperCase();
}
public String getCode(){
return code;
}
/**
* #param args the command line arguments
*/
public class SMSMain {
public void main(String[] args) {
// TODO code application logic here
Student a = new Student();
a.studentName = "Maria";
a.studentID = 1236;
System.out.println("Student Name:" + a.studentName);
System.out.println("Student Name:" + a.studentID);
}
}
}}}}
You need to use
public static void main(String[] args){/* ... */}
with the static keyword.
Also, put your class SMSMain in it's own file SMSMain.java. Then import the needed classes (ctrl-shift-o in Eclipse).
Try to always put a class in it's own file, unless you've got a really good reason not to.
You main class must be static.
Like this:
public static void main(String[] args)
{
// main goes here
}
Try replacing public void main(String[] args) { with public static void main(String[] args) {
Your coding style is confusing. Learn the Oracle/Sun Java coding standards.
The main method has to follow the exact signature and be associated with the public outer class, not one of the inner classes.
package na.edu.pon.oop210s.s12012.s211045888.sms;
/**
* Student Number: <211045888>
* Date: 3/11/12 7:47 pM
* Exercise: <Exercise 4>
* Created using: <netbeans>
*/
// bad naming. What's sms? student management system? sado-masochistic society?
public class sms {
public static void main(String [] args) {
}
}