[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Whats wrong with this code?

Whats wrong with this code?

import java.awt.*;

 public class text
 {
  String input;
  public static void main(String args[])
  {
   input = args[1];
   System.out.println(input);
   System.exit(0);
  }
 }

I get this message:
text.java:8: Can't make a static reference to nonstatic variable input in class text.
input = args[1];

I am want to read a line from what the app is started and print it.
like
C:\java text line to print
line to print
C:\
Thanks for any help!
0
dvd99
Asked:
dvd99
1 Solution
 
TheImmortalCommented:
You must declare your String input as:

Static String input;

Since you are accessing a Global String from a static method, the string must also be static. Your code sthould look like this:

import java.awt.*;

 public class text
 {
  static String input;
  public static void main(String args[])
  {
   input = args[1];
   System.out.println(input);
   System.exit(0);
  }
 }

Remember to pass in two arguments to your code since you have args[1] specified. The args array is Zero based (starts at 0) and increments from there.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now