Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: Loops and number programs ZI

  1. #1
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loops and number programs ZI

    public class Eratosten{
     
    	public static void main(String[] args) {
     
    		int m = 20;
    		boolean eratosten = false;
     
    		for (int k=2; k<=m; k++) {
    			eratosten = true;
    			for(int i=2; i<k; i++) {
     
    				if(k%i==0) {
    				eratosten = false;
    				continue;
    				}			
    			}		
    			if(eratosten) {
    				System.out.println(k);
    			}
    		}
    	}
    }
    Last edited by DonnyWatts; May 21st, 2019 at 01:37 AM.

  2. #2
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Game with multiple loops uid

    	public class Game extends JFrame {
    	Timer timer = new Timer();
    	private JPanel contentPane;
     
    	}
     
    	int random = (int) (Math.random() * 4 + 1);
     
    	JTextArea textArea = new JTextArea();
     
    	public void start() {
     
    		TimerTask task = new TimerTask() {
    			int i = 10;
     
    			@Override
    			public void run() {
    				System.out.println(i--);
    				textArea.setText(Integer.toString(i));
    				if (i == 0) {
    					timer.cancel();
    					System.out.println("timer sa zastavil");
    					vybuch();
    				};
    			}
    		};
    		timer.schedule(task, 0, 1000);
     
    	}
     
     
    	public Game() {
     
    		start();
     
     
    		JButton btnRed = new JButton("\u010Cerven\u00E1");
    		btnRed.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent e) {
    				int i = 1;
    				if (random != i) {
    					System.out.println("printbtnRED");
    					timer.cancel();
    					vybuch();
    				} else {
    					timer.cancel();
    					zachrana();
    				}
    			}
    		});
     
    	}
     
    }
    Last edited by DonnyWatts; May 21st, 2019 at 01:39 AM.

  3. #3
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating color chooser pg

    private JPanel contentPane;
    private JTextField winGreen;
    private JTextField winRed;
    private JTextField winBlue;
    private JPanel winColor;
     
    public void ColorChange () 
    	{
     
     
    		String Red = winRed.getText();
    		String Green = winGreen.getText();
    		String Blue = winBlue.getText();
     
    		int R= 0, G= 0, B = 0;
     
    		if(cislo(Red)) {
    			R = Integer.parseInt(winRed.getText());
    		}
    		if(cislo(Green)) {
    			G = Integer.parseInt(winGreen.getText());		
    		}
    		if(cislo(Blue)) {
    			B = Integer.parseInt(winBlue.getText());
    		}
     
     
     
    		Color farba = new Color(R,G,B);
    		winColor.setBackground(farba);
     
    	}
     
    	public static boolean cislo(String color){  
    	  try{  
    	    int i = Integer.parseInt(color);  
    	    return true; 
     
    	  }catch(NumberFormatException nfe){ 
    		System.out.println("Error");
    	    return false;  
    	  }  
     
    	}
     
    	public RGB() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
     
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
     
     
    		winRed = new JTextField();
    		winRed.addKeyListener(new KeyAdapter() {
    			@Override
    			public void keyReleased(KeyEvent arg0) {
    				ColorChange();
    			}
    		});
     
     
    		winRed.setText("0");
    		winRed.setColumns(10);
     
     
     
    		winColor = new JPanel();
     
     
    	}
    	public Color getwinColorBackground() {
    		return winColor.getBackground();
    	}
    	public void setwinColorBackground(Color background) {
    		winColor.setBackground(background);
    	}
    }
    Last edited by DonnyWatts; May 21st, 2019 at 02:04 AM.

  4. #4
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default From decimal to binary

    public static void main(String[] args) {
     
    		int x = 10;
    		String num ="";
     
    		while(x!=0) {
    			num=Integer.toString(x%2)+num;
    			x = x/2;
    		}
    		System.out.print(num);		
    	}	
    }

  5. #5
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default From binary to decimal

    public class FromBin{
     
    	public static int power(int number, int exp){
     
    		int k=1;
    		for(int i=1; i<=exp;i++) {
    			k*=number;	
    		}
    		return(k);
    	}
     
    	public static void main(String[] args) {
     
    		int result= 0;
    		String number= "0100";
     
    		for(int i=0; i<number.length(); i++) {
     
    			if(number.charAt(i)=='1') {
    				result+=power(2,number.length()-1-i);	
    			}
    		}
     
    		System.out.print(result);
    	}
    }

  6. #6
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sum of vowels pz

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.InputStreamReader;
     
    public class Sum{
     
    	public static void main(String[] args) throws Exception {
     
    		File file = new File("c:\\user.txt");
    		BufferedReader reader = null;
    		try {
    			reader = new BufferedReader(new FileReader(file));
    			String text = null;
     
    			int sum = 0;
    			while ((text = reader.readLine()) != null) {
    				text=text.toLowerCase();
    				for(int i=0; i<text.length(); i++) {
    					if(text.charAt(i)=='a'|| text.charAt(i)=='e'|| text.charAt(i)=='i'|| text.charAt(i)=='o'||text.charAt(i)=='u') 
    					{
    						sum++;
    					}
    				}	
    			}
    			System.out.println(sum);
     
    		}catch (Exception e) {
    		}
    	}
    }

  7. #7
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bubble sort simplest sorting algorithm

    public class BubbleSort {
     
    	public static int[] bubblesort(int [] array) {
     
    		for(int i = 0; i < array.length - 1; i++) {
     
    			for (int j = 0; j <array.length - i - 1; j++) {
     
    				if(array[j]<array[j+1]) {
    					int tmp = array[j];
    					array[j] = array[j+1];
    					array[j+1] = tmp;
    				}
    			}
    		}
    		return array;
    	}
     
    	public static void main( String[] args) {
     
    		int[] list = {8,4,7,0,1,5,2,3,9,6};
    		bubblesort(list);
    		for(int k = 0; k < list.length; k++) {
    			System.out.print(list[k]);
    		}
    		System.out.println();
     
    		for(int k = list.length-1; k >= 0; k--) {
    			System.out.print(list[k]);
    		}
    	}
    }
     
     
    //=IF(MOD(SUM(A1:B1);2)=0;"False";"True")

  8. #8
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Computing greatest common divisor - euclids algorithm

    public class EuclidAlg {
     
    	public static int NSD (int a, int b) {
     
    		if(a>b) {
    			return NSD(a-b,b);
    		}
    		if(a<b) {
    			return NSD(a,b-a);
    		}
    		if(a==b) {
    			return a = b;
    		}
    		return 0;
    	}
     
    	public static void main(String[] args) {
    		System.out.print(NSD(15, 25));
    	}
    }

  9. #9
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Text editor TS

    		btnSave.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
    				String text = textArea.getText();	
    				JFileChooser chooser = new JFileChooser();
    				chooser.setCurrentDirectory(new File("/Users"));
    			    int retrival = chooser.showSaveDialog(null);
    			    if (retrival == JFileChooser.APPROVE_OPTION) {
    			    	try {
    						BufferedWriter out = new BufferedWriter(new FileWriter(
    								chooser.getSelectedFile()+".txt"
    								));
    						out.write(text);
    						out.close();
    						} catch(IOException e) {
    						e.printStackTrace();
    						}
    			    }
    			}
    		});
     
     
    		textArea.setFont(textArea.getFont().deriveFont(Font.ITALIC, textArea.getFont().getSize()));
    		textArea.setFont(textArea.getFont().deriveFont(Font.BOLD, textArea.getFont().getSize()));

  10. #10
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Finder and replacer s

    public void mouseClicked(MouseEvent e) {
     
    	String sentence = textArea.getText();
    	String find = textVyhl.getText();
     
    	for(String st : sentence.split(" ")){
    		if(st.indexOf(find)!=-1){
    			System.out.println(st);
    			textArea2.setText(textArea2.getText()+st+'\n');
    		}
    	}
    }

  11. #11
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Caesar cipher

    String word = "Hello";
    String code = "";
     
    for(int i = 0; i < word.length(); i++) {
    	char cipher = (char) (word.charAt(i)+2);
    	code = code+Character.toString(cipher);
    }
     
    System.out.println(code);

  12. #12
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Checkerboard style background

    	 public void paint(Graphics g) {
     
    		for(int j = 0; j<4; j++)
    		{
     
    			 for(int i = 0; i<8; i++)
    			    {
    			    	if(i%2==0) {
    			    		g.setColor(Color.red);
    			    	}else {
    			    		  g.setColor(Color.black);
    					}			
    			    	g.fillOval(i*50+10,50+j*100,50,50);
    			    }
     
    			    for(int i = 0; i<8; i++)
    			    {
    			    	if(i%2==0) {
    			    		g.setColor(Color.black);
    			    	}else {
    			    		  g.setColor(Color.red);
    					}			
    			    	g.fillOval(i*50+10,100+j*100,50,50);
    			    }
    		}
     
     
    	}
     
     
     
    		JFrame colorFrame = new JFrame();
     
    		JPanel colorPane = new JPanel()
    				{
    					@Override
    					public void paintComponent(Graphics g)
    					{
     
    						for(int j = 0; j<2; j++)
    						{
     
    							 for(int i = 0; i<4; i++)
    							    {
    							    	if(i%2==0) {
    							    		g.setColor(Color.red);
    							    	}else {
    							    		  g.setColor(Color.black);
    									}			
    							    	g.fillRect(i*50+10,0+j*100,50,50);
    							    }
     
    							    for(int i = 0; i<4; i++)
    							    {
    							    	if(i%2==0) {
    							    		g.setColor(Color.black);
    							    	}else {
    							    		  g.setColor(Color.red);
    									}			
    							    	g.fillRect(i*50+10,50+j*100,50,50);
    							    }
    						}
     
    					}
    				};
     
    	public ChessTour() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 440, 480);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    		colorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		colorFrame.add(colorPane);
     
    		//colorFrame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
    		colorFrame.setSize(230, 250);
    		colorFrame.setLocation(540, 160);
    		colorFrame.setVisible(true);
     
    	}	
     
     
    }

  13. #13
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multipling with dots and prime number creator I

    		JPanel panel = new JPanel(){
     
    			public void paint(Graphics g) {
     
    				int rad = Integer.parseInt(textA1.getText());
    				int stlp = Integer.parseInt(textA2.getText());
     
    					for(int j = 0; j<rad; j++)
    					{
    						 for(int i = 0; i<stlp; i++)
    						    {
    						    	g.setColor(Color.red);
    						    	g.fillOval(i*40+10,j*40,40,40);
    						    }
    					}
    			}
     
    		};	
     
     
     
    		JButton btnNa = new JButton("N\u00E1sob");
    		btnNa.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
     
     
    				int a = Integer.parseInt(textA1.getText());
    				int b = Integer.parseInt(textA2.getText());	
    				int c = a*b;
     
    				textA3.setText(Integer.toString(c));
    				panel.repaint();
    				repaint();
     
    			}
    		});
     
     
    		JTextArea textAreaRoz = new JTextArea();	
    		JTextArea textAreaVys = new JTextArea();
     
    		JButton btnRozlo = new JButton("Rozlo\u017E");
    		btnRozlo.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
     
    				textAreaVys.setText("");
    				int i = Integer.parseInt(textAreaRoz.getText());
    				String vysledok = "";
    				for(int j = 1; j<=i; j++) {
    					if(i%j==0) {
    						vysledok = Integer.toString(j)+"x"+Integer.toString(i/j)+"\n";
    						textAreaVys.append(vysledok);
    						System.out.print(vysledok);
     
    					}
    				}
     
    			}
    		});

  14. #14
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple arrays with changing colors I

    public class ListColor extends JFrame {
    	Color[] farby =  {Color.RED, Color.YELLOW, Color.BLUE, Color.GRAY, Color.GREEN, Color.ORANGE, Color.BLACK};
     
    	private JPanel contentPane;
    	private JPanel panel;
    	private JPanel panel_1;
    	private JPanel panel_2;
    	private JPanel panel_3;
    	private JPanel panel_4;
    	private JPanel panel_5;
    	private JPanel panel_6;
    	private JButton btnNakresli;
     
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ListColor frame = new ListColor();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	public ListColor() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		JButton btnNakresli = new JButton("Nakresli");
    		btnNakresli.setBounds(298, 129, 89, 23);
    		contentPane.add(btnNakresli);
     
    		JButton btnPosun = new JButton("Posun");
    		btnPosun.setBounds(298, 163, 89, 23);
    		contentPane.add(btnPosun);
     
    		JPanel panel = new JPanel();
    		panel.setBounds(37, 48, 50, 50);
    		contentPane.add(panel);
     
    		JPanel panel_1 = new JPanel();
    		panel_1.setBounds(87, 48, 50, 50);
    		contentPane.add(panel_1);
     
    		JPanel panel_2 = new JPanel();
    		panel_2.setBounds(137, 48, 50, 50);
    		contentPane.add(panel_2);
     
    		JPanel panel_3 = new JPanel();
    		panel_3.setBounds(187, 48, 50, 50);
    		contentPane.add(panel_3);
     
    		JPanel panel_4 = new JPanel();
    		panel_4.setBounds(237, 48, 50, 50);
    		contentPane.add(panel_4);
     
    		JPanel panel_5 = new JPanel();
    		panel_5.setBounds(287, 48, 50, 50);
    		contentPane.add(panel_5);
     
    		JPanel panel_6 = new JPanel();
    		panel_6.setBounds(337, 48, 50, 50);
    		contentPane.add(panel_6);
     
    		btnNakresli.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    					panel.setBackground(farby[0]);
    					panel_1.setBackground(farby[1]);
    					panel_2.setBackground(farby[2]);
    					panel_3.setBackground(farby[3]);
    					panel_4.setBackground(farby[4]);
    					panel_5.setBackground(farby[5]);
    					panel_6.setBackground(farby[6]);
    			}
    		});
    		btnPosun.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				Color temp = farby[6];
    				for (int i = farby.length-1; i>0; i--) {
    					farby[i] = farby [i-1];
    				}
    				farby[0] = temp;
    			}
    		});
    	}
    }

  15. #15
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stone loop resetting integers

    How can I reset both integers me and pc once I have used them?

    	int random = 0; 
    	int me = 0;
    	int pc = 0;
     
    	public void vyber(int typ) {
    		random = (int) (Math.random() * 3 + 0);
    		textArea.setText("");
     
    		if(random==0) {
    			textA2.setText("Kamen");
    		};
    		if(random==1) {
    			textA2.setText("Papier");
    		};
    		if(random==2) {
    			textA2.setText("Noznice");
    		};
     
    		if(typ==0&&random==2) {
    			textArea.setText("Vyhral si");
    			me++;
    		}
    		else if(typ==0&&random==1) {
    			textArea.setText("Prehral si");
    			pc++;
    		}
    		else if(typ==1&&random==0) {
    			textArea.setText("Vyhral si");
    			me++;
    		}
    		else if(typ==1&&random==2){
    			textArea.setText("Prehral si");
    			pc++;
    		}
    		else if(typ==2&&random==1) {
    			textArea.setText("Vyhral si");
    			me++;
    		}
    		else if(typ==2&&random==0) {
    			textArea.setText("Prehral si");
    			pc++;
    		};
     
     
    		if(typ==0&&random==0) {
    			textArea.setText("Remiza");
    			me++;
    			pc++;
    		};
    		if(typ==1&&random==1) {
    			textArea.setText("Remiza");
    			me++;
    			pc++;
    		};
    		if(typ==2&&random==2) {
    			textArea.setText("Remiza");
    			me++;
    			pc++;
    		};
     
    		textScore.setText(Integer.toString(me)+":"+Integer.toString(pc));
     
    		System.out.println(random);	
    		System.out.println(typ);	
    	}
    Last edited by DonnyWatts; May 21st, 2019 at 02:08 AM.

  16. #16
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Text mirroring NKN

    		String thisLine = null;
    		String text = "";
     
    	        try{
    	    		BufferedReader br = new BufferedReader( new FileReader( new File(("c:\\users"))));
    	    		while ((thisLine = br.readLine()) != null) {
    	    			  for(int j = thisLine.length()-1; j>=0; j--) {
    	    				  if(thisLine.charAt(j)=='.') {
    		    				text = text + "\n";
    	    				  }
    	    				  text = text + thisLine.charAt(j);
    	    				  }
    	    		}
    	    		br.close();
    	    		} catch(Exception e) {
    	    		e.printStackTrace();
    	    		}
     
    	        System.out.print(text);
     
    	        JFileChooser chooser = new JFileChooser();
    			chooser.setCurrentDirectory(new File("/Users"));
    		    int retrival = chooser.showSaveDialog(null);
    		    if (retrival == JFileChooser.APPROVE_OPTION) {
    		    	try {
    					BufferedWriter out = new BufferedWriter(new FileWriter(
    							chooser.getSelectedFile()+".txt"));
    					out.write(text);
    					out.close();
    					} catch(IOException e) {
    					e.printStackTrace();
    					}
    		    }

  17. #17
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Text shortening each loop TK

    String thisLine = null;
    		String name= "";
     
            try{
        		BufferedReader br = new BufferedReader( new FileReader( new File(("c:\\users"))));
        		while ((thisLine = br.readLine()) != null) {
        			  for(int j = thisLine.length()-1; j>=0; j--) {
        				  name = thisLine.charAt(j) + name;
        				  }
        		}
        		br.close();
        		} catch(Exception e) {
        		e.printStackTrace();
        		}
     
    		for(int y = 0; y<name.length(); y++) {
     
    			for(int x = 0; x<name.length()-y; x++) {
     
    				System.out.print(name.charAt(x));
     
    			}
    			System.out.println();
    		}

  18. #18
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parentheses checking and testing program

    public class Parentheses extends JFrame {
     
    	private JPanel contentPane;
    	private JTextField textField;
    	private JButton btnOtestuj;
    	private JLabel lblNewLabel;
     
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Parentheses frame = new Parentheses();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	public Parentheses() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		textField = new JTextField();
    		textField.setBounds(39, 37, 279, 35);
    		contentPane.add(textField);
    		textField.setColumns(10);
     
    		JButton btnOtestuj = new JButton("Otestuj");
    		btnOtestuj.setBounds(335, 43, 89, 23);
    		contentPane.add(btnOtestuj);
     
    		JLabel lblNewLabel = new JLabel("");
    		lblNewLabel.setBounds(359, 95, 46, 40);
    		contentPane.add(lblNewLabel);
    		btnOtestuj.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				String text = textField.getText();
    				int pocetO = 0;
    				int pocetZ = 0;
    				for (int i = 0; i < text.length(); i++) {
    					if (text.charAt(i) == '(') {
    						pocetO++;
    					}
    					if (text.charAt(i) == ')') {
    						pocetZ++;
    					}
    					if (pocetO == pocetZ) {
    						lblNewLabel.setText("Ok");
    					}
    					if (pocetO < pocetZ) {
    						lblNewLabel.setText("Chyba");
    						return;
    					}
    				}
    				if (pocetO > pocetZ) {
    					lblNewLabel.setText("Chyba");
    				}
    			};
    		});
    	}
    }
     
    //=IF(MOD(SUM(A1:B1);2)=0;"Mistake";"Correct")

  19. #19
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Temperature sorting and changing system

    public static void main(String[] args) throws Exception {
     
    			BufferedReader br = new BufferedReader(new FileReader(new File("c:\\users")));
     
    			String strLine;
    			int num = 0;
    			float average = 0;
    			int sum = 0;
    			float faren = 0;
     
    			while ((strLine = br.readLine()) != null)   {
     
    			  num = Integer.parseInt(strLine) + num;
    			  sum++;
    			}
    			System.out.println(num);
    			average = (float)num / sum;
    			faren = average* 9/5 + 32;
    			System.out.println ("Temp in C = "+average);
    			System.out.println ("Temp in F = "+ faren);
     
    			br.close();	
    	}
    Last edited by DonnyWatts; May 21st, 2019 at 02:09 AM.

  20. #20
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculating simple to difficult operations

    	private JPanel contentPane;
    	private JTextField textField;
    	float vysledok = 0;
     
    	public Kalkulator() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		JTextPane textHist = new JTextPane();
    		textHist.setText("0");
    		textHist.setBounds(227, 11, 197, 187);
    		contentPane.add(textHist);
     
    		textField = new JTextField();
    		textField.setBounds(10, 11, 207, 38);
    		contentPane.add(textField);
    		textField.setColumns(10);
     
    		JButton buttonP = new JButton("+");
    		buttonP.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				textHist.setText(textHist.getText() + "+" + " " + textField.getText() + " ");
    				vysledok = vysledok + Integer.parseInt(textField.getText());
    				textField.setText("");
    			}
    		});
    		buttonP.setBounds(10, 60, 47, 40);
    		contentPane.add(buttonP);
     
    		JButton buttonM = new JButton("-");
    		buttonM.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				textHist.setText(textHist.getText() + "-" + " " + textField.getText() + " ");
    				vysledok = vysledok - Integer.parseInt(textField.getText());
    				textField.setText("");
    			}
    		});
    		buttonM.setBounds(60, 60, 47, 40);
    		contentPane.add(buttonM);
     
    		JButton buttonK = new JButton("*");
    		buttonK.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textHist.setText(textHist.getText() + "*" + " " + textField.getText() + " ");
    				vysledok = vysledok*Integer.parseInt(textField.getText());
    				textField.setText("");
    			}
    		});
    		buttonK.setBounds(110, 60, 47, 40);
    		contentPane.add(buttonK);
     
    		JButton buttonD = new JButton("/");
    		buttonD.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textHist.setText(textHist.getText() + "/" + " " + textField.getText() + " ");
    				vysledok = vysledok / Integer.parseInt(textField.getText());
    				textField.setText("");
    			}
    		});
    		buttonD.setBounds(160, 60, 47, 40);
    		contentPane.add(buttonD);
     
    		JButton buttonR = new JButton("=");
    		buttonR.setBounds(10, 138, 207, 38);
    		contentPane.add(buttonR);
     
    		JButton buttonC = new JButton("C");
    		buttonC.setBounds(10, 212, 207, 38);
    		contentPane.add(buttonC);
     
    		JTextPane textVysledok = new JTextPane();
    		textVysledok.setBounds(227, 212, 197, 38);
    		contentPane.add(textVysledok);
    		buttonR.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textVysledok.setText(Float.toString(vysledok));
    			}
    		});
    		buttonC.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textField.setText("");
    				textVysledok.setText("");
    				textHist.setText("0");
    				vysledok = 0;
    			}
    		});
    	}
    Last edited by DonnyWatts; May 21st, 2019 at 02:10 AM.

  21. #21
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arranging names and points

    String strLine;
    	JTextArea textArea = new JTextArea();
    	JTextArea textArea2 = new JTextArea();
    	String subor = "";
     
    	public void readData() {
     
    		try {
    		BufferedReader br = new BufferedReader((new FileReader(new File("c:\\users"))));
    			while ((strLine = br.readLine()) != null)   {
    				subor=subor+strLine+"\n";
    				textArea.setText(textArea.getText()+strLine + "\n");
    			}br.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	};
     
    	public void Prepocitat() {
     
    		String [] rozdelenySubor = subor.split("\n");	
     
    		for(int i=0; i<rozdelenySubor.length; i++) {
    			String [] riadok = rozdelenySubor[i].split(" ");
    			System.out.print(rozdelenySubor[i]+"\n");
    			int body = Integer.parseInt(riadok[2]);
    			System.out.print(body+"\n");
     
    			int znamka = 0;
     
    			if(body>=9) {
    				znamka = 1;
     
    			}else if(body>=7) {
    				znamka = 2;
     
    			}else if(body>=5) {
    				znamka = 3;
     
    			}else if(body>=3) {
    				znamka = 4;
     
    			}else if(body>=0) {
    				znamka = 5;
     
    			};
     
    			textArea2.setText(textArea2.getText()+riadok[0]+" "+riadok[1]+" "+znamka + "\n");
     
    		}
    	};
     
    	public StupnicaStudentov() {
     
     
    		readData();

  22. #22
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sentence counter DV

    public class Sum{
     
    	public static void main(String[] args) throws Exception {
     
    		File file = new File("c:\\user.txt");
    		BufferedReader reader = null;
    		try {
    			reader = new BufferedReader(new FileReader(file));
    			String text = null;
     
    			int sum = 0;
    			while ((text = reader.readLine()) != null) {
    				text=text.toLowerCase();
    				for(int i=0; i<text.length(); i++) {
    					if(text.charAt(i)=='a'|| text.charAt(i)=='e'|| text.charAt(i)=='i'|| text.charAt(i)=='o'||text.charAt(i)=='u') 
    					{
    						sum++;
    					}
    				}	
    			}
    			System.out.println(sum);
     
    		}catch (Exception e) {
    		}
    	}
    }

  23. #23
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to get the ASCII value of a character?

    j = Integer.parseInt(DoString);
     
    	for(i = Integer.parseInt(OdString); i<=j; i++) {
    		System.out.println((char)i);
    		textArea.setText(textArea.getText()+(char)i+"\n");
    	}
     
     
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("/Users"));
    int retrival = chooser.showSaveDialog(null);
    if (retrival == JFileChooser.APPROVE_OPTION) {
    	try {
    		BufferedWriter out = new BufferedWriter(new FileWriter(chooser.getSelectedFile()+".txt"));
    		out.write(text);
    		out.close();
    	} catch(IOException e) {
    	e.printStackTrace();}
    	}
    Last edited by DonnyWatts; May 21st, 2019 at 02:11 AM.

  24. #24
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to count the exact time from when you were born

    	JTextArea textAreaDatum = new JTextArea();
    	JTextArea textAreaCas = new JTextArea();
    	JLabel lblAktualny = new JLabel("Aktu\u00E1lny \u010Das a d\u00E1tum");
    	JLabel lblRokov = new JLabel("Rokov:");
    	JLabel lblMesiacov = new JLabel("Mesiacov:");
    	JLabel lblDn = new JLabel("Dn\u00ED:");
    	JLabel lblHodn = new JLabel("Hod\u00EDn:");
    	JLabel lblMint = new JLabel("Min\u00FAt:");
    	JLabel lblSeknd = new JLabel("Sek\u00FAnd:");	
    	JTextArea textAreaRok = new JTextArea();	
    	JTextArea textAreaMes = new JTextArea();
    	JTextArea textAreaDni = new JTextArea();
    	JTextArea textAreaHod = new JTextArea();
    	JTextArea textAreaMin = new JTextArea();
    	JTextArea textAreaSek = new JTextArea();
    	JLabel label = new JLabel("");
     
    	DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
    	Date date = new Date();
    	DateFormat casFormat = new SimpleDateFormat("HH:mm:ss");
    	Date cas = new Date();
    	DateFormat datumFormat = new SimpleDateFormat("yyyy.MM.dd");
    	Date datum = new Date();
     
    	String datumNovy = "";
    	String denNew = "";
    	String test = "";
     
    	String sekundy = "";
    	Double rok = 365.25;
     
    	public void Start() throws ParseException {
     
    		datumNovy = textAreaDatum.getText()+" "+textAreaCas.getText();
     
    		String denFormat = new SimpleDateFormat("EE").format(dateFormat.parse(datumNovy));
    		textAreaDni.setText(denFormat);
     
     
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(dateFormat.parse(datumNovy));
    		int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
     
    		Date dateNovy = dateFormat.parse(datumNovy);
     
    		System.out.println(dateNovy);
    		System.out.println(datumNovy);
    		System.out.println("DEN:"+denFormat);
     
    		System.out.println("DEN V ROKU:" + dayOfYear);
     
    		long epoch = date.getTime();
    		long epochnovy = dateNovy.getTime();
     
    		long vysledok = epoch - epochnovy;
     
    		long sek = vysledok /1000;
    		long min = sek /60;
    		long hod = min / 60;
    		long dni = min / 1440 ;
    		long mes = dni / 30 ;
    		long rok = min / 1440 / 365 ;
     
    		/*
    		LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    		int month = localDate.getMonthValue();
    		int year = localDate.getYear();
    		*/
    		//long longlong = dateNovy.getTimeInMillis();
     
    		System.out.println("Sekundy="+sek);
    		System.out.println("Minuty="+min);
    		System.out.println("Hodiny="+hod);
    		System.out.println("Dni="+dni);
    		System.out.println("Roky="+rok);
     
    		textAreaMes.setText(Long.toString(mes));
    		textAreaRok.setText(Long.toString(rok));
    		textAreaDni.setText(Long.toString(dni));
    		textAreaHod.setText(Long.toString(hod));
    		textAreaMin.setText(Long.toString(min));
    		textAreaSek.setText(Long.toString(sek));
     
    	}
     
    	public DatumNar() {
     
     
    		label.setText(dateFormat.format(date));
     
     
    		textAreaDatum.setText(datumFormat.format(datum));
    		textAreaCas.setText(casFormat.format(cas));
     
    		JButton btnVypotaj = new JButton("Vypo\u010D\u00EDtaj");
    		btnVypotaj.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
     
    				try {
    					Start();
    				} catch (ParseException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		});

  25. #25
    Member
    Join Date
    May 2019
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pyramide from squares

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
     
    public class Pyramida extends JFrame {
     
    	private JPanel contentPane;
     
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Pyramida frame = new Pyramida();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	public Pyramida() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 500, 500);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    		contentPane.add(new DrawCircle());
    	}
     
    }
     
     
    ...
     
    import java.awt.event.MouseListener;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class DrawCircle extends JFrame implements MouseListener { 
     
      private static int x=40;   // leftmost pixel in circle has this x-coordinate
      private static int y=40;   // topmost  pixel in circle has this y-coordinate
      private int c = 1;
      private Color p;
      private Color n;
      public DrawCircle(Color parna, Color neparna) {
        p = parna;
        n = neparna;
    	setSize(800,800);
        setLocation(100,100);
        addMouseListener(this); 
        setVisible(true);
      }
     
      // paint is called automatically when program begins, when window is
      //   refreshed and  when repaint() is invoked 
      public void paint(Graphics g) {
        g.fillOval(x,y,64,64);
        for (int i = 0; i<8; i++) {
        	if (i%2 == 0) {
        	    g.setColor(n);
        	    for (int l = 0; l<8; l++) {;
        		if (c%2 == 0) {
        			g.fillOval(x, y, 64, 64);
            		g.setColor(n);
        		}else {
        			g.fillOval(x, y, 64, 64);
            		g.setColor(p);
        		}
        		c++;
        		x+= 64;
        	}
        	x = 40;
    		y+= 64;
        	}else {
        	    g.setColor(p);
        	for (int l = 0; l<8; l++) {;
        		if (c%2 == 0) {
        			g.fillOval(x, y, 64, 64);
            		g.setColor(p);
        		}else {
        			g.fillOval(x, y, 64, 64);
            		g.setColor(n);
        		}
        		c++;
        		x+= 64;
        	}
        	x = 40;
    		y+= 64;
        }
        }
        y=40;
    }
     
      // The next 4 methods must be defined, but you won't use them.
      public void mouseReleased(MouseEvent e ) { }
      public void mouseEntered(MouseEvent e)   { }
      public void mouseExited(MouseEvent e)    { }
      public void mousePressed(MouseEvent e)   { }
      public void mouseClicked(MouseEvent e)   { }
     
     
     
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Re: Help With Odd/Even number program
    By Bambee in forum Java Theory & Questions
    Replies: 2
    Last Post: August 11th, 2013, 10:36 AM
  2. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  3. Java program inputting month name and outputting month number and number of days
    By Brovahkiin501 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 19th, 2012, 09:30 AM
  4. Help With Odd/Even number program
    By JonoScho in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 23rd, 2009, 10:53 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM