tong1 22 Posting Whiz

According to NormR1's suggestion,
(1) Add one space character as an extra delimiter into the sDelim string in line 8:
String sDelim = ".? !";
(2)Insert the following line of code after line 14
if (s1.compareTo("do")!=0)

so that the "do" will not be printed on DOS

tong1 22 Posting Whiz

The following code is an example demonstrating what I am thinking about. I have tested positively. You may write code differently to read the file one by one. I hope you may understand me.

import java.io.*;// read several input files as indicated via the arguments of main method
class XML{
  public static void main(String args[]){
    int length;
    byte buf[]=new byte[1024];
    
    for (int i=0; i< args.length ;i++ )  // each loop read one file. 
    try{
      FileInputStream in;      
      in =new FileInputStream(args[i]);  // establish the input channel for read each file
      while((length=in.read(buf,0,1024))!=-1) // read one line of character
      System.out.println(new String(buf));    // print out the line of character on DOS
    }catch(IOException e){
      System.out.println("Error: "+e);
      System.exit(-1);
    }
  }
}

Under the same folder, for example, you may have several files to be read: 1.txt, 2.txt, 3.txt.
You may compile the program, and then run the program with the following command lines on DOS

javac XML.java
java XML 1.txt 2.txt 3.txt

The program thus prints the contents of all the files on DOS window accordingly.

tong1 22 Posting Whiz

You may write a program to open and read several files whose names are passed via the String args[] array as the arguments of the main methods. The first argument tells the program how many files to be proceesed, and the following arguments represent the names of every files. For example:
java XML 4 input1.txt input2.txt input3.txt input4.txt
provided the files are located under the same folder

tong1 22 Posting Whiz

The data type of choice is int. So you should define each case by a constant of type int. Therefore the case bady should be:

case 1:
	howKilometers(meters);
	break;
case 2 :
	showInches(meters);
	break;
case 3 :
	showFeet(meters);
	break;
case 4 :
	System.exit(0);
	default:  JOptionPane.showMessageDialog(null, "Invalid number");
tong1 22 Posting Whiz

May I suggest you write a program to open and read a file, and the file name is the first String argument ( args[0]) for the main method :
public static void main(String args[]) {...}
so that each time your command should simply be :
java XML input1.txt

tong1 22 Posting Whiz

If it is the case (3), in the method body the argument n (the instance of Node) rather than "jemz" should be used for comparison. The content of the data member of the node n could be any string. You are searching for the node n in the list which has nothing to do with "jemz". So there are some misunderstandings in your method body. If I were you I would choose (2) which may make more sense.
If you understand me could you please write a search method based on the choice (2)?

tong1 22 Posting Whiz

Are you going to search for a specific string "jemz" in a list? If so, your seach method should be declared as follows:
(1) public Boolean search(String s);
where the argument s will contain a string, such as "jemz". If there is a node where the data member value is "jemz" in the list, the method returns true. If not it returns false.
Or
(2) public Node search(String s); if there is a node where the data member value is "jemz" then the method returns that node.

(3) Your declaration is :
public Boolean search(Node n); which means this method is going to search for a specific Node.

Depending on different situations you have to choose only one from the above 3 declaration.

tong1 22 Posting Whiz

Should we first establish the input channel properly?
(1) In line 20 the "input= " is missing.
The corrected code would be:
input = JOptionPane.showInputDialog("Enter the next number");
(2) lines 15, 16 are deleted.
(3) before comming into the while loop the variable number should be initiated, e.g., as 0.
(4) try to print out each input. Test if the channel is established successfully or nor.
Then you may consider how to get max/min values among the series of input values.

tong1 22 Posting Whiz

jemz, when removing a node from a list one has to specify which node is going to be deleted. For example, if you are going to delete a node where the data member value is a specific string, e.g. "wonderful". You may have to do a search for the specific string in the list. In this case the signature of the method should be:
Node remove(String s) {...} rather than Node remove(Node n){....} unless you just want to delete a specific node n.
Anyway, I write the following two methods to delete 1. the head or 2. the tail node for your reference. Provided that Node definition is:

class Node{
String value;
Node next;
}
public Node removeFirst(){// delete the head node of the list
	if (isEmpty()) // if the list is empty
	return null;   // return null, i.e. no node can be returned
	else {  // if the list is not empty
	Node p = head; // make node p to represent the head node
	head = head.next; // after delete the head the next node of the original head will be the new head
	return p;  // return the previous head represented by p
	}
}
	
public Node removeLast(){ // delete the tail of the list
	if (isEmpty()) // if the list is empty
	return null;   // return no node
	else { // if the list is not empty
	Node p = head; // make p to represent the original head
	while(p.next !=tail) //when …
tong1 22 Posting Whiz

jemz, I have comments on your program:

(1) In your program, the declaration of some methods should be changed.
For example, public Node addFirst(Node n) // in line 26
It does not make sense if you have to return a node after successfully inserting the new node.So, when you are going to insert a new node n into the list the return type should altered with boolean or void.

After 37 line you should insert a code:
Size++; .// since you have inserted a new code before the head.

tong1 22 Posting Whiz

skyzer, Is the n an instance of Node or that of String?
I think some things in jemz declare of the method "remove" should be altered as :
public boolean remove(String n) {...}
provided the definition of the class Node is wirtten as :

class Node {
String item;
Node next;
}
tong1 22 Posting Whiz

Not at all. Please make sure you mark the thread as solved if your queries have been answered.

tong1 22 Posting Whiz

toferdagofer, As I said before, within constructor you re-define/re-declare the label2,label4,label6, and label8 as local variable of the type JTextField, which is wrong. Why? While you re-declare and initiate them as local variables, the attributes of the same names, as you declared for the class, are in fact not initiated. Therefore, the run time error message : “java.lang.NullPointerException” is thrown when executing the line of code:

adultPrice = label2.getText() ; //(line 99)

because the attribute label2 is null.
I also have a comment on the naming of variables: label2,…label8, which are in fact TextField’s instances. I would name them as textfield1,... textfield4 so that their names are meaningful.

tong1 22 Posting Whiz

In constructor you redefine the label2,label4,label6, and label8 as local variable of the type JTextField, which is wrong. You have already declared them as attributes.

You should thus replace the 8 lines of code in the constructor by the following code:

JLabel label1 = new JLabel("Enter the price of one adult ticket : ");
label2 = new JTextField(10);
JLabel  label3 = new JLabel("Enter the number of adult tickets sold : ");
label4 = new JTextField(10);
JLabel label5 = new JLabel("Enter the price of one child ticket : ");
label6 = new JTextField(10);
JLabel label7 = new JLabel("Enter the number of child tickets sold : ");
label8 = new JTextField(10);
tong1 22 Posting Whiz

Thank you, JavaAddict. The code you have modified above is definitely much better than previous code. I would like to put a comment on how to write a new line in a file.
My code:

bw.write(name + (char)10); // Add the ASCII code 10 for the character new line

works partially. After execution I have opened the output file “write.txt” via notepad where the new line character doesn’t work (it is shown as an unrecognized symbol). It works only in the case of opening the file by MS Word.
So one should use the way as JavaAddict writes:

bw.newLine(); // for changing line when writing to a file.
tong1 22 Posting Whiz

One thing you have to remember:
If you have defined some non-default constructors, the compiler will not implicitly provide a default constructor. If you need a default one you'd better define one. Otherwise it wouldn't pass compiling. For example, the following code leads to an error message for line 10:unsolved symbol

import java.io.*;

public class DefaultConstructor {

    public DefaultConstructor(String s) {
    	System.out.println("I am Not Default with no arguments. " + s );
    	}      

    public static void main(String[] args) {
        DefaultConstructor d = new DefaultConstructor();
    }
}
prem2 commented: Nice Example +1
tong1 22 Posting Whiz

Default constructor is the constructor with no arguments requested. It is called implicitly when creating an instance.

import java.io.*;

public class DefaultConstructor {
    
    public DefaultConstructor() {
    	System.out.println("I am Default with no arguments.");
    	}      

    public static void main(String[] args) {
        DefaultConstructor d = new DefaultConstructor();
    }
}

output:
I am Default with no arguments.

tong1 22 Posting Whiz

jemz, it works correctly. However, it works only for one line input. IOException would be more concrete/precise than Exception. I have modified your code so that client may type in several lines of strings. Type in "end" to finish typing.

import java.io.*;
class Writefile{		
  public static void main(String []args)throws IOException{
String name;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	BufferedWriter bw = new BufferedWriter(new FileWriter("write.txt"));  	
	System.out.println("Write into the file write.txt. To finish typing input \"end\".");
	while(true){ 			//loop continues unless typing in the "end" line
	name = br.readLine(); 	// read each line of strings you typed in
	if (name.equals("end")) // check the content of input. If it is "end" 
	break;   				// then jump out of the while loop
	bw.write(name + (char)10); // Add the ASCII code 10 for the character new line
	}
	bw.close(); 		
     }
 }
tong1 22 Posting Whiz

"Node" is just a class name you have given. It is not a reserved word.

tong1 22 Posting Whiz

Thank you , jon, for your quick response and correction. Should we call it "Same Type of Reference" or "Self-Type Reference" rather than self- referencing ?
A linked list contains several Node's objects, which are connected linearly (one by another) via the attribute "next". The "next" is a reference to its next Node' instance. The type of the "next" is also Node: itself Type. Therefore, they are of not only the same type but also itself type (the type as same as itself). The relationship for linked list is one to one.

class Node{
String value; //data member
Node next;    // self-Type reference, Node type reference
}

For the Linkedlist class we may call it as self-referencial class
http://www.cplusplus.com/forum/beginner/3311/

tong1 22 Posting Whiz
class Node {  // The definition of the class Node
String value; // data member
Node next;  // self referencing: referring to itself
}

Self-reference occurs in natural or formal languages when a sentence or formula refers to itself.
http://en.wikipedia.org/wiki/Self-reference

tong1 22 Posting Whiz

I added a main method so that the program runs.

public static void main(String args[]){
   	Display dis=new Display();
   	dis.setVisible(true);
   	dis.setSize(500,150);
         dis.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
   }

I put an icon.png in the same folder.
both the program and the button works. The problem is that the icon will be located before the text rather than after the text.
So the question has become: How to make icon inserted after the text? I think, adams161 has quoted the answer:"....the icon is effectively inserted at the current position of the caret. ". So "before inserting, put the cursor after the text" is the solution.

The output is shown in the image: icon.gif

tong1 22 Posting Whiz

It works in my machine. For example, you put the Main.java in the folder "forum"
Via DOS window do the following cammands:

E:\forum>javac Main.java

E:\forum>java Main
Hello everyone on the forum DaniWeb
end

so that a text file "MyFile.txt" is created where the "Hello everyone on the forum DaniWeb " is stored.

tong1 22 Posting Whiz

code line 17 is not correct:
BufferedReader br = new BufferedReader(new BufferedWriter((System.in)));
The argument should be a Writer object.

I have modified the program by Java source code example for your reference.
http://www.javadb.com/write-to-file-using-bufferedwriter
The data you typed in will be stored in myFile.txt
In one line type in "end" only to terminate the program.

import java.io.*;
import java.util.Scanner;

public class Main {
    
    /**
     * Type in some data to a file using a BufferedWriter via DOS
     */
    public void writeToFile() {
        
        BufferedWriter bufferedWriter = null;
        BufferedReader br=null;
        Scanner in = new Scanner(System.in);       
        try {            
          //Construct the BufferedWriter object
            bufferedWriter = new BufferedWriter(new FileWriter("MyFile.txt"));
            while(true){
            	String s = in.nextLine();
            	if (s.equals("end"))
            	break;
            	bufferedWriter.write(s);
            }     
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedWriter
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    

    public static void main(String[] args) {
        new Main().writeToFile();
    }
}
tong1 22 Posting Whiz

Thank you for your "Why". I found I sent you a wrong version. In line code 11 if you put:

FileDialog fd = new FileDialog(null , "Open a document", FileDialog.LOAD);

you will have the error message: "the constructor file dialog is ambiguous"
However, the line of code 11:

FileDialog fd = new FileDialog(this, "Open a document", FileDialog.LOAD);

will pass compiling. Therefore, we should emphasize:
For the first argument "Frame parent" of the FileDialog constroctor one should use "this" indicating the parent Frame is the current object.

tong1 22 Posting Whiz

ezkonekgal ,thank you for your reply. I have checked and tested your code.
(1) You use JFileChooser which seems better than FileDialog which I use currently (because, as you reported, eclipse complains about the constructor I wrote).
(2) May I suggest you delete the "OK" button which is useless.
(3) Your code runs correctly. I have replaced your LayoutManager by FlowLayout so that components show up properly.
Attached please find the modified code. It runs correctly in my machine.

tong1 22 Posting Whiz

You have incorrectly placed these lines of code outside of methods. This means that you are declaring attributes for your class. When you declare an array, you may initiate the String array with the corresponding values at the same time. However you wrote assignments' code during declaration, which is wrong.
String[] greeting = new String[4]; // declare an attribute of STring array. It's good.
greeting[0] = "Why, Hello there!"; // Wrong. Assignment shoud be written within a method.
greeting[1] = "Welcome.";//wrong
greeting[2] = "blah blah blah";// wrong
greeting[3] = "more useless crap";// wrong

You may write one line of code to complete the task.

String[] greeting = {"Why, Hello there!","Welcome.","blah blah blah","more useless crap"};
tong1 22 Posting Whiz

Your code shown above is correct. I have tested with a known file. The result is correct. AS long as the browsing files is concerned, as jon.kiparsky indicated, it is already available in Swing. For example, the class FileDialog is a candidate for this purpose. Since its constructor requests a parent frame , as written in Java API, the class you define has to inherit JFrame. I have modified your code as follows. You have to modify your main method accordingly. Attached please the code altered for your reference.

import java.io.*;
import java.awt.*;
import javax.swing.*;
class FileRead extends JFrame {
	static File name;  // it is created in constructor, then used in main
	static int count =0; // count the number of lines where characters are found
	static int count2=0; // count the nubmer of empty lines
	
public 	FileRead(){  // constructor for FileRead
	try{
      FileDialog fd = new FileDialog(this, "Open a document", FileDialog.LOAD); 
      fd.setDirectory(System.getProperty("user.dir")); // the window starts with current folder
      fd.setVisible(true);	
   	  name= new File(fd.getDirectory(), fd.getFile());
   	 }catch(Exception e){
   	 	System.err.println(e.getMessage());
   	 	}
   	setSize(400,400);
   	setVisible(true);
   	 }

Good luck.

tong1 22 Posting Whiz

Yes. In super class you may define a method to invoke the method of the subclass so that you may call the method with the super class reference.

class Superclass{
void display(){
System.out.println("Super");
}

void Sub2Display(){  // the extra method you define to call the method of its subclass
	Sub2 sub2= new Sub2();
	sub2.display();
	}
}

class Sub1 extends Superclass{
void display(){
System.out.println("sub class1");
}
}
class Sub2 extends Sub1{
void display(){
System.out.println("Sub class2");
}

void display2(){
System.out.println("Second method");
}
}


public class reference {
public static void main(String[] args){
Superclass ref=new Sub1();
ref.display();
ref.Sub2Display(); //invoke the method of the subclass with the super class reference
//((Sub2)ref).display();
}
}
tong1 22 Posting Whiz

(1) The buttons show up in my machine (JDK 1.6). So it seems to be some problem with your machine. You may use Button of AWT rather than JButton of swing which is a lightweight component and does not show up among any AWT stuff. However, your case is OK. Since you put all the buttons into one separated container so that no conflict occurs, and even JButton shoud show up. If you simply put a JButton into a canvas (e.g. Applet) it wouldn't show up.
(2) You did not set up Layout manager correctly. I modified your code with BorderLayout. It works. (See attached file)
(3) Should we call the class ClickyLatinHelp as a driver class rather then "main class" ? One class may implement several interfaces. Since ActionListener and MouseListener both are interfaces the driver class may implement both.
I am noticed that :
Your code:

if (showeng = true) {
				showeng = false;
			}else {
				showeng = true;
			}
		}

is not correct. It should be if (showeng == true)... rather than if (showeng=true)

tong1 22 Posting Whiz

ankilosado, I have a question for you:
Why do you declare the class statItem has an attribute valor of type of Integer instead of the primitive type int? Integer is the wrapper class of int. Am I right?

tong1 22 Posting Whiz

You may replace the code in line 24 where the error occurs by the following two lines of code:

int va = this.statVect[i].getValor();
     va += otroVect[i].getValor();
     this.statVect[i].set(va);

Can you answer me Why we should modify in this way?

javaAddict commented: Allowing the OP to think +6
tong1 22 Posting Whiz

It works with one Thread. You may add a button (not JButton) so that when client clicks, one more ball will be added. One should be noticed that the JButton, which is lightweight component, will not be shown on the heavyweight stuff( e.g. awt Graphics). That's why only Button(the heavyweight component) works. You may also setLayout(null), so that the button may use setBounds(int, int, int, int) method to have its own location and size. Then you may start a coding for a game..... Good luck.

/*
Only one Thread is used. You may have more balls up to 10.
*/

import javax.swing.*;
import java.awt.*;


public class Balls extends JFrame implements Runnable {
	
	static Ball balls[]= new Ball[10];
    static int count=0;
    Thread timer = null;
    
    class Ball {  // inner class 
    	int x,y;  // each ball has its own location indicated by x,y
    	boolean flag;  // each ball has its own flag to direct if y++ or y--
    	
    	public Ball(int x){
    		flag=true;
    		y=0;
    		this.x=x;
    	}
    	
    	public void drawball(Graphics g){ // draw itself
    		g.setColor(Color.red);
    		g.fillOval(x,y,10,10);
    		if (flag)
    		y++;
    		else
    		y--;
    		if (y >= getHeight())  // inner class may call any method of its outer class
    		flag = false;
    		else
    		if (y <=0)
    		flag = true;
    	}
    } // end of the inner class
    
    public void start(){
    	if (timer == null){
    		timer = new Thread(this);
    		timer.start();
    	}
    }
    public void run(){
    	while(timer !=null){
    		try{
    			Thread.sleep(10);
    		}catch(InterruptedException e){}
    		repaint();
    	}
    }
    
    public void paint(Graphics g){
    	g.setColor(Color.white);
    	g.fillRect(0,0,800,600);
    	for (int i=0; i<count; i++){ // …
tong1 22 Posting Whiz

You may try to use one Thread only in your program.
Your class drawpanel will have attributes x,y only. That is, you define each ball as an instance with their own attributes x and y.

tong1 22 Posting Whiz

Thank you, NormR1. I have tested your code. Yes, it works when the BorderLayout is used swhere two balls are moving in different "subframes":BorderLayout.EAST and BorderLayout.WEST. I have tried FlowLayout, resulted int an overlap of two components. In a game program, two objects (balls) should be moving within one frame rather than two: EAST and WEST. Therefore, we have to figure out the right Layout Manager. Meanwhile programming in J2ME shows different balls are able to move within a common frame. Are they associated with one thread or different threads? We have to make further investigation.

tong1 22 Posting Whiz

I have implemented Xyfuan's latter arrays using two methods: static void init(int a[][]) to assign initial values to each elements and static void printArray(int a[][]) to print a 2D array. it works. Please check if there are any improper remarks/comments.
The output:
100 90 80 70
60 50 40
30 20
10
100
90 80
70 60 50
40 30 20 10

The code:

public class array{
     static void printArray(int a[][]){
     for ( int i=0; i<a.length; i++ ){
       for ( int j=0; j<a[i].length; j++ )
       System.out.print (a[i][j] + " ");
       System.out.println ();
     		} 
     }
     static void init(int a[][]){
     int k=100;
     for (int i=0; i<a.length; i++ )
       for (int j=0; j<a[i].length; j++ ) {     
       a[i][j] = k;
       k = k-10;
     }	
     }

public static void main (String [] args){

 	int [][] TwoDArray1; // declare 2D array TwoDArray1
 	int [][] TwoDArray2; // declare 2D array TwoDArray2
 	TwoDArray1 = new int [4][]; // allocate spaces for 4 rows, i.e. declare 4 1D arrays
	TwoDArray2 = new int [4][]; // allocate spaces for 4 rows, i.e. declare 4 1D arrays
   	for (int i=0; i<4; i++){
  	TwoDArray1[i]= new int[4-i]; // allocate spaces for the 2D array TwoDArray1
   	TwoDArray2[i]= new int[i+1]; // allocate spaces for the 2D array TwoDArray2
   	}
   	
	init(TwoDArray1); // assign initial values to every elements of the 2D array TwoDArray2
	init(TwoDArray2); // assign initial values to every elements of the 2D array TwoDArray1
   	printArray(TwoDArray1); // print all elemens in format of rows and columns 
   	printArray(TwoDArray2); // print all elemens in format of rows and columns 
  }
}
tong1 22 Posting Whiz

(1) Different from C/C++,in Java the following array declaration and allocating space is not allowed:
int arrey [3][3];
(2) the following way to have your arrey is valid:
int arrey[][]; //declaration of a 2-D array
arrey = new int[3][3]; //creates corresponding space for the array arrey with no values assigned.
Then you have to write code to assign values to its elements:

int value=1;
for (int i=0; i<arrey.length; i++)
for (int j=0; j<arrey[i].length;j++)
arrey[i][i]=value++;

(3) Another way to have your arrey: in Java you may declare a array, allocate space, and assign values to it at the same time ( in one line of code), like:
int arrey[][]= {(1,2,3},{4,5,6},{7,8,9}}; //declare, allocate, and assign initial values

tong1 22 Posting Whiz

In line 1 the condiction for while loop answer.equalsIgnoreCase("Y") repeats once.
Why?

tong1 22 Posting Whiz

glamourhits,
as an exercise, you may create more JOptionPane window by the end of the while loop to ask cleint: " Would you like to enter another Student Grade " after each run.
Also you may use "break" instead of System.exit(0) to terminate the loop. Try different code.

tong1 22 Posting Whiz

Replace line 40 by the following code:

name=JOptionPane.showInputDialog("Enter Student's full Name:\nClick Cancel to terminate program");
if (name==null) {
	JOptionPane.showMessageDialog(null,"Bey for now.","Welcome to DANIWEB", JOptionPane.INFORMATION_MESSAGE);
	System.exit(0);
}

This is to set up a guard so that if client clicks cancel button, the program may terminate.

Replace your main method with the following method where the while loop seems endless. This is what jon.kiparsky said: " to use one of these structures to make this run forever ". However, the above guard may terminate the program by calling System.exit(0). Therefore, the while loop in fact does not run forever.

public static void main(String[] args)
{
 	Student s = new Student();
 	while(true){
	s.inputGrades();
	System.out.println("Average --> " + s.getAverage());
	System.out.println("Letter grade --> " + s.letterGrade());
	System.out.println("s.toString() --> " + s.toString());
	}
}
tong1 22 Posting Whiz

It could be:
int arrey[][]= {{1,2,3},{4,5,6},{7,8,9}};
May I suggest you write a method : void printArray(int a[][]); like the following code by red color to print any 2-D array.
When you create a two-demension ladder form in opposite way, some of the minor changes should be made. The following code works fine:

public class array{
public static void main (String [] args){
 int i, j, k = 100;
 int [][] TwoDArray;
 TwoDArray = new int [4][];
   TwoDArray [0] = new int [4];
   TwoDArray [1] = new int [3];
   TwoDArray [2] = new int [2];
   TwoDArray [3] = new int [1];

  for ( i=0; i<TwoDArray.length; i++ )
     for ( j=0; j<TwoDArray[i].length; j++ ) {     
     TwoDArray [i][j] = k;
     	k = k-10;
     }
   for ( i=0; i<TwoDArray.length; i++ )
     {
     for ( j=0; j<TwoDArray[i].length; j++ )
     System.out.print (TwoDArray [i][j] + " ");
     System.out.println ("");
     } 
  }
}
tong1 22 Posting Whiz

Or, The 40 line should be

answer = myScanner.next();

Then the program works. Remember, you should use the method next() instead of readLine() in this case.

tong1 22 Posting Whiz

May I suggest in terms of your first post:
(1) replace line 10 by

while (answer.compareToIgnoreCase("Y")==0){

since tne comparison" answer == "Y" is the conpare references (addresses) of two instances.
(2) remove line 26 which seems useless
(3) line 40 should be:

answer = myScanner.nextLine();

the L in the method named "nextLine()" should be upper case.
I put one more line code after line 40:
answer = myScanner.nextLine();
( to "eat" the character '\n'???)
It works.

tong1 22 Posting Whiz

Your program can not be compiled successfully.
Where is your declaration for average,jgrade...
the code in line 26 is a dead loop if (average < 101)
The last curly brace is redundant that should be removed.

tong1 22 Posting Whiz

Should we delete the break; in the line 29
since the

default:
valid=false;
break;

is the last case.

tong1 22 Posting Whiz

Thank you for the comments.
I was wrong. It should be:
total += prices;

tong1 22 Posting Whiz

should be
total += prices;

tong1 22 Posting Whiz

jon, thank you for interpretation and clarification you made.

tong1 22 Posting Whiz

Dear NewOrder, jon's post is very important which tells you how to ask question precisely. Please read the web site jon indicates.
I explain how String Snumber="" works.
Each time when you declare String Snumber=""; you will have a new instance of String with no character. Then for example, after executing Snumber += "a"; the Snumber instance will change location to store "a". You should know the Sting instance is a constant which can not become larger like StringBuffer. If you want Snumber to have a 'a', you have to find a new location for Snumber. The String instances may work well with '+=' while StringBuffer instances shoud work with its method append(String s). You have placed the String Snumber =""; into the outerloop so that it becomes the fist line of the loop body. So each time when starting a new loop, a new instance of Snumber is created so that it will get ride of previous contents and store the next digit's string. Snumber changes location after each execution of '+=".
jon, am I right.
P.S. If you want to let some object, such as an instance of String Snumber, dead, you may use the code:
Snumber=null;
so that the garbage collector will return the Snumber to operation system.

tong1 22 Posting Whiz

jon and jemz, I have just completed the program using StringBuffer method: reverse() to check palindrome. My attention is to show the usefullness of StringBuffer

import java.io.*;
import javax.swing.JOptionPane;
 public class Jemz1{   


 static boolean palindromeCouplet (String s)   {
 	StringBuffer s1 = new StringBuffer(s);
 return ((s.compareTo(new String(s1.reverse()))==0) ? true :false)  ;
 	}	

 public static void main(String[] args) {	
 	while(true){
    String str=JOptionPane.showInputDialog( null,  
	"Type in a word!\nPress cancel to ternimate。",
	"IS A PALINROME",
	JOptionPane.QUESTION_MESSAGE);    
    
	if (str == null) {
	System.out.println("Bey for now");
    	break;
    	} 
JOptionPane.showMessageDialog( null,  
	"Palindrome: " + palindromeCouplet(str),
	str + " IS A PALIDROME? ",
	JOptionPane.INFORMATION_MESSAGE);  
          }
      }   
}