• Post Reply Bookmark Topic Watch Topic
  • New Topic

cmd is not wating for input  RSS feed

 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Argentina{
void Argntna(){
System.out.println("Argentina Welcomes You");
}
}
class Brazil{
void Brazl(){
System.out.println("Brazil Welcomes You!!");
}
}
public class ArgentinaTestDrive{
public static void main(String[] args){
String a="null";
for(int n=0 ; n<args.length- 3 ; n++){
a=args[n];
}
if(a=="Argentina"){
Argentina arg = new Argentina();
arg.Argntna();
}
else if(a=="Brazil"){
Brazil bza = new Brazil();
bza.Brazl();
}
}
}
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IP ??? .... I am assuming that you don't mean the IP used for networking?


BTW, it would probably be better to use the equals() method to compare the strings, as the command line processing create new strings, and hence, are not the same instances of strings in the string pool.

Henry
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks henry ..

here IP is not related to networking.. its INPUT..
oks.. but why not "=="..
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivam singhal wrote:oks.. but why not "=="..


SearchFirst, as this is a very common question.

Henry
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,, can anybody please tell me why this cmd is not wating for input?? why my program terminates before taking input??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because there is no code in the program (not "cmd") that causes input to be accepted. You need to write code that will accept console input if that's what you want to happen.
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using BufferReader ?? i am unable to take input in the form of arrays....
so, how can i take input in the form of arrays..???
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivam singhal wrote:using BufferReader ?? i am unable to take input in the form of arrays....
so, how can i take input in the form of arrays..???


Java gives you access to standard input via System.in -- which should be the console assuming that the operating system hasn't routed it from somewhere else.

Java uses a stream to represent I/O. It also provides some processing, such as the Scanner class to help parse the input.


Anyway, to answer your question, if you want the standard input to be returned as an array, then you have to do it yourself -- it is not built into the core library.

Henry
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah henry..
google also showing the same results .. that for array we have to hard code it!!

BufferReader was just an example... anyway thanks henry for telling..
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you will need to code in to accept input and then place the input into an array. however I don't think you will need an array for what you want to do here?



then you could remove your methods in those classes and just put the System.out.println with your message into the constructor.

Remember Strings are objects, not primitives so to compare them you use stringobject.equals("string") not ==

also why complicate it with an array when it is not needed. you could even just use the command line arguments instead of needing user input etc.

ps this is just coded from my head so can't gaurentee it will work without modification, also if you use it remember your imports.
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Wesleigh..

my aim is not just entering the country name ,, but user must have to enter a 2-digit electric unit used ..
thats why i think to use array..
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivam singhal wrote:hello Wesleigh..

my aim is not just entering the country name ,, but user must have to enter a 2-digit electric unit used ..
thats why i think to use array..


so they will enter a 2 digit int? (sorry what do you mean by electric unit used?) and then the 2 digit int must relate to a country(class)?
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah.. 2 digit int...
in question its mentioning like that" CONSUMED UNITS in electricity bill"

n yeah they are related to country class.. as both the country provide electricity at different cost per unit..
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivam singhal wrote:yeah.. 2 digit int...
in question its mentioning like that" CONSUMED UNITS in electricity bill"

n yeah they are related to country class.. as both the country provide electricity at different cost per unit..


ok so then you can still use my way just convert the input to an int



then use that n an if statement to check for country.

still even though it is checking based on an int I fail to see the need for an array, do you need to store multiple inputs for later use in the program?
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RESOLVED ::

 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivam singhal wrote:RESOLVED :: . . .
Except for the dreadful design error in having classes named Argentina and Brazil. Those are not classes, but are instances of Country. You will have to work out how to choose 50 or 100; you might do that with a switch statement on the country names, but that only works in Java7. You make a Country class (or FootballTeam or whatever) and pass "Brazil", 100 to its constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!