storing input for multiple methods

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 716 Views - Last Post: 07 July 2013 - 12:20 AM Rate Topic: -----

#1 javanooblit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 13

storing input for multiple methods

Posted 06 July 2013 - 10:13 PM

import java.util.Scanner; 
    
  public class BonczkedNSLE51 
  { 
   private Scanner input = new Scanner( System.in ); 
          
        
          
      private String cityName = ""; 
      private String stateName = ""; 
      private double statePopulation = 0.0; 
      private double cityStatePopulation = 0.0; 
      private double cityPercState = ( statePopulation / cityStatePopulation * 100 ); 
    //application starting point 
    public static void main( String[] args ) 
    { 
        
    } 
    public static void cityName() 
    {   
       System.out.printf( "What is the name of your city" );
      [b] cityName = input.nextLine;[/b]
       
      
    }                       
        
    public static void stateName() 
    { 
        
    } 
    
    public static void cityStatePopulation()
    {
      
    }
    
    public static void cityPercState()
    {
      
    }  
      
          
    
  }  


Hi everyone im new here, and new to java. I just started my java 1 course this summer and we are moving fast. For this lab we are calling multiple methods. So what im trying to do now is store the variables in the different methods. Right now the only error im getting is in bold and reads cityName = input.nextLine;. Im trying to figure out how to get the input for that method (and the others as well). The error i get reads "

2 errors and 4 warnings found:
--------------
*** Errors ***
--------------
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 22]
Error: Cannot make a static reference to the non-static field cityName
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 22]
Error: Cannot make a static reference to the non-static field input
--------------
** Warnings **
--------------
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 5]
Warning: The field BonczkedNSLE51.input is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 9]
Warning: The field BonczkedNSLE51.cityName is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 10]
Warning: The field BonczkedNSLE51.stateName is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 13]
Warning: The field BonczkedNSLE51.cityPercState is never read locally
"

sample output looks like this:
what is your city? san antonio

what is your stat? tx

enter the population of san antonio: 1382951

enter the population of tx: 26059203

POPULATION STATISTICS

city:san antonio
stat: tx
city population: 1,382,951
state population: 26,059,203
city to state population: 5.31%




Can anyone help me out?

Is This A Good Question/Topic? 0
  • +

Replies To: storing input for multiple methods

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10644
  • View blog
  • Posts: 39,515
  • Joined: 27-December 08

Re: storing input for multiple methods

Posted 06 July 2013 - 10:17 PM

Moved to Java. Note that Java and Javascript are two very different languages.

Your errors are very explicit- you cannot access non-static variables in static methods. So either declare the variables as static or adopt a more OO convention.

Also, I'd avoid methods simply to get input for a single variable. It makes for clunky and brittle code.
Was This Post Helpful? 0
  • +
  • -

#3 javanooblit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 13

Re: storing input for multiple methods

Posted 06 July 2013 - 10:45 PM

ok so i changed all the variables at top to private static and i get this error.

import java.util.Scanner; 
    
  public class BonczkedNSLE51 
  { 
   private Scanner input = new Scanner( System.in ); 
          
        
          
      private static String cityName = ""; 
      private static String stateName = ""; 
      private static double statePopulation = 0.0; 
      private static double cityStatePopulation = 0.0; 
      private static double cityPercState = ( statePopulation / cityStatePopulation * 100 ); 
    //application starting point 
    public static void main( String[] args ) 
    { 
      
    } 
    public static void cityName() 
    {   
       System.out.printf( "What is the name of your city" );
       
        cityName = input.nextLine; 
      
    }                       
        
    public static void stateName() 
    { 
        
    } 
    
    public static void cityStatePopulation()
    {
      
    }
    
    public static void cityPercState()
    {
      
    }  
      
          
    
  }  

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10644
  • View blog
  • Posts: 39,515
  • Joined: 27-December 08

Re: storing input for multiple methods

Posted 06 July 2013 - 10:48 PM

See here input.nextLine;. Remember that nextLine() is a method. Thus, you must use the parentheses to denote this. The syntax you are using input.nextLine; tells the compiler that you are expecting nextLine to be a variable belonging to the Scanner object.
Was This Post Helpful? 0
  • +
  • -

#5 javanooblit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 13

Re: storing input for multiple methods

Posted 06 July 2013 - 10:48 PM

1 error and 4 warnings found:
-------------
*** Error ***
-------------
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 23]
Error: Cannot make a static reference to the non-static field input
--------------
** Warnings **
--------------
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 5]
Warning: The field BonczkedNSLE51.input is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 9]
Warning: The field BonczkedNSLE51.cityName is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 10]
Warning: The field BonczkedNSLE51.stateName is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 13]
Warning: The field BonczkedNSLE51.cityPercState is never read locally
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10644
  • View blog
  • Posts: 39,515
  • Joined: 27-December 08

Re: storing input for multiple methods

Posted 06 July 2013 - 10:48 PM

Is input static? ;)
Was This Post Helpful? 0
  • +
  • -

#7 javanooblit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 13

Re: storing input for multiple methods

Posted 06 July 2013 - 10:57 PM

I think i remember someone telling me to put some kind of input under the scanner is that where you are going?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10644
  • View blog
  • Posts: 39,515
  • Joined: 27-December 08

Re: storing input for multiple methods

Posted 06 July 2013 - 10:59 PM

Your question doesn't make sense. I'm telling you to make your input variable static like you did your other variables.
Was This Post Helpful? 0
  • +
  • -

#9 javanooblit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 13

Re: storing input for multiple methods

Posted 06 July 2013 - 11:04 PM

ok bear with me, ty very much btw for this help. I now get this error.

1 error found:
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 22]
Error: Syntax error on token "cityName", VariableDeclaratorId expected after this token
Was This Post Helpful? 0
  • +
  • -

#10 javanooblit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 13

Re: storing input for multiple methods

Posted 06 July 2013 - 11:10 PM

import java.util.Scanner; 
    
  public class BonczkedNSLE51 
  { 
   private Scanner input = new Scanner( System.in ); 
          
        
          
      private static String cityName = ""; 
      private static String stateName = ""; 
      private static double statePopulation = 0.0; 
      private static double cityStatePopulation = 0.0; 
      private static double cityPercState = ( statePopulation / cityStatePopulation * 100 ); 
      
    public static void main( String[] args ) 
    { 
      
    } 
    public static void cityName() 
    {   
       System.out.printf( "What is the name of your city" );
       private static cityName = input.nextLine();
         
      
    }                       
        
    public static void stateName() 
    { 
        
    } 
    
    public static void cityStatePopulation()
    {
      
    }
    
    public static void cityPercState()
    {
      
    }  
      
          
    
  }  

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10644
  • View blog
  • Posts: 39,515
  • Joined: 27-December 08

Re: storing input for multiple methods

Posted 06 July 2013 - 11:39 PM

Don't re-declare the variable in the method. Get rid of the private static modifier on the variable in the method.
Was This Post Helpful? 0
  • +
  • -

#12 javanooblit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 13

Re: storing input for multiple methods

Posted 06 July 2013 - 11:46 PM

import java.util.Scanner; 
    
  public class BonczkedNSLE51 
  { 
   private Scanner input = new Scanner( System.in ); 
          
        
          
      private static String cityName = ""; 
      private static String stateName = ""; 
      private static double statePopulation = 0.0; 
      private static double cityStatePopulation = 0.0; 
      private static double cityPercState = ( statePopulation / cityStatePopulation * 100 ); 
      
    public static void main( String[] args ) 
    { 
      
    } 
    public static void cityName() 
    {   
       System.out.printf( "What is the name of your city" );
       cityName = input.nextLine();
         
      
    }                       
        
    public static void stateName() 
    { 
        
    } 
    
    public static void cityStatePopulation()
    {
      
    }
    
    public static void cityPercState()
    {
      
    }  
      
          
    
  }  


so like that i get the same error again

1 error and 4 warnings found:
-------------
*** Error ***
-------------
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 17]
Error: Cannot make a static reference to the non-static field input
--------------
** Warnings **
--------------
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 5]
Warning: The field BonczkedNSLE51.input is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 9]
Warning: The field BonczkedNSLE51.cityName is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 10]
Warning: The field BonczkedNSLE51.stateName is never read locally
File: C:\Users\SJB Sony\Desktop\JAVA\BonczkedNSLE51.java [line: 13]
Warning: The field BonczkedNSLE51.cityPercState is never read locally
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10644
  • View blog
  • Posts: 39,515
  • Joined: 27-December 08

Re: storing input for multiple methods

Posted 06 July 2013 - 11:55 PM

Are you even reading my posts? I've already answered this question twice now.
Was This Post Helpful? 0
  • +
  • -

#14 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: storing input for multiple methods

Posted 06 July 2013 - 11:55 PM

I'm sorry to have to be the one to inform you, but if every method in your Java program is static, you're doing it wrong.

Start by removing it from the static context. We can do this by creating an instance of your class. Begin with this.
public class BonczkedNSLE51 { //I suggest making a clearer name for this class...

	public static void main(String[] args) {
		BonczkedNSLE51 b = new BonczkedNSLE51(); //your instance is now called b
	}
...more code



Your static variables needn't be static any more. You can use instance variables from now on. Let's put them in there.

public class BonczkedNSLE51 {

	private Scanner input = new Scanner( System.in ); 
	private String cityName = ""; 
	private String stateName = ""; 
	private double cityPopulation = 0.0; //your population can't have half people, a dwarf is still a whole person.
	private double statePopulation = 0.0; //so let's make those ints
	//private double cityPercState = ( statePopulation / cityPopulation * 100 );
		//We don't do this because currently: statePopulation is 0, cityStatePopulation is also 0, 0*100 is also 0.
		//Dividing by 0 causes bad things to happen like black holes 'n' s#!+..
	private double cityPercState = 0.0;

	public static void main(String[] args) {
		BonczkedNSLE51 b = new BonczkedNSLE51();
	}
...more code



Now that we've got an instance of your class, and it's full of workable data containers called fields, we can create methods that manipulate the data in those fields. Let's add those now.
public class BonczkedNSLE51 {

	private Scanner input = new Scanner( System.in ); 
	private String cityName = ""; 
	private String stateName = ""; 
	private int cityPopulation = 0; 
	private int statePopulation = 0;
	private int cityPercState = 0;

	public static void main(String[] args) {
		BonczkedNSLE51 b = new BonczkedNSLE51();
	}
	
	private void promptCityName() {
		System.out.println("What is the name of your city?");
		cityName = input.nextLine();
	}
	
	private void promptStateName() {
		System.out.println("What is the name of the state?");
		stateName = input.nextLine();
	}
	
	private void promptCityStatePopulation() {
		System.out.println("What is the population of the city?");
		cityPopulation = input.nextLine();
		System.out.println("What is the population of the state?");
		statePopulation = input.nextLine();
	}
	
	private void calculateCityPercState() {
		//you can calculate this here, as long as the previous method has been called first.
	}
}



Now that's set up it's time to actually call the methods. Call them using the instance we created earlier in the main method.

public class BonczkedNSLE51 {

	private Scanner input = new Scanner( System.in ); 
	private String cityName = ""; 
	private String stateName = ""; 
	private int cityPopulation = 0; 
	private int statePopulation = 0;
	private int cityPercState = 0;

	public static void main(String[] args) {
		BonczkedNSLE51 b = new BonczkedNSLE51();
		//call the methods here:
		b.promptCityName();
		b.promptStateName();
		b.promptCityStatePopulation();
		b.calculateCityPercState();
	}
	
	private void promptCityName() {
		System.out.println("What is the name of your city?");
		cityName = input.nextLine();
	}
	
	private void promptStateName() {
		System.out.println("What is the name of the state?");
		stateName = input.nextLine();
	}
	
	private void promptCityStatePopulation() {
		System.out.println("What is the population of the city?");
		cityPopulation = input.nextLine();
		System.out.println("What is the population of the state?");
		statePopulation = input.nextLine();
	}
	
	private void calculateCityPercState() {
		cityPercState = (cityPopulation / statePopulation) * 100
	}
}



This is now a program ready to take further to meet your needs. I warn you, if you copy this without understanding it, you will learn nothing.

Hope this helps.

This post has been edited by Flukeshot: 07 July 2013 - 12:22 AM
Reason for edit:: Oversight

Was This Post Helpful? 1
  • +
  • -

#15 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: storing input for multiple methods

Posted 06 July 2013 - 11:58 PM

Edit: nevermind.. i can't read tonight,

Flukeshot, why are you initializing integers at a value of 0.0? lol

This post has been edited by schutzzz: 07 July 2013 - 12:02 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2