The Artima Developer Community
Sponsored Link

Java Answers Forum
New to Java , Please Help

14 replies on 1 page. Most recent reply: Apr 2, 2005 12:51 PM by Antony

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 14 replies on 1 page
Antony

Posts: 8
Nickname: antz
Registered: Mar, 2005

New to Java , Please Help Posted: Mar 27, 2005 12:14 PM
Reply to this message Reply
Advertisement
I have made a program for an assignment i have but it has problems can you have alook at it please and tell me what i am doing wrong? Thank You

/*Program which reads in 10 numbers and adds them together,
if the user inputs negative number it is ignored and the running total
is not updated*/

import java.io.*;
class q3{

public static void class q3(String[] args) {
throws IOException
{
int num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,sum;
/*decalare and intialise variables*/

{
System.out.println("input a number greater then zero");
num1=Course_io.readlnt();}
{
System.out.println("input a number greater then zero");
num2=Course_io.readlnt();}
{
System.out.println("input a number greater then zero");
num3=Course_io.readlnt();}

System.out.println("input a number greater then zero");
num4=Course_io.readlnt();

System.out.println("input a number greater then zero");
num5 = Course_io.readlnt();

System.out.println("input a number greater then zero");
num6=Course_io.readlnt();

System.out.println("input a number greater then zero");
num7=Course_io.readlnt();

System.out.println("input a number greater then zero");
num8=Course_io.readlnt();

System.out.println("input a number greater then zero");
num9=Course_io.readlnt();

System.out.println("input a number greater then zero");
num10 = course_io.readlnt();
}while(num1<0) (num2<0) (num3<0) (num4<0) (num5<0) (num6<0) (num7<0) (num8<0) (num9<0) (num10<0)
{
System.out.println("You entered a negative number")
}
else
{
sum = num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;
System.out.println("The answer:"+sum);
};
}
}


Antony

Posts: 8
Nickname: antz
Registered: Mar, 2005

Re: New to Java , Please Help Posted: Mar 27, 2005 12:28 PM
Reply to this message Reply
Changed the code to this:

/*Program which reads in 10 numbers and adds them together,
if the user inputs negative number it is ignored and the running total
is not updated*/

import java.io.*; //tell the compiler where to find IOException
class q3{ //name of program

public static void main(String[] args) {
throws IOException
{
int num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,sum;
/*decalare and intialise variables*/

{
System.out.println("input a number greater then zero");
num1=Course_io.readlnt();}
{
System.out.println("input a number greater then zero");
num2=Course_io.readlnt();}
{
System.out.println("input a number greater then zero");
num3=Course_io.readlnt();}

System.out.println("input a number greater then zero");
num4=Course_io.readlnt();

System.out.println("input a number greater then zero");
num5 = Course_io.readlnt();

System.out.println("input a number greater then zero");
num6=Course_io.readlnt();

System.out.println("input a number greater then zero");
num7=Course_io.readlnt();

System.out.println("input a number greater then zero");
num8=Course_io.readlnt();

System.out.println("input a number greater then zero");
num9=Course_io.readlnt();

System.out.println("input a number greater then zero");
num10 = course_io.readlnt();
}while(num1<0) (num2<0) (num3<0) (num4<0) (num5<0) (num6<0) (num7<0) (num8<0) (num9<0) (num10<0)
{
System.out.println("You entered a negative number")
}
else
{
sum = num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;
System.out.println("The answer:"+sum);
};
}
}

The problem is:

javac -d . -g -nowarn "C:\Java Programs\q3.java"

C:\Java Programs\q3.java:9: illegal start of expression
throws IOException
^
1 error

Thank you

Raj

Posts: 1
Nickname: knraj
Registered: Mar, 2005

Re: New to Java , Please Help Posted: Mar 28, 2005 1:23 AM
Reply to this message Reply
change this line

public static void main(String[] args) {
throws IOException
{

to

public static void main(String[] args) throws IOException
{

Antony

Posts: 8
Nickname: antz
Registered: Mar, 2005

Re: New to Java , Please Help Posted: Mar 28, 2005 2:10 PM
Reply to this message Reply
Thank you but then these errors appear:( What am i doing wrong?



javac -d . -g -nowarn "C:\Java Programs\q3.java"

C:\Java Programs\q3.java:44: not a statement
}while(num1<0)(num2<0)(num3<0)(num4<0)(num5<0)(num6<0)(num7&l t;0)(num8<0)(num9<0)(num10<0)
^
C:\Java Programs\q3.java:44: ';' expected
}while(num1<0)(num2<0)(num3<0)(num4<0)(num5<0)(num6<0)(num7&l t;0)(num8<0)(num9<0)(num10<0)
^
C:\Java Programs\q3.java:16: cannot resolve symbol
symbol : variable Course_io
location: class q3
num1=Course_io.readlnt();}
^
C:\Java Programs\q3.java:19: cannot resolve symbol
symbol : variable Course_io
location: class q3
num2=Course_io.readlnt();}
^
C:\Java Programs\q3.java:22: cannot resolve symbol
symbol : variable Course_io
location: class q3
num3=Course_io.readlnt();}
^
C:\Java Programs\q3.java:25: cannot resolve symbol
symbol : variable Course_io
location: class q3
num4=Course_io.readlnt();
^
C:\Java Programs\q3.java:28: cannot resolve symbol
symbol : variable Course_io
location: class q3
num5 = Course_io.readlnt();
^
C:\Java Programs\q3.java:31: cannot resolve symbol
symbol : variable Course_io
location: class q3
num6=Course_io.readlnt();
^
C:\Java Programs\q3.java:34: cannot resolve symbol
symbol : variable Course_io
location: class q3
num7=Course_io.readlnt();
^
C:\Java Programs\q3.java:37: cannot resolve symbol
symbol : variable Course_io
location: class q3
num8=Course_io.readlnt();
^
C:\Java Programs\q3.java:40: cannot resolve symbol
symbol : variable Course_io
location: class q3
num9=Course_io.readlnt();
^
C:\Java Programs\q3.java:43: cannot resolve symbol
symbol : variable course_io
location: class q3
num10 = course_io.readlnt();
^
C:\Java Programs\q3.java:44: cannot resolve symbol
symbol : variable num1
location: class q3
}while(num1<0)(num2<0)(num3<0)(num4<0)(num5<0)(num6<0)(num7&l t;0)(num8<0)(num9<0)(num10<0)
^
13 errors

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: New to Java , Please Help Posted: Mar 28, 2005 11:39 PM
Reply to this message Reply
You have ab bit of a mess with your brackets.

1. If you want to use while you need a start point.
{ //this one is missing at the beginning of the loop.
 ...
}while ();


2. while (b1)(b2)(b3)(b4) does not exist (at least as far as I know).
Use while (() || () || () || ()); or
while ((b1) || (b2) || (b3) || (b4)); instead, in your case:
 while(num1<0 || num2<0 || num3<0 || num4<0 || num5<0 || num6<0 || num<0 || num8<0 || num9<0 || num10<0);

while checks only one value, so you must connect all conditions to form one single condition.

3. It is not neccessary to use brackets here:
{
  System.out.println("input a number greater then zero");
  num1=Course_io.readlnt();
}


That would only make sense if you would do some sort of error handling here.
I suppose this is where your first while error comes from.
use:
{
  System.out ...
  num1 = ...
  System.out ...
  num2 = ...
} while (num1 < 0 || num2 < 0 || ...);

Antony

Posts: 8
Nickname: antz
Registered: Mar, 2005

Re: New to Java , Please Help Posted: Mar 29, 2005 3:38 PM
Reply to this message Reply
Thanks Alot! But i still don't understand why it isn't intialising the variables?

I'm having this problem with all the programs i have made.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: New to Java , Please Help Posted: Mar 29, 2005 10:29 PM
Reply to this message Reply
in the case of a primitive type it's really annoying to get the "might not have been initialized" message, because every numeric primitive type variable is 0 by default.

But regarding your example, I don't know what you mean.
I have no problem compiling this code:
public static void main (String[] args) {
  int num;
  do {
    num = 6;
  } while (num < 5);
  if (num == 6)
    System.out.println ("No compilation error");
}


Post your new code and the error message.
But before you post:
Use the Java - html tag:

"[ java ]"
... code
"[ /java ]"
Just remove the empty spaces in the tag.
Click on "Preview" to see if it worked.

Antony

Posts: 8
Nickname: antz
Registered: Mar, 2005

Re: New to Java , Please Help Posted: Mar 30, 2005 4:04 PM
Reply to this message Reply
/*Program which reads in 10 numbers and adds them together,
if the user inputs negative number it is ignored and the running total
is not updated*/
import java.io.*; //tell the compiler where to find IOException
class q3 {//name of program
    public static void main(String[] args) throws IOException
    {
    int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum;/*decalare and intialise variables*/
 
 
            	do{System.out.println("input a number greater then zero");
    	      num1=Course_io.readlnt;
                   System.out.println("input a number greater then zero");
                  num2=Course_io.readlnt();
                System.out.println("input a number greater then zero");
                  num3=Course_io.readlnt();
    	      System.out.println("input a number greater then zero");
    	      num4=Course_io.readlnt();
    	      System.out.println("input a number greater then zero");
    	      num5 = Course_io.readlnt();
    	      System.out.println("input a number greater then zero");
    	      num6=Course_io.readlnt();
    	      System.out.println("input a number greater then zero");
    	      num7=Course_io.readlnt();
    	      System.out.println("input a number greater then zero");
    	      num8=Course_io.readlnt();
    	      System.out.println("input a number greater then zero");
    	      num9=Course_io.readlnt();
    	      System.out.println("input a number greater then zero");
    	      num10 = course_io.readlnt();
                  }
               while(num1<0 || num2<0 || num3<0 || num4<0 || num5<0 || num6<0 || num7<0 || num8<0 || num9<0 || num10<0);
 
                   	System.out.println("You entered a negative number");
 
                   if(num1>0 || num2>0 || num3>0 || num4>0 || num5>0 || num6>0 || num7>0 || num8>0 || num9>0 || num10>0);
 
                   	sum = num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;
                           System.out.println("The answer:"+sum);
                           }
   }


still 10 errors with the Course_io :
javac   -d . -g -nowarn "C:\Java Programs\q3.java" 
 
C:\Java Programs\q3.java:12: cannot resolve symbol
symbol  : variable Course_io 
location: class q3
    	      num1=Course_io.readlnt;
                   ^
C:\Java Programs\q3.java:14: cannot resolve symbol
symbol  : variable Course_io
location: class q3
                  num2=Course_io.readlnt();
                       ^
C:\Java Programs\q3.java:16: cannot resolve symbol
symbol  : variable Course_io 
location: class q3
                  num3=Course_io.readlnt();
                       ^
C:\Java Programs\q3.java:18: cannot resolve symbol
symbol  : variable Course_io 
location: class q3
    	      num4=Course_io.readlnt();
                   ^
C:\Java Programs\q3.java:20: cannot resolve symbol
symbol  : variable Course_io 
location: class q3
    	      num5 = Course_io.readlnt();
                     ^
C:\Java Programs\q3.java:22: cannot resolve symbol
symbol  : variable Course_io 
location: class q3
    	      num6=Course_io.readlnt();
                   ^
C:\Java Programs\q3.java:24: cannot resolve symbol
symbol  : variable Course_io 
location: class q3
    	      num7=Course_io.readlnt();
                   ^
C:\Java Programs\q3.java:26: cannot resolve symbol
symbol  : variable Course_io 
location: class q3
    	      num8=Course_io.readlnt();
                   ^
C:\Java Programs\q3.java:28: cannot resolve symbol
symbol  : variable Course_io 
location: class q3
    	      num9=Course_io.readlnt();
                   ^
C:\Java Programs\q3.java:30: cannot resolve symbol
symbol  : variable course_io 
location: class q3
    	      num10 = course_io.readlnt();
                      ^
10 errors

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: New to Java , Please Help Posted: Mar 31, 2005 12:48 AM
Reply to this message Reply
Obviously the compiler can't find Course_io.

What is it? A class you wrote?
I suppose the read readlnt is static, otherwise you would have to create an instance of Course_io and use that instance instead.

Solution: You must tell the compiler where to find the Course_io class.
If it's in another package, you have 2 possibilities:
1. at the beginning of the file, write:
import packagename.Course_io;

2. Instead of:
num1 = Course_io.readlnt();

write
num1 = packagename.Course_io.readlnt();


Note: Sometimes you wrote course_io and sometimes Course_io. Wich one is right?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: New to Java , Please Help Posted: Mar 31, 2005 8:36 AM
Reply to this message Reply
Unless the intent is to get typing practice, it seems like you'd want something more like this:
/*
 * Created on Mar 31, 2005
 * @author Matt Gerrans
 */
public class Numbskull
{
   private static void println(String s) { System.out.println(s); }
   private static void print(String s) { System.out.print(s); }
   public static void main(String[] args)
   {
      java.io.BufferedReader input = new java.io.BufferedReader( new java.io.InputStreamReader(System.in) );
      long sum = 0;
      int numCount = 0;
      while( numCount < 10 )
      {
         println("Input a number that's greater then zero.");
         print( (numCount+1) + ". ");
         try
         {
            try
            {
               int read = Integer.parseInt( input.readLine() );
               if( read < 0 )
                  println("Sorry, no negative numbers, please.");
               else
               {               
                  numCount++;
                  sum += read;
               }
            }
            catch( java.lang.NumberFormatException nfe )
            {
               println("Please enter an integer value.");
            }
         }
         catch( java.io.IOException ioe )
         {
            println("Ack!  I can't read that junk!");
         }
      }
      println("The sum of your numbers is " + sum + "." );
   }
}

But maybe the instructor's intent is to have you first do it "the hard way" and then show you how much easier it could be in the next lesson...

Antony

Posts: 8
Nickname: antz
Registered: Mar, 2005

Re: New to Java , Please Help Posted: Apr 1, 2005 2:17 PM
Reply to this message Reply
Wow Thankz everyone as helped me so much, thank u all, I still have the course_io problem though :( guess i'll figure it out eventually.

!!THANKZ!!

kishan

Posts: 1
Nickname: bala12
Registered: Apr, 2005

Re: New to Java , Please Help Posted: Apr 1, 2005 5:59 PM
Reply to this message Reply
hi

In your code what is Course_io.readlnt()?.

what Course_io ?? where you have declared??

there is no declaration for Course_io in your code.

Antony

Posts: 8
Nickname: antz
Registered: Mar, 2005

Re: New to Java , Please Help Posted: Apr 2, 2005 11:30 AM
Reply to this message Reply
how would i declare that then?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: New to Java , Please Help Posted: Apr 2, 2005 12:14 PM
Reply to this message Reply
Did your instructor give you some package or class that contains course_io? If you don't know what it is, why is it in your code? How did it get there? If you're just using a copy of someone else's code, ask that person where it came from. Maybe part of the assignment is to write the "Course_io" class?

Antony

Posts: 8
Nickname: antz
Registered: Mar, 2005

Re: New to Java , Please Help Posted: Apr 2, 2005 12:51 PM
Reply to this message Reply
My tutor told me that you type Course_io.readlnt(); to read in a variable, also in some of the sample codes it has this coding, I haven't got a package for this code and in the sample coding it doesn't show where it declares it.

I am going to have a look through my disks i might have missed one that has the packages on, but i doubt it.

Thank you for all of your help

Flat View: This topic has 14 replies on 1 page
Topic: Java program icons Previous Topic   Next Topic Topic: Access VSS database from Linux, Unix, Apple Mac and others?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use