Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Programming Help
New Posts  All Forums:Forum Nav:

Java Programming Help - Page 3

post #21 of 26
Quote:
Originally Posted by wire;15559174 
That's actually a very good idea lol. So in java, is T different from t?

Yeah, if you're comparing strings/chars, yeah. Same goes for C++ and whatnot.
Skylake
(10 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core-i7 6700K ASUS ROG Maximus VIII Hero EVGA GTX 970 4GB G.SKILL Ripjaws 4 32GB DDR4 2800 
Hard DriveHard DriveCoolingOS
SAMSUNG SM951 M.2 SAMSUNG 850 EVO Corsair H100i Windows 10 
PowerCase
SeaSonic G-750 Fractal Design Define R4 
  hide details  
Reply
Skylake
(10 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core-i7 6700K ASUS ROG Maximus VIII Hero EVGA GTX 970 4GB G.SKILL Ripjaws 4 32GB DDR4 2800 
Hard DriveHard DriveCoolingOS
SAMSUNG SM951 M.2 SAMSUNG 850 EVO Corsair H100i Windows 10 
PowerCase
SeaSonic G-750 Fractal Design Define R4 
  hide details  
Reply
post #22 of 26
Quote:
Originally Posted by wire;15558928 
Okay so I have a new question. I can't quite come up with the logic for doing this next part of the program.

Lets say I have String dna = "acgtacgtacgtttaacgttt"

What I want to do is replace all "a" with "t", all "t" with "a", all "g" with "c", and all "c" with "g". I don't think I can use the replace method with this one.



*UPDATED*
Code:
public String replace(String one){

char [] chars = one.toCharArray();

for(int i = 0; i < one.length(); ++i){
        if(one.charAt(i) == 'a')
           chars[i] = 't'; //replace a with t here

        else if(one.charAt(i) == 't')
                 chars[i] = 'a'; //replace t with a here

        else if(one.charAt(i) == 'g')
                 chars[i] = 'c'; //replace g with c here

        else
             chars[i] = 'g'; //replace c with g here
}

one = chars.toString();

return one;
}

Edited by surfbumb - 11/3/11 at 4:43pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #23 of 26
i'm too lazy to format the code...but it is all there...let me know if it works.

class dna(main)
Code:
import java.util.Scanner;
public class dna
{

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.print("Please enter a DNA sequence ");
String dna = in.nextLine();

//Performs a check to test the validity of the inputed DNA sequence
int i = 0;
while (i < dna.length())
{
char ch = dna.charAt(i);

if (ch == 'a' || ch=='c' || ch=='g' || ch=='t')
{
i++;
}
else
{
System.out.print("Invalid DNA sequence. Please enter a DNA sequence ");
dna = in.nextLine();
} 
}

//Prints out a menu
System.out.println("Select one of the following four options: \n"+
"1. Count the number of times a specific character occurs in the DNA\n"+
"2. Count the number of a pair of characters in the DNA sequence\n"+
"3. Create the RNA sequence from the DNA sequence input\n"+
"4. Create the complement of the DNA sequence");

int menu = in.nextInt();

methodClass m = new methodClass();

//switch statement to handle menu choice
switch(menu)
{
case 1:
System.out.print("Please enter a character: ");
String interestedchar = in.next();

char s = interestedchar.charAt(0);

int number = m.countCharacter(dna, s);
System.out.println("The character "+interestedchar+" appears "+number+" times in the dna sequence. ");
break;
case 2:

break;
case 3:
break;
case 4:
System.out.println("The original dna sequence is " + dna);
m.replace(dna);
break;
}
}

}



class methodClass(contains all the methods you should call)
Code:


public class methodClass
{
    

   
    public methodClass()
    {
        
    }
    

public int countCharacter(String str, char lookup)
{
int count = 0;

for(int i = 0; i<str.length(); i++)
{
if(str.charAt(i) == lookup)
++count;

}
return count;
} 
    
    
    
   
    public void replace(String one){

        char[] chars = new char[one.length()];

        for(int i = 0; i < one.length(); ++i){
        if(one.charAt(i) == 'a')
           chars[i] = 't'; //replace a with t here

        else if(one.charAt(i) == 't')
                 chars[i] = 'a'; //replace t with a here

        else if(one.charAt(i) == 'g')
                 chars[i] = 'c'; //replace g with c here

        else
             chars[i] = 'g'; //replace c with g here
            }
         System.out.print("The complement of the dna sequence is ");   
         System.out.println(chars);
            
        }
}

Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #24 of 26
Thread Starter 
I'm still getting an error with methodClass m = new methodClass(); saying non static variable this cannot be referenced from a static context.
post #25 of 26
Quote:
Originally Posted by wire;15564117 
I'm still getting an error with methodClass m = new methodClass(); saying non static variable this cannot be referenced from a static context.

the only thing static at this point should be your main declaration
Code:
public static void main(String args[])


class methodClass should be outside main

Here is my output window:

unledohi.jpg
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #26 of 26
Code:
import java.util.Scanner;
public class dna
{

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.print("Please enter a DNA sequence ");
String dna = in.nextLine();

//Performs a check to test the validity of the inputed DNA sequence
int i = 0;
while (i < dna.length())
{
char ch = dna.charAt(i);

if (ch == 'a' || ch=='c' || ch=='g' || ch=='t')
{
i++;
}
else
{
System.out.print("Invalid DNA sequence. Please enter a DNA sequence ");
dna = in.nextLine();
} 
}

//Prints out a menu
System.out.println("Select one of the following four options: \n"+
"1. Count the number of times a specific character occurs in the DNA\n"+
"2. Count the number of a pair of characters in the DNA sequence\n"+
"3. Create the RNA sequence from the DNA sequence input\n"+
"4. Create the complement of the DNA sequence");

int menu = in.nextInt();



//switch statement to handle menu choice
switch(menu)
{
case 1:
System.out.print("Please enter a character: ");
String interestedchar = in.next();

char s = interestedchar.charAt(0);

int number = countCharacter(dna, s);
System.out.println("The character "+interestedchar+" appears "+number+" times in the dna sequence. ");
break;
case 2:

break;
case 3:
break;
case 4:
System.out.println("The original dna string is " + dna);
replace(dna);
break;
}
}

public static int countCharacter(String str, char lookup)
{
int count = 0;

for(int i = 0; i<str.length(); i++)
{
if(str.charAt(i) == lookup)
++count;

}
return count;
} 


public static void replace(String one){

        char[] chars = new char[one.length()];

        for(int i = 0; i < one.length(); ++i){
        if(one.charAt(i) == 'a')
           chars[i] = 't'; //replace a with t here

        else if(one.charAt(i) == 't')
                 chars[i] = 'a'; //replace t with a here

        else if(one.charAt(i) == 'g')
                 chars[i] = 'c'; //replace g with c here

        else
             chars[i] = 'g'; //replace c with g here
            }
         System.out.print("The complement of the dna sequence is ");   
         System.out.println(chars);
            
        }



}
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Programming Help