Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Help With Java


  • Please log in to reply

#1
zell_ffhut

zell_ffhut

    Member

  • Member
  • PipPip
  • 34 posts
Hello

Just started a java module at University, and im trying to teach myself some of the basics. But im really having some problems getting to grips with it.

At the moment, im just trying to make a program that reads users inputs, and does things with them. But im getting errors i can't seem to fix!

I have 2 files.


Text.java

/* Based on Text.class in Java Gently by Judy Bishop */
/* Ken Brownsey 10/6/00 */
import java.io.*;
import java.util.*;
public class Text 
{  
	public Text() 
	{  
	} 
	private static StringTokenizer tokens;  
	private static String str;  
	private static void Refresh(BufferedReader in) 
	{	
		try
		{	  
			str = in.readLine();   
		}	catch(Exception e) 
		{	  
			System.out.println("Refresh fail");	
		}	
		tokens = new StringTokenizer(str, "\n");  
		}  
	public static void Prompt (String str) 
	{	System.out.print(str + " ");	
		 System.out.flush();  
	}  
	public static int ReadInt(BufferedReader in) 
	{	
		String item;	if (tokens == null) 
		{	 
			Refresh(in);   
		}  
		for(;true;) 
		{	 
			try 
			{  
				item = tokens.nextToken();	   
				return Integer.valueOf(item.trim()).intValue();	
			}	  
			catch(NoSuchElementException e1)
			{	  
				Refresh(in);	
			}	 
			catch(NumberFormatException e2) 
			{	 
				System.out.println("Number format exception - try again");	  
			}  
		}  
	} 
	public static char ReadChar(BufferedReader in) 
	{   
		if (tokens == null)
		{	 
			Refresh(in);   
		} 
		for(;true;)
		{	
			try
			{   
				return tokens.nextToken().charAt(0);   
			}	
			catch(NoSuchElementException e1)
			{	
				Refresh(in);   
			}   
		} 
	}  
	public static double ReadDouble(BufferedReader in) 
	{   
		String item;  
		if (tokens == null)
		{	
			Refresh(in); 
		}  
		for(;true;)
		{	
			try 
			{   
				item = tokens.nextToken();
				return Double.valueOf(item.trim()).doubleValue();
			}	 
			catch(NoSuchElementException e1)
			{	 
				Refresh(in);
			}	
			catch(NumberFormatException e2)
			{	   
				System.out.println("Number format exception - try again");  
			}   
		} 
	}  
	public static String ReadString(BufferedReader in)
	{   
		String item;  
		if (tokens == null) 
		{	
			Refresh(in); 
		}  
		for(;true;) 
		{   
			try 
			{	
				return tokens.nextToken();  
			}	
			catch(NoSuchElementException e1) 
			{	
				Refresh(in);   
			}   
		} 
	}
}

Thats the code for getting the text from the input etc.. Was not made by me, but was supplied by the university

And my little bit of code....

/*
 * Hello World.java
 *
 * Created on 06 June 2005, 09:39
 *
 */

package helloworld;

/*
 *Created by Christopher Murray
 */

import java.io.*;
public class Main 
{
	static private BufferedReader in =
		new BufferedReader(new InputStreamReader(System.in));

	public static void main(String[] args) 
	{
		double amount;
		
		System.out.println("How Much for the Monkey?");
		amount = Text.ReadDouble(in); 
		amount = amount +25.00;
		
		System.out.print("We will Bill $");
		System.out.print(amount);
		System.out.println("To your Credit Card");
		System.out.println("Have a good day!");

	}
	   
}

The error is on the line "amount = Text.ReadDouble(in);"

Claims it Can't find Symbol. And the symbol is the vairable "Text"


But as far as i can tell from all my lecture notes, it should be correct. :-/

Any ideas?
  • 0

Advertisements


#2
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Nevermind. I sorted it :tazz:
  • 0

#3
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Glad you got it working.

We can make some improvements to your main class though.
public static void main(String[] args) {
	System.out.println("How Much for the Monkey?");
	double amount = Text.ReadDouble(in) + 25.0;
	
	System.out.print("We will Bill $" + amount + " To your Credit Card");
	System.out.println("Have a good day!");
}
You'll see I did all your computations with amount on one line. I also used the + operator to concatenate amount onto your strings. I could have also done:
System.out.print("We will Bill $" + amount + " To your Credit Card\nHave a good day!");
But that just looks ugly.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP