more java hw

Joined
Nov 26, 2006
Messages
620
Ok so I have a java hw assignment due friday. Im still a real noob with this stuff so I hope you guys can help me out. Basically I have to create a program that prompts (from the command line) the user to input the temperature and based on the temperature it tells you the probable season. So far im stuck at getting the program to prompt the user....I get an "<identifier> expected" error on System.out.print( "Please input the temperature:" ); ....Also Im not sure how to change the string into an int variable...heres what ive got

import java.io.*;

public class Project3Problem50Page271 {

public static void Project3Problem50Page271( String[] args ) throws IOException {
}
private static BufferedReader stdin =
new BufferedReader( new InputStreamReader( System.in ) );
System.out.print("Please input the temperature:");
String temp = stdin.readLine();
}
 
Several issues:
Your compiler error is because your code is not in a method. Java classes contains variables & methods. (hint, your method is currently completely empty).

static variables can't be declared inside a method, so you wouldn't want to just move everything into the method.

and, I think you want to have your method called 'main' instead, so it can be executed from the commandline.

after you do all that it should get the number (as a string!) into temp, so you have the right code, just some of the java organization incorrect.
 
I don't see any reason to make your buffered reader static. Other than that, after you get the string containing the temperature, you want to make it an int.

Google "parsing an integer from a string in java" and you should find something. I think it's something like:

int temperature = Integer.parseInt(temp);

Then, you'll want a couple if statements to check the most likely season. If you want help on your specific error you'll probably need to copy and paste the error exactly. Chances are you're forgetting an import like import java.io;

This is a one time thing you should solve these problems yourself or you'll fall behind.
 
Yeah, as he said, you'll want to keep your class name as it is (named after the file name), but rename your method to main, like
public class Assignment3
{//starts class

public void main(string[] args)\
{ //starts method

blah;
blah;
blah;

} //ends method/program logic


} // ends class
 
Ok its been revised and so far it compiles (even though its far from done). However Im not sure what I have to do to get the "please input temperature" message to display when the program is run..as of right now thats not displayed until I input a number and press enter. Anyway heres what Ive got.

import java.io.*;

public class Project3Problem50Page271 {
public static void main(String[] args) throws IOException {
System.out.print("Please input the temperature:");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String input2 = input.readLine();
int temp = Integer.parseInt(input2);
}



}
 
Also can anyone link me to a good site that explains the structure and syntax used in java. So far the only sites Ive found are way over my head, I think its because they present the material as if the reader already knows a good bit about programming. But that doesn't help me because this java course is my first taste of programming.
 
ok its all but finished, the only thing I need to know now is how to make the program loop. heres the code btw.

import java.io.*;
import java.util.Scanner;
public class Project3Problem50Page271 {
public static void main(String[] args) throws IOException {
System.out.println("Please input the temperature:");
Scanner input = new Scanner(System.in);
int temp = input.nextInt();
if ( temp > 110 )
System.out.println("The temperature entered is outside the valid range");
if ( temp < -5 )
System.out.println("The temperature entered is outside the valid range");
if ( temp >= 90 )
System.out.println("It is most likely Summer");
if ( temp >= 70 && temp < 90 )
System.out.println("It is most likely Spring");
if ( temp >= 50 && temp < 70 ){
System.out.println("It is most likely Fall");
}
else;{
System.out.println("It is most likely Winter");
}
}


}
 
ok so keeping in mind that I have to use at least one if/else statement (as well as if statements), how should I change the above code so that numbers that should display "The temperature entered is outside the valid range" don't also display "It is most likely Winter"
 
Instead of many different If statements, and just the one If..Else, use If..ElseIf

If Summer Range: Say Summer
ElseIf Spring Range: Say Spring
ElseIf Winter Range: Say Winter
ElseIf Autumn Range: Say Autumn
Else Say Out Of Range

This also saves the program from processing each if, for as an example, if it's within the Summer Range, it doesn't have to check Spring, Winter, or Autumn.
 
if (temp >= -5 && temp < 110) {
//Decide what season it is based on previously posted criteria/IF statements
} else {
//Temp is outside of desired range
}


That should do it ;)
 
Ok how about this? This looks like it should work but im getting a ")" expected error on the last if statement how do i correct this?

import java.io.*;
import java.util.Scanner;
public class Project3Problem50Page271 {
public static void main(String[] args) throws IOException {
System.out.println("Please input the temperature:");
Scanner input = new Scanner(System.in);
int temp = input.nextInt();
if ( temp < 110 && temp >= 90 )
System.out.println("It is most likely Summer");
else if ( temp < 90 && temp >= 70 )
System.out.println("It is most likely Spring");
else if ( temp < 70 && temp >= 50 )
System.out.println("It is most likely Fall");

if ( temp < 50 $$ temp > -5 )
{
System.out.println("It is most likely Winter");
}
else;{
System.out.println("The temperature entered is outside the valid range");
}
}

}
 
Code:
import java.io.*;
import java.util.Scanner;

public class Project3Problem50Page271 {
	public static void main(String[] args) throws IOException {
		System.out.println("Please input the temperature:");
		Scanner input = new Scanner(System.in);
		int temp = input.nextInt();
		if (temp < 110 && temp >= 90)
			System.out.println("It is most likely Summer");
		else if (temp < 90 && temp >= 70)
			System.out.println("It is most likely Spring");
		else if (temp < 70 && temp >= 50)
			System.out.println("It is most likely Fall");

		if (temp < 50 $$ temp > -5) {
			System.out.println("It is most likely Winter");
		} else
			;
		{
			System.out.println("The temperature entered is outside the valid range");
		}
	}
}

please use the
HTML:
[CODE] and [/CODE]
tags to surround your code, it will preserve the formatting which makes things easier to diagnose.

And you have a couple problems. $$ isnt a valid boolean operator :) you probably mean &&

and your else statement probably isn't meant to be empty - just a semicolon. remove the semicolon and then the last system.out block will execute instead.

I don't get a ) expected error though.

http://java.sun.com/docs/books/tutorial/ has some nice things on there. I refer to the swing one fairly often. You will want to read through Trail: Getting Started and Trail: Learning the java language i think.

and if you want your code to look pretty, i suggest reading through: Sun: Java Code Conventions
 
NVM its all done and working correctly thanks for all the help, heres the final code.

Code:
import java.io.*; 
import java.util.Scanner;
public class Project3Problem50Page271 {
    public static void main(String[] args) throws IOException {
        while (true) { 
        System.out.println("Please input the temperature:");
Scanner input = new Scanner(System.in);
int temp = input.nextInt();
if ( temp < 110 && temp >= 90 )
    System.out.println("It is most likely Summer");
else if ( temp < 90 && temp >= 70 )
    System.out.println("It is most likely Spring");
else if ( temp < 70 && temp >= 50 )
    System.out.println("It is most likely Fall");
else if ( temp < 50 && temp > -5 )
System.out.println("It is most likely Winter");
else
System.out.println("The temperature entered is outside the valid range");
    }
    
}
}
 
actually it needs a break. if I wanted it to stop when i typed in "end" how would I write the break statement?
 
Indenting / formatting your code correctly makes it a great deal more readable, since you are starting out, i would try and make it look better by hand using the style guide i linked to previously.

Once you have done that for a few programs, come back and ask us about setting up an IDE like eclipse (or netbeans...shudder) if you are permitted to use one for class. They have built in commands for formatting your code, which can make errors like the else; bug much more obvious.
 
Indenting / formatting your code correctly makes it a great deal more readable, since you are starting out, i would try and make it look better by hand using the style guide i linked to previously.

Once you have done that for a few programs, come back and ask us about setting up an IDE like eclipse (or netbeans...shudder) if you are permitted to use one for class. They have built in commands for formatting your code, which can make errors like the else; bug much more obvious.

actually im required to use netbeans 5.5.1
 
I
Once you have done that for a few programs, come back and ask us about setting up an IDE like eclipse (or netbeans...shudder) if you are permitted to use one for class. They have built in commands for formatting your code, which can make errors like the else; bug much more obvious.

+1 for Eclipse. My Java Professor, in fact, recommends that we use Eclipse when doing our Java programs.
 
since you are using scanner to read in int's, it might be easier to take -9999(or something else special) == end, and check for that in an if statement, then break. I'm not familiar enough with scanner to know how you'd accept a string or number.

i personally prefer the
Code:
boolean done = false
while(!done) {
    dostuff();
    done = checkForDone();
}

style instead of using break, where possible.
 
is there a way to create a boolean value that returns a value of false if the scanner input is "end". that way I could do a do-while loop that ends when you type "end"
 
hmm, according to the Scanner javadocs you could do something like (but totally untested):
Code:
boolean done = false;
while(!done){
    if(scanner.hasNextInt()) {
        int myInt = scanner.nextInt();
        ////more stuff
    }else {
        String end = scanner.next();
        if(end.equals("end")){  //must use .equals with strings, instead of ==
            done = true;
        }
    }

}

but you should check if you are required to use break. if you are required to use break, you can just do while(true) and break where i set done = true.
 
hmm, don't use that code. its probably totally broken. You'll need to find a Scanner expert to help you.
 
If you have an infinite loop, you'll have to check if the scanner input was "end". If it was, you'll want a break; to break out of your infinite loop so it'll reach the end of your main method and the program will exit.

also, why use eclipse when you can use intelliJ?
 
hmm, don't use that code. its probably totally broken.
Yeah, a little... :) When scanner.hasNextInt() is called, the Scanner stream is empty (i.e. it occurs before the program waits for input), which will probably throw up an exception.

Just use Scanner.nextLine() to grab the input as a String. You can then easily test if it's "end", and either set your loop condition to false (or break, or System.exit(0), or however you decide to bail out) or try to parse it as an integer with Integer.parseInt().
 
Yeah, a little... :) When scanner.hasNextInt() is called, the Scanner stream is empty (i.e. it occurs before the program waits for input), which will probably throw up an exception.

Just use Scanner.nextLine() to grab the input as a String. You can then easily test if it's "end", and either set your loop condition to false (or break, or System.exit(0), or however you decide to bail out) or try to parse it as an integer with Integer.parseInt().

Seems like there should be a better way to do this with a scanner so you can still use nextInt(), if you just nextLine() its no better than using a BufferedReader.readLine() + Integer.parseInt.
 
i know netbeans has a code formatter! find the menu option and use it :)

CTRL+Shift+F is the shortcut for netbeans 5.5, ALT+Shift+F for netbeans 6.0 (which, confusingly fixes the imports in 5.5)

oh and King_weaver, please surround any if block with accolades, even if its only a single line

Code:
if (condition) {
    doStuff(); // like so
}

this makes the code a lot more readable, and if you choose to add a line, it wont make you fall into the following pit:

Code:
if (condition)
    doThis(); // gets executed only when condition is true
    doThat();  // always gets executed, regardless of the condition
 
Seems like there should be a better way to do this with a scanner so you can still use nextInt()
There is. You can catch the NumberFormatException which comes up when you call nextInt() for a non-numeric string, and then call nextLine() in the catch block (nextInt() doesn't consume the input if it can't be parsed):
Code:
boolean looping = true;
while(looping)
{
    try
    {
        doSomethingWith(input.nextInt());
    }
    catch(NumberFormatException e)
    {
        looping = !input.nextLine().equals("end");
    }
}

But I got the impression that the OP probably hadn't covered exception handling yet, so I didn't mention it.
 
Back
Top